基于opencv的车牌提取项目

初学图像处理,做了一个车牌提取项目,本博客仅仅是为了记录一下学习过程,该项目只具备初级功能,还有待改善

第一部分:车牌倾斜矫正

# 导入所需模块
import cv2
import math
from matplotlib import pyplot as plt

# 显示图片
def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey()
    cv2.destroyAllWindows()

# 调整图片大小
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]
    if width is None and height is None:
        return image
    if width is None:
        r = height / float(h)
        dim = (int(w * r), height)
    else:
        r = width / float(w)
        dim = (width, int(h * r))
    resized = cv2.resize(image, dim, interpolation=inter)
    return resized

# 加载图片
origin_Image = cv2.imread("./images/car_09.jpg")
rawImage = resize(origin_Image,height=500)

# 高斯去噪
image = cv2.GaussianBlur(rawImage, (3, 3), 0)
# 灰度处理
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# sobel算子边缘检测(做了一个y方向的检测)
Sobel_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0)
absX = cv2.convertScaleAbs(Sobel_x)  # 转回uint8
image = absX
# 自适应阈值处理
ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
# 闭运算,是白色部分练成整体
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (14, 5))
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX,iterations = 1)
# 去除一些小的白点
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 19))
# 膨胀,腐蚀
image = cv2.dilate(image, kernelX)
image = cv2.erode(image, kernelX)
# 腐蚀,膨胀
image = cv2.erode(image, kernelY)
image = cv2.dilate(image, kernelY)
# 中值滤波去除噪点
image = cv2.medianBlur(image, 15)
# 轮廓检测
# cv2.RETR_EXTERNAL表示只检测外轮廓
# cv2.CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息
thresh_, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
image1 = rawImage.copy()
cv2.drawContours(image1, contours, -1, (0, 255, 0), 5)
cv_show('image1',image1)

# 筛选出车牌位置的轮廓
# 这里我只做了一个车牌的长宽比在3:1到4:1之间这样一个判断
for i,item in enumerate(contours): # enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中
    # cv2.boundingRect用一个最小的矩形,把找到的形状包起来
    rect = cv2.boundingRect(item)
    x = rect[0]
    y = rect[1]
    weight = rect[2]
    height = rect[3]
    if (weight > (height * 1.5)) and (weight < (height * 4)) and height>50:
        index = i
        image2 = rawImage.copy()
        cv2.drawContours(image2, contours, index, (0, 0, 255), 3)
        cv_show('image2',image2)
# 参数:
#  https://blog.csdn.net/lovetaozibaby/article/details/99482973
# InputArray Points: 待拟合的直线的集合,必须是矩阵形式;
# distType: 距离类型。fitline为距离最小化函数,拟合直线时,要使输入点到拟合直线的距离和最小化。这里的 距离的类型有以下几种:
# cv2.DIST_USER : User defined distance
# cv2.DIST_L1: distance = |x1-x2| + |y1-y2|
# cv2.DIST_L2: 欧式距离,此时与最小二乘法相同
# cv2.DIST_C:distance = max(|x1-x2|,|y1-y2|)
# cv2.DIST_L12:L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1))
# cv2.DIST_FAIR:distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998
# cv2.DIST_WELSCH: distance = c2/2(1-exp(-(x/c)2)), c = 2.9846
# cv2.DIST_HUBER:distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345
# param: 距离参数,跟所选的距离类型有关,值可以设置为0。
#
# reps, aeps: 第5/6个参数用于表示拟合直线所需要的径向和角度精度,通常情况下两个值均被设定为1e-2.
# output :
#
# 对于二维直线,输出output为4维,前两维代表拟合出的直线的方向,后两位代表直线上的一点。(即通常说的点斜式直线)
# 其中(vx, vy) 是直线的方向向量,(x, y) 是直线上的一个点。
# 斜率k = vy / vx
# 截距b = y - k * x

# 直线拟合找斜率
cnt = contours[index]
image3 = rawImage.copy()
h, w = image3.shape[:2]
[vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01)
k = vy/vx
b = y-k*x
lefty = b
righty = k*w+b
img = cv2.line(image3, (w, righty), (0, lefty), (0, 255, 0), 2)
cv_show('img',img)

a = math.atan(k)
a = math.degrees(a)
image4 = origin_Image.copy()
# 图像旋转
h,w = image4.shape[:2]
print(h,w)
#第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例
M = cv2.getRotationMatrix2D((w/2,h/2),a,1)
#第三个参数:变换后的图像大小
dst = cv2.warpAffine(image4,M,(int(w*1),int(h*1)))
cv_show('dst',dst)
cv2.imwrite('car_09.jpg',dst)

 

 

