多年前,使用写的一个车牌识别程序。里面需要用到CV2进行图像采集、分割处理,用python编写,深度学习识别部分可以自己训练或者采用各种云上提供的人工智能服务。程序仅仅涉及主要流程处理,没有界面,只能处理蓝牌车和绿牌车,对于更多的车牌类型也没有去研究,仅仅在于摸清楚原理。另外,训练深度学习,采用笔记本电脑,确实太费时间,这帖子不写。
主要流程
imgread = cv2.imread('mytest\img2.jpg')
frame=location_demo(imgread)
cv2.imwrite("img70.jpg",frame)
cv2.imshow("vehicle plate location", frame)
cv2.waitKey(0)
my_cut("img70.jpg")
cn=Ocr()
res=cn.ocr("img80.jpg")
print(res)
cv2.destroyAllWindows()
定位车牌
定位车牌主要通过设置色彩空间范围,查找到车牌响应色彩的多边形,通过多边形的面积,查找出车牌。因为一定距离,车牌的面积是相对稳定的。最后画出车牌轮廓,用于调试;剪裁出图片,可以给后续程序使用。
def location_demo(img):
image = cv2.resize(img,(1200,1200))
lower_blue = np.array([100, 80, 60])
upper_blue = np.array([130, 255, 255])
lower_green = np.array([30, 100, 100])
upper_green = np.array([78, 255, 255])
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_green = cv2.inRange(hsv, lower_green, upper_green)
output = cv2.bitwise_and(hsv, hsv, mask=mask_blue)
Matrix = np.ones((20, 20), np.uint8)
cv2.imshow('img_edge2', output)
cv2.waitKey(0)
img_edge1 = cv2.morphologyEx(output, cv2.MORPH_CLOSE, Matrix)
cv2.imshow('img_edge2', img_edge1)
cv2.waitKey(0)
img_edge2 = cv2.morphologyEx(img_edge1, cv2.MORPH_OPEN, Matrix)
cv2.imshow('img_edge2',img_edge2)
cv2.waitKey(0)
img_edge3 = np.clip(img_edge2, 0, 255)
contours , hierarchy = cv2.findContours(img_edge3[...,0],cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print(contours)
cv2.imshow('img_edge2', img_edge3)
cv2.waitKey(0)
for i in range(0, len(contours)):
cnt = contours[i]
area = cv2.contourArea(cnt)
if area >3000 :
rect=cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
print(area)
print(box)
cj =cut1(image,box)
cv2.imshow("vehicle plate location", cj)
cv2.waitKey(0)
image = cv2.drawContours(image, [box], -1, (0, 255, ), 4)
cv2.imshow("vehicle plate location", image)
cv2.waitKey(0)
return cj
分割车牌
def my_cut(img_name):
loc=[40,80,96,136,176,216,256,296]
img = cv2.imread(img_name)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img_gray = cv2.GaussianBlur(img_gray, (3,3),0)
immean=cv2.mean(img)
img_gray2= cv2.equalizeHist(img_gray)
cv2.imshow('gray2',img_gray2)
cv2.waitKey(0)
if immean[0]>immean[1]:
binary_threshold = 190
else:
binary_threshold = 80
img_thre = img_gray2
cv2.threshold(img_gray2, binary_threshold, 255, cv2.THRESH_BINARY_INV, img_thre)
cv2.imshow('lumk', img_thre)
cv2.waitKey(0)
height = img_thre.shape[0]
width = img_thre.shape[1]
img_thre = cv2.resize(img_thre, (300, 80))
cv2.imwrite('img80.jpg', img_thre)
old=0
ind=1
for i in loc:
cj = img_thre[1:80, old:i]
cv2.imwrite('d:\\img\\ii' + format(ind) + '.bmp', cj)
image3 = cv2.imread('d:\\img\\ii' + format(ind) + '.bmp', cv2.IMREAD_GRAYSCALE)
image3 = cv2.resize(image3, (32, 40))
cv2.bitwise_not(image3, image3)
cv2.imwrite('d:\\img\\ii' + format(ind) + '.bmp', image3)
old=i
ind=ind+1
cv2.imshow('cutChar', cj)
cv2.waitKey(0)
'''
white = []
black = []
height = img_thre.shape[0]
width = img_thre.shape[1]
print(width, height)
white_max = 0
black_max = 0
for i in range(width):
w_count = 0
b_count = 0
for j in range(height):
if img_thre[j][i] == 255:
w_count += 1
else:
b_count += 1
white_max = max(white_max, w_count)
black_max = max(black_max, b_count)
white.append(w_count)
black.append(b_count)
识别车牌
识别车牌字母的两种方式,采用自己训练的车牌识别程序,我训练了一周,而且数据获取,太麻烦,太费事,最终只实现了字母和数字识别,汉字没有做。 最终,用了网络上的识别程序实现,识别率非常高,完美!
过程输出
原始图,采用手机拍摄,由于车牌是别人的,所以改了号码。 这是定位到的图片位置 下面是根据图片位置裁剪和变换的结果。 最后是识别的输出。
|