我正在尝试为邮政编码排序应用程序裁剪 6 位数的手写邮政编码,因为代码适用于 10 位数的电话号码,但无法识别 6 位数的轮廓。请更正给定的代码或提供更好的替代代码来完成上述任务。
import numpy as np
import cv2
import imutils
image = cv2.imread("pin5.jpg")
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
ret,thresh1 = cv2.threshold(gray ,127,255,cv2.THRESH_BINARY_INV)
dilate = cv2.dilate(thresh1, None, iterations=2)
cnts = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0]
orig = image.copy()
i = 0
for cnt in cnts:
if(cv2.contourArea(cnt) < 80):
continue
x,y,w,h = cv2.boundingRect(cnt)
roi = image[y:y+h, x:x+w]
cv2.rectangle(orig,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imwrite("roi" + str(i) + ".png", roi)
i = i + 1
cv2.imshow("Image", orig)
cv2.waitKey(0)