问题描述
我一直致力于仅使用 OpenCV 检测摩托车牌照。我的问题是:
- 我能否仅找到并绘制最大的轮廓(在车牌周围)?
- 如果没有,我该怎么办?
- 还有其他检测摩托车牌照的方法吗?
这是源代码
import cv2
image = cv2.imread('image_0002.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray,(3,3),0)
canny = cv2.Canny(blurred,120,255,1)
# Find contours
cnts = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Iterate thorugh contours and draw rectangles around contours
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image,(x,y),(x + w,y + h),(36,12),2)
cv2.imshow('canny',canny)
cv2.imshow('image',image)
cv2.imwrite('canny.png',canny)
cv2.imwrite('image.png',image)
cv2.waitKey(0)
解决方法
1- 对于最大的矩形 - 计算所有矩形的面积
largest_area = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
area = 4*w*h
if area > largest_area:
largest_area = area
largest_area_contour = c
现在,我们得到了最大的矩形。让我们绘制它(在 for loop
之外)。
x,h = cv2.boundingRect(largest_area_contour)
cv2.rectangle(image,(x,y),(x + w,y + h),(36,255,12),2)
2- 你的模型给了你太多的误报。确保最大矩形方法始终有效。
3- 车牌(汽车或自行车无关紧要)阅读是一个经过充分研究的玩具问题。你会在互联网上找到很多讨论。