【发布时间】:2025-11-29 19:40:02
【问题描述】:
我有一组图像,其中的方框和圆圈包含数字。一些方框和圆圈没有在所有边上连接,线条之间存在一些间隙。
我想填补这个空白(如图所示),有人知道使用 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)
【问题讨论】:
-
有很多不同的方法可以做到这一点。到目前为止,您尝试过什么?
-
@Grillteller,请查看更新后的问题。
标签: python opencv opencv3.0 gaussian canny-operator