我有一組圖像,其中的框和圓圈包含數字。一些盒子和圓圈沒有在所有邊上連接,線條上有一些間隙。我想填補這個空白(如圖所示),任何人都知道使用 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)
2 回答

子衿沉夜
TA貢獻1828條經驗 獲得超3個贊
一次膨脹就足夠了,關鍵是使用垂直內核,嘗試更改內核的尺寸,例如 kernel = np.ones((5,2),np.uint8) 檢查 1 次膨脹后的結果,它應該縮小差距.

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
您可以嘗試使用輪廓嗎?輪廓可以簡單地解釋為連接所有連續點(沿邊界)的曲線,具有相同的顏色或強度。首先找到所有的計數,然后將輪廓繪制到原始圖像上。這可能會解決問題
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)
添加回答
舉報
0/150
提交
取消