gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:07:34 27 4
gpt4 key购买 nike

我有一组图像,其中包含数字的方框和圆圈。一些方框和圆圈没有四面相连,线条之间存在一些间隙。

enter image description here

我想填补这个空白(如图所示),有人对使用 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)

最佳答案

一个dilation应该就够了,关键是要用vertical kernel,尝试改变内核的尺寸 例如 内核 = np.ones((5,2),np.uint8)检查 1 次扩张后的结果,它应该缩小差距。

关于python - OpenCV,如何加入/填充线或框或圆中的间隙(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55706407/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com