如何删除未连接到二进制图像中的循环的白色像素

时间:2019-08-08 14:04:29

标签: python python-3.x opencv image-morphology image-thresholding

我有一个已image进行过二值化处理的问题,问题是我想删除该图像中未成环连接的白点,即小的白点。原因是我想对here.

所示的部分进行度量

我尝试了一些OpenCV形态功能,例如腐蚀,打开,关闭,但结果不是我所需要的。我也尝试过使用Canny Edge,但是我想要进行一些处理的对角线也消失了。 here 是thresh(左)和canny(右)的结果

我已经知道修剪是一个过程,其中我们删除了未连接的像素,但我不知道它是如何工作的? Opencv中有为此功能吗?

th3 = cv2.adaptiveThreshold(gray_img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
th3=~th3
th3 = cv2.dilate(th3, cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)))
th3 = cv2.erode(th3, cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)))
edged = cv2.Canny(gray_img,lower,upper)

1 个答案:

答案 0 :(得分:2)

此答案说明了如何测量钻头的直径:

第一步是将图像读取为2通道(灰度),并使用Canny滤镜查找边缘:

img = cv2.imread('/home/stephen/Desktop/drill.jpg',0)
canny = cv2.Canny(img, 100, 100)

canny

使用np.argmax()进行钻取的边缘:

diameters = []
# Iterate through each column in the image
for row in range(img.shape[1]-1):
    img_row = img[:, row:row+1]
    start = np.argmax(img_row)
    end = np.argmax(np.flip(img_row))
    diameters.append(end+start)
    a = row, 0
    b = row, start
    c = row, img.shape[1]
    d = row, img.shape[0] - end
    cv2.line(img, a, b, 123, 1)
    cv2.line(img, c, d, 123, 1)

outline

每列中钻头的直径在此处绘制:

diameters

所有这些都假定您具有对齐的图像。我使用this code来对齐图像。