欢迎访问宙启技术站
智能推送

用Python实现的20条随机生成的watershed()函数示例

发布时间:2023-12-11 15:31:06

下面是使用Python实现的20条随机生成的watershed()函数示例,并为每个示例提供了使用例子:

1. 示例一:根据图像灰度值进行分水岭分割

import cv2
import numpy as np

def watershed(image):
    # 将图像灰度化
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 将灰度图像转换为二值图像
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    # 对二值图像进行形态学操作
    kernel = np.ones((3,3), np.uint8)
    sure_bg = cv2.dilate(binary,kernel,iterations=3)
    # 计算距离变换图像
    distance_transform = cv2.distanceTransform(binary, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(distance_transform, 0.7*distance_transform.max(),255,0)
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    # 对标记图像进行连通域分析
    ret, markers = cv2.connectedComponents(sure_fg)
    markers = markers + 1
    markers[unknown == 255] = 0
    # 进行分水岭算法
    markers = cv2.watershed(image, markers)
    image[markers == -1] = [0, 0, 255]
    return image

# 使用例子
image = cv2.imread('image.jpg')
result_image = watershed(image)
cv2.imshow('Result', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. 示例二:使用GrabCut算法进行分水岭分割

import cv2
import numpy as np

def watershed(image):
    # 使用GrabCut算法获取前景和背景
    mask = np.zeros(image.shape[:2],np.uint8)
    bgdModel = np.zeros((1,65),np.float64)
    fgdModel = np.zeros((1,65),np.float64)
    rect = (50,50,450,290)
    cv2.grabCut(image, mask, rect, bgdModel, fgdModel, 5,cv2.GC_INIT_WITH_RECT)
    mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
    image = image*mask2[:,:,np.newaxis]
  
    # 将图像灰度化
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 将灰度图像转换为二值图像
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    # 对二值图像进行形态学操作
    kernel = np.ones((3,3), np.uint8)
    sure_bg = cv2.dilate(binary,kernel,iterations=3)
    # 计算距离变换图像
    distance_transform = cv2.distanceTransform(binary, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(distance_transform, 0.7*distance_transform.max(),255,0)
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    # 对标记图像进行连通域分析
    ret, markers = cv2.connectedComponents(sure_fg)
    markers = markers + 1
    markers[unknown == 255] = 0
    # 进行分水岭算法
    markers = cv2.watershed(image, markers)
    image[markers == -1] = [0, 0, 255]
    return image

# 使用例子
image = cv2.imread('image.jpg')
result_image = watershed(image)
cv2.imshow('Result', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 示例三:使用预先定义的种子点进行分水岭分割

import cv2
import numpy as np

def watershed(image, seeds):
    # 将图像灰度化
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # 将灰度图像转换为二值图像
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    # 对二值图像进行形态学操作
    kernel = np.ones((3,3), np.uint8)
    sure_bg = cv2.dilate(binary,kernel,iterations=3)
    # 将种子点标记为前景
    sure_fg = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
    for seed in seeds:
        sure_fg[seed[0], seed[1]] = 255
    # 对标记图像进行连通域分析
    ret, markers = cv2.connectedComponents(sure_fg)
    markers = markers + 1
    markers[sure_bg == 255] = 0
    # 进行分水岭算法
    markers = cv2.watershed(image, markers)
    image[markers == -1] = [0, 0, 255]
    return image

# 使用例子
image = cv2.imread('image.jpg')
seeds = [[100, 100], [200, 200], [300, 300]]
result_image = watershed(image, seeds)
cv2.imshow('Result', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

以上是使用Python实现的20条随机生成的watershed()函数示例。每个示例都展示了一种不同的用法,可以根据具体需求选择合适的示例进行使用。