首页 > 解决方案 > 使用输入的种子点过滤双阈值图像

问题描述

我已将多边形内的种子点标记为该多边形的双重阈值。在阈值图像取决于我标记的种子点之后,我想删除其余的连接组件。根据裁剪的图像,我有种子点的坐标。我想删除其余不需要的标记组件以设置为零并仅显示必要的图像片段。想象中心坐标为(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

标签: pythonopencv

解决方案


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

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

推荐阅读