第二部分:车牌号码提取

 1 # 导入所需模块
 2 import cv2
 3 from matplotlib import pyplot as plt
 4 import os
 5 import numpy as np
 6 
 7 # 显示图片
 8 def cv_show(name,img):
 9     cv2.imshow(name,img)
10     cv2.waitKey()
11     cv2.destroyAllWindows()
12 
13 def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
14     dim = None
15     (h, w) = image.shape[:2]
16     if width is None and height is None:
17         return image
18     if width is None:
19         r = height / float(h)
20         dim = (int(w * r), height)
21     else:
22         r = width / float(w)
23         dim = (width, int(h * r))
24     resized = cv2.resize(image, dim, interpolation=inter)
25     return resized
26 
27 #读取待检测图片
28 origin_image = cv2.imread('./car_09.jpg')
29 resize_image = resize(origin_image,height=600)
30 ratio = origin_image.shape[0]/600
31 print(ratio)
32 cv_show('resize_image',resize_image)
33 
34 
35 #高斯滤波,灰度化
36 image = cv2.GaussianBlur(resize_image, (3, 3), 0)
37 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
38 cv_show('gray_image',gray_image)
39 
40 #梯度化
41 Sobel_x = cv2.Sobel(gray_image, cv2.CV_16S, 1, 0)
42 absX = cv2.convertScaleAbs(Sobel_x)
43 image = absX
44 cv_show('image',image)
45 
46 #闭操作
47 ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)
48 kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1))
49 image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernelX, iterations=2)
50 cv_show('image',image)
51 kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 1))
52 kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
53 image = cv2.dilate(image, kernelX)
54 image = cv2.erode(image, kernelX)
55 image = cv2.erode(image, kernelY)
56 image = cv2.dilate(image, kernelY)
57 image = cv2.medianBlur(image, 9)
58 cv_show('image',image)
59 
60 #绘制轮廓
61 thresh, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
62 print(type(contours))
63 print(len(contours))
64 
65 cur_img = resize_image.copy()
66 cv2.drawContours(cur_img,contours,-1,(0,0,255),3)
67 cv_show('img',cur_img)
68 
69 for item in contours:
70     rect = cv2.boundingRect(item)
71     x = rect[0]
72     y = rect[1]
73     weight = rect[2]
74     height = rect[3]
75     if (weight > (height * 2.5)) and (weight < (height * 4)):
76         if height > 40 and height < 80:
77             image = origin_image[int(y*ratio): int((y + height)*ratio), int(x*ratio) : int((x + weight)*ratio)]
78 cv_show('image',image)
79 cv2.imwrite('chepai_09.jpg',image)

 

第三部分:车牌号码分割:

 1 # 导入所需模块
 2 import cv2
 3 from matplotlib import pyplot as plt
 4 import os
 5 import numpy as np
 6 
 7 # 显示图片
 8 def cv_show(name,img):
 9     cv2.imshow(name,img)
10     cv2.waitKey()
11     cv2.destroyAllWindows()
12 
13 #车牌灰度化
14 chepai_image = cv2.imread('chepai_09.jpg')
15 image = cv2.GaussianBlur(chepai_image, (3, 3), 0)
16 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
17 cv_show('gray_image',gray_image)
18 
19 ret, image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU)
20 cv_show('image',image)
21 
22 #计算二值图像黑白点的个数,处理绿牌照问题,让车牌号码始终为白色
23 area_white = 0
24 area_black = 0
25 height, width = image.shape
26 for i in range(height):
27     for j in range(width):
28         if image[i, j] == 255:
29             area_white += 1
30         else:
31             area_black += 1
32 print(area_black,area_white)
33 if area_white > area_black:
34     ret, image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
35     cv_show('image',image)
36 
37 #绘制轮廓
38 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 5))
39 image = cv2.dilate(image, kernel)
40 cv_show('image', image)
41 thresh, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
42 cur_img = chepai_image.copy()
43 cv2.drawContours(cur_img,contours,-1,(0,0,255),3)
44 cv_show('img',cur_img)
45 words = []
46 word_images = []
47 print(len(contours))
48 for item in contours:
49     word = []
50     rect = cv2.boundingRect(item)
51     x = rect[0]
52     y = rect[1]
53     weight = rect[2]
54     height = rect[3]
55     word.append(x)
56     word.append(y)
57     word.append(weight)
58     word.append(height)
59     words.append(word)
60 words = sorted(words, key=lambda s:s[0], reverse=False)
61 print(words)
62 i = 0
63 for word in words:
64     if (word[3] > (word[2] * 1)) and (word[3] < (word[2] * 5)):
65         i = i + 1
66         splite_image = chepai_image[word[1]:word[1] + word[3], word[0]:word[0] + word[2]]
67         word_images.append(splite_image)
68 
69 for i, j in enumerate(word_images):
70     cv_show('word_images[i]',word_images[i])
71     cv2.imwrite("./chepai_09/0{}.png".format(i),word_images[i])
View Code

 

