车牌定位的原理:https://blog.csdn.net/relocy/article/details/78705662
训练好的分类器:https://github.com/zeusees/HyperLPR/tree/master/model
这个开源项目中还有完整的车牌识别代码。
这里只是简单地根据这个项目训练好的分类器来实现了一下车牌定位,核心代码也是参考的这个项目。
代码:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import cv2
# 使用的是HyperLPR已经训练好了的分类器
watch_cascade = cv2.CascadeClassifier('./cascade.xml')
# 先读取图片
image = cv2.imread("1.jpg")
resize_h = 1000
height = image.shape[0]
scale = image.shape[1]/float(image.shape[0])
image = cv2.resize(image, (int(scale*resize_h), resize_h))
image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
watches = watch_cascade.detectMultiScale(image_gray, 1.1, 2, minSize=(36, 9), maxSize=(36*40, 9*40))
print("检测到车牌数", len(watches))
for (x, y, w, h) in watches:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 1)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果: