问题描述
我想用 OpenCV 和 Python 检测车牌,这是我的代码:
img = cv2.imread('C:/Users/ACER/Downloads/BikePlate3K/plate_train_2K/image_1703.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Grayscale
kernel = np.ones((5,5),np.uint8)
# dilation
dilation = cv2.dilate(gray,kernel,iterations = 1)
#Edge detection
edges = cv2.Canny(dilation,120,560,1) #120 255
squareKern = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
light = cv2.morphologyEx(edges,cv2.MORPH_CLOSE,squareKern)
light = cv2.threshold(light,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
thresh = cv2.bitwise_and(edges,edges,mask=light)
rectKern = cv2.getStructuringElement(cv2.MORPH_RECT,(4,3))
closing = cv2.morphologyEx(thresh,rectKern)
cnts = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(img,(x,y),(x + w,y + h),(255,0),2)
cv2.imshow("dilation",dilation)
cv2.imshow("Edges",edges)
cv2.imshow("Image",img)
cv2.waitKey(0)
这是我绘制每个检测到的轮廓后的结果。
那么问题是,我如何才能只绘制正确的矩形(它限制了车牌)?
(我想出了一些解决方案,比如找到最大的轮廓,但并不总是返回正确的)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)