第四部分:字符匹配:

  1 # 导入所需模块
  2 import cv2
  3 from matplotlib import pyplot as plt
  4 import os
  5 import numpy as np
  6 
  7 # 准备模板
  8 template = ['0','1','2','3','4','5','6','7','8','9',
  9             'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
 10             '','','','','','','','','','','','','','','','','','','',
 11             '','','','','','','','','','','','']
 12 
 13 # 显示图片
 14 def cv_show(name,img):
 15     cv2.imshow(name,img)
 16     cv2.waitKey()
 17     cv2.destroyAllWindows()
 18 
 19 # 读取一个文件夹下的所有图片,输入参数是文件名,返回文件地址列表
 20 def read_directory(directory_name):
 21     referImg_list = []
 22     for filename in os.listdir(directory_name):
 23         referImg_list.append(directory_name + "/" + filename)
 24     return referImg_list
 25 
 26 # 中文模板列表(只匹配车牌的第一个字符)
 27 def get_chinese_words_list():
 28     chinese_words_list = []
 29     for i in range(34,64):
 30         c_word = read_directory('./refer1/'+ template[i])
 31         chinese_words_list.append(c_word)
 32     return chinese_words_list
 33 
 34 #英文模板列表(只匹配车牌的第二个字符)
 35 def get_english_words_list():
 36     eng_words_list = []
 37     for i in range(10,34):
 38         e_word = read_directory('./refer1/'+ template[i])
 39         eng_words_list.append(e_word)
 40     return eng_words_list
 41 
 42 # 英文数字模板列表(匹配车牌后面的字符)
 43 def get_eng_num_words_list():
 44     eng_num_words_list = []
 45     for i in range(0,34):
 46         word = read_directory('./refer1/'+ template[i])
 47         eng_num_words_list.append(word)
 48     return eng_num_words_list
 49 
 50 #模版匹配
 51 def template_matching(words_list):
 52     if words_list == 'chinese_words_list':
 53         template_words_list = chinese_words_list
 54         first_num = 34
 55     elif words_list == 'english_words_list':
 56         template_words_list = english_words_list
 57         first_num = 10
 58     else:
 59         template_words_list = eng_num_words_list
 60         first_num = 0
 61     best_score = []
 62     for template_word in template_words_list:
 63         score = []
 64         for word in template_word:
 65             template_img = cv2.imdecode(np.fromfile(word, dtype=np.uint8), 1)
 66             template_img = cv2.cvtColor(template_img, cv2.COLOR_RGB2GRAY)
 67             ret, template_img = cv2.threshold(template_img, 0, 255, cv2.THRESH_OTSU)
 68             height, width = template_img.shape
 69             image = image_.copy()
 70             image = cv2.resize(image, (width, height))
 71             result = cv2.matchTemplate(image, template_img, cv2.TM_CCOEFF)
 72             score.append(result[0][0])
 73         best_score.append(max(score))
 74     return template[first_num + best_score.index(max(best_score))]
 75 
 76 referImg_list = read_directory("chepai_13")
 77 chinese_words_list = get_chinese_words_list()
 78 english_words_list = get_english_words_list()
 79 eng_num_words_list = get_eng_num_words_list()
 80 chepai_num = []
 81 
 82 #匹配第一个汉字
 83 img = cv2.imread(referImg_list[0])
 84 # 高斯去噪
 85 image = cv2.GaussianBlur(img, (3, 3), 0)
 86 # 灰度处理
 87 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
 88 # 自适应阈值处理
 89 ret, image_ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU)
 90 #第一个汉字匹配
 91 chepai_num.append(template_matching('chinese_words_list'))
 92 print(chepai_num[0])
 93 
 94 #匹配第二个英文字母
 95 img = cv2.imread(referImg_list[1])
 96 # 高斯去噪
 97 image = cv2.GaussianBlur(img, (3, 3), 0)
 98 # 灰度处理
 99 gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
100 # 自适应阈值处理
101 ret, image_ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU)
102 #第二个英文字母匹配
103 chepai_num.append(template_matching('english_words_list'))
104 print(chepai_num[1])
105 
106 #匹配其余5个字母,数字
107 for i in range(2,7):
108     img = cv2.imread(referImg_list[i])
109     # 高斯去噪
110     image = cv2.GaussianBlur(img, (3, 3), 0)
111     # 灰度处理
112     gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
113     # 自适应阈值处理
114     ret, image_ = cv2.threshold(gray_image, 0, 255, cv2.THRESH_OTSU)
115     #其余字母匹配
116     chepai_num.append(template_matching('eng_num_words_list'))
117     print(chepai_num[i])
118 print(chepai_num)
View Code

 

posted @ 2020-06-28 17:22  *精灵鼠*  阅读(1146)  评论(0编辑  收藏  举报