使用输入的种子点过滤双阈值图像



我已经在多边形内标记了一个种子点,以对该多边形进行双阈值。我想在阈值图像取决于我标记的种子点之后删除其余连接的组件。我根据裁剪的图像有种子点的坐标。我想删除其余不需要的标记组件以设置为零,并仅显示必要的图像段。想象一下,中心坐标为 (x=37,y=40(。

种子点为 1 的提取图像

用于提取图像2的双阈值图像

以下是从提取的图像中获取连接组件的代码。

def dual_threshold_method(crop_img):
mean, meanStd = cv2.meanStdDev(crop_img)
w = 2.0
lower_Threshold = mean - (w * meanStd) + 90
upper_Threshold = mean + (w * meanStd) + 90
ret1, img1 = cv2.threshold(crop_img, int(lower_Threshold), 255, cv2.THRESH_BINARY)
ret2, img2 = cv2.threshold(crop_img, int(upper_Threshold), 255, cv2.THRESH_BINARY)
dual_threshold_img = img1 - img2
retval, labels = cv2.connectedComponents(dual_threshold_img)
cv2.imshow('Dual_Threshold_Image', dual_threshold_img)
return labels, retval

https://drive.google.com/drive/folders/1UbXkYyCiMqVRXhn_KuSaKRHDUtQWhdnl?usp=sharing

上面的链接包含双阈值图像和种子点标记图像

预期结果将与下图相同

https://drive.google.com/open?id=14zaQWz9C3qRNN-niM4LI1-iCenk7QIMN

通过使用中心坐标像素强度过滤并添加扩张和侵蚀来解决这个问题。

pixel_value = labels[cropped_image_center_coordinates[0][0],cropped_image_center_coordinates[0][1]]
mask = np.array(labels, dtype=np.uint8)
mask[labels == pixel_value] = 255
kernel = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(mask, kernel, iterations=1)
erosion = cv2.erode(dilation, kernel, iterations=1)
cv2.imshow('component', erosion)enter code here

最新更新