OpenCV,如何加入/填充线条或框或圆圈中的间隙(Python)

我有一组图像,其中的框和圆圈包含数字。一些盒子和圆圈没有在所有边上连接,线条上有一些间隙。

http://img2.mukewang.com/61dd4a96000146f000760093.jpg

我想填补这个空白(如图所示),任何人都知道使用 OpenCV ......?


我的代码在这里…………


    self.res = cv2.imread("1.jpg")


    self.store_path = "../storage-data/" 


    #define kernal value

    kernel = np.ones((2,2),np.uint8)


    #grayscale

    gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)


    dilate = cv2.dilate(gray,kernel,iterations = 1)

    dilate1 = cv2.dilate(dilate,kernel,iterations = 1)


    dilate1 = cv2.morphologyEx(dilate1, cv2.MORPH_OPEN, kernel)


    #Canny

    canny = cv2.Canny(dilate1,160,160,3)


    dilate = cv2.dilate(canny,kernel,iterations = 1)


    #Gaussian Blurring

    blur = cv2.GaussianBlur(dilate,(5,5),0)

    erode = cv2.dilate(blur,kernel,iterations = 1)

    blur = cv2.GaussianBlur(erode,(5,5),1)


    blur = cv2.morphologyEx(blur, cv2.MORPH_CLOSE, kernel)

    ret, thresh = cv2.threshold(blur,127,255,cv2.THRESH_BINARY)


    blur = cv2.GaussianBlur(thresh,(5,5),1)

    ret1, thresh1 = cv2.threshold(blur,127,255,cv2.THRESH_BINARY)


    opening = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel)


    cv2.imwrite(self.store_path + 'opening.jpg', opening)


    contours, hierarchy = cv2.findContours(opening,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)


皈依舞
浏览 352回答 2
2回答

子衿沉夜

一次膨胀就足够了,关键是使用垂直内核,尝试更改内核的尺寸,例如 kernel = np.ones((5,2),np.uint8) 检查 1 次膨胀后的结果,它应该缩小差距.

繁华开满天机

您可以尝试使用轮廓吗?轮廓可以简单地解释为连接所有连续点(沿边界)的曲线,具有相同的颜色或强度。首先找到所有的计数,然后将轮廓绘制到原始图像上。这可能会解决问题contours, _ = cv2.findContours(canny, 2,2)length = len(contours)for i in range(len(contours)):    cv2.drawContours(img, contours, i, (0,255,0), 2, cv2.LINE_8)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python