如何在opencv python中使用分水岭分割

我有一个问题,即如何使用 python 中的分水岭分割单独分割该图像中的粒子。我的主要目标是通过应用过滤器 medianBlur 然后应用 Canny 边缘检测方法来去除噪声。


[![img = cv2.imread('sands.jpg')

img = cv2.medianBlur(img,7)

img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

imo = cv2.Canny(img,140,255)][1]][1]

我想增强 Canny 边缘检测功能产生的轮廓,因为我使用这些图像来检测图像中粒子的区域属性以估计面积。

http://img4.mukewang.com/6285f94c00019ed306580831.jpg

回首忆惘然
浏览 119回答 1
1回答

德玛西亚99

这是改编自这篇博文的一种方法将图像转换为灰度获得二值图像的 Otsu 阈值计算欧几里得距离变换执行连通分量分析应用分水岭遍历标签值并提取对象这是结果在遍历每个轮廓时,您可以累积总面积1388903.5import cv2import numpy as npfrom skimage.feature import peak_local_maxfrom skimage.morphology import watershedfrom scipy import ndimage# Load in image, convert to gray scale, and Otsu's thresholdimage = cv2.imread('1.jpg')gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]# Compute Euclidean distance from every binary pixel# to the nearest zero pixel then find peaksdistance_map = ndimage.distance_transform_edt(thresh)local_max = peak_local_max(distance_map, indices=False, min_distance=20, labels=thresh)# Perform connected component analysis then apply Watershedmarkers = ndimage.label(local_max, structure=np.ones((3, 3)))[0]labels = watershed(-distance_map, markers, mask=thresh)# Iterate through unique labelstotal_area = 0for label in np.unique(labels):    if label == 0:        continue    # Create a mask    mask = np.zeros(gray.shape, dtype="uint8")    mask[labels == label] = 255    # Find contours and determine contour area    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    cnts = cnts[0] if len(cnts) == 2 else cnts[1]    c = max(cnts, key=cv2.contourArea)    area = cv2.contourArea(c)    total_area += area    cv2.drawContours(image, [c], -1, (36,255,12), 4)print(total_area)cv2.imshow('image', image)cv2.waitKey()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python