python车牌识别_python实现车牌识别的⽰例代码某天回家之时,听到有个朋友说起他正在做⼀个车牌识别的项⽬
于是对其定位车牌的位置算法颇有兴趣,今⽇有空得以研究,事实上车牌识别算是⽐较成熟的技术了,
这⾥我只是简单实现。
我的思路为:
对图⽚进⾏⼀些预处理,包括灰度化、⾼斯平滑、中值滤波、Sobel算⼦边缘检测等等。
利⽤OpenCV对预处理后的图像进⾏轮廓查,然后根据⼀些参数判断该轮廓是否为车牌轮廓。
效果如下:
test1:
test2
实现代码如下(对图像预处理(滤波器等)的原理⽐较简单,这⾥只是对⼀些函数进⾏调包):
import cv2
import numpy as np
# 形态学处理
def Process(img):
# ⾼斯平滑
gaussian = cv2.GaussianBlur(img, (3, 3), 0, 0, cv2.BORDER_DEFAULT) # 中值滤波
median = dianBlur(gaussian, 5)
# Sobel算⼦
# 梯度⽅向: x
sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3)
# ⼆值化
ret, binary = cv2.threshold(sobel, 170, 255, cv2.THRESH_BINARY)
# 核函数
element1 = StructuringElement(cv2.MORPH_RECT, (9, 1))
element2 = StructuringElement(cv2.MORPH_RECT, (9, 7))
# 膨胀
dilation = cv2.dilate(binary, element2, iterations=1)
# 腐蚀
erosion = de(dilation, element1, iterations=1)
# 膨胀
dilation2 = cv2.dilate(erosion, element2, iterations=3)
return dilation2
def GetRegion(img):
regions = []
# 查轮廓
_, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for contour in contours:
area = urArea(contour)
if (area < 2000):
continue
eps = 1e-3 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, eps, True)
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
height = abs(box[0][1] - box[2][1])
width = abs(box[0][0] - box[2][0])
ratio =float(width) / float(height)
if (ratio < 5 and ratio > 1.8):
regions.append(box)
return regions
def detect(img):
# 灰度化
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
prc = Process(gray)
regions = GetRegion(prc)
print('[INFO]:Detect %d license plates' % len(regions))
for box in regions:
cv2.drawContours(img, [box], 0, (0, 255, 0), 2)
cv2.imshow('Result', img)
#保存结果⽂件名
cv2.imwrite('result2.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
python新手代码示例
#输⼊的参数为图⽚的路径
img = cv2.imread('test2.jpg')
detect(img)
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持我们。
本⽂标题: python实现车牌识别的⽰例代码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。