0


数字图像处理——实验五 基于图像分割的车牌定位识别

数字图像处理——实验五 基于图像分割的车牌定位识别

一、实验目的

(1)掌握车牌阈值分割;

(2)掌握基于形态学计算的图像分割;

(3)掌握图像的二值化;

(4)掌握基于像素投影的字符分割;

(5)掌握字符识别原理。

二、实验主要仪器设备

(1)计算机;

(2)Python 3.x及PyCharm软件;

(3)需进行车牌识别的图片。

  • 注:opencv-python 使用的是3.x 版本

三、实验原理

(1) 图像灰度化

灰度数字图像是每个像素只有一个采样颜色的图像。这类图像通常显示为从最暗黑色到最亮的白色的灰度,尽管理论上这个采样可以任何颜色的不同深浅,甚至可以是不同亮度上的不同颜色。灰度图像与黑白图像不同,在计算机图像领域中黑白图像只有黑白两种颜色,灰度图像在黑色与白色之间还有许多级的颜色深度。

(2) 图像二值化

图像二值化就是将图像上的像素点的灰度值设置为 0 或 255,也就是将整个图像呈现出明显的黑白效果。

(3) 图像形态学运算

用具有一定形态的结构元素去度量和提取图像中的对应形状以达到对图像分析和识别的目的。

(4) 阈值分割原理

阈值分割算法是图形分割中应用场景最多的算法之一。简单地说,对灰度图像进行阈值分割就是先确定一个处于图像灰度取值范围内的阈值,然后将图像中各个像素的灰度值与这个阈值比较,并根据比较的结果将对应的像素划分为两类:像素灰度大于阈值的一类和像素值小于阈值的另一类,灰度值等于阈值的像素可以归入这两类之一。分割后的两类像素一般分属图像的两个不同区域,所以对像素根据阈值分类达到了区域分割的目的。

(5) 字符分割原理

二值化后的图像,在没有字符的区域,y方向上像素灰度和为0,在有字符的区域为灰度和非0。

四、实验指导

4.1 车牌定位

按照下面给出的阈值遍历图片,选取适当区域进行分割。遍历图像可利用for循环遍历图片上所有点,遍历方法为:

  1. for i=1:m
  2. for j=1:n
  3. Rij=I(i,j,1);
  4. Gij=I(i,j,2);
  5. Bij=I(i,j,3);

其中

  1. I
  2. I
  3. I 为大小是
  4. m
  5. n
  6. m*n
  7. mn RGB图像,
  8. R
  9. i
  10. j
  11. R_{ij}
  12. Rij​、
  13. G
  14. i
  15. j
  16. G_{ij}
  17. Gij​、
  18. B
  19. i
  20. j
  21. B_{ij}
  22. Bij 分别为
  23. (
  24. i
  25. ,
  26. j
  27. )
  28. (i,j)
  29. (i,j) 点像素的RGB值,将三个值与下方给出的阈值比较,可得出像素是否属于车牌区域。

定位车牌区域时可以分别从行和列的角度进行遍历,即若某行符合要求的像素点数量大于等于某阈值时则认为该行属于车牌区域;遍历列时亦然,即若某列符合要求的像素点数量大于等于某阈值时则认为该列属于车牌区域。

车牌分割参考阈值:

  • RGB图像参考阈值

  1. R
  2. i
  3. j
  4. R_{ij}
  5. Rij​、
  6. G
  7. i
  8. j
  9. G_{ij}
  10. Gij​、
  11. B
  12. i
  13. j
  14. B_{ij}
  15. Bij 分别为
  16. (
  17. i
  18. ,
  19. j
  20. )
  21. (i,j)
  22. (i,j) 点的RGB值,则
  23. R
  24. i
  25. j
  26. R_{ij}
  27. Rij​/
  28. B
  29. i
  30. j
  31. B_{ij}
  32. Bij​<0.35
  33. G
  34. i
  35. j
  36. G_{ij}
  37. Gij​/
  38. B
  39. i
  40. j
  41. B_{ij}
  42. Bij​<0.9
  43. B
  44. i
  45. j
  46. B_{ij}
  47. Bij​>90
  48. G
  49. i
  50. j
  51. G_{ij}
  52. Gij​/
  53. B
  54. i
  55. j
  56. B_{ij}
  57. Bij​< 0.35
  58. R
  59. i
  60. j
  61. R_{ij}
  62. Rij​/
  63. B
  64. i
  65. j
  66. B_{ij}
  67. Bij​<0.9
  68. B
  69. i
  70. j
  71. B_{ij}
  72. Bij​<90
  • HSV图像参考阈值

也可将RGB图像转化为HSV图像进行阈值比较,记

  1. H
  2. i
  3. j
  4. H_{ij}
  5. Hij​、
  6. S
  7. i
  8. j
  9. S_{ij}
  10. Sij​、
  11. V
  12. i
  13. j
  14. V_{ij}
  15. Vij 分别为
  16. (
  17. i
  18. ,
  19. j
  20. )
  21. (i,j)
  22. (i,j) 点的HSV值,则

190 <

  1. H
  2. i
  3. j
  4. H_{ij}
  5. Hij < 2450.35 <
  6. S
  7. i
  8. j
  9. S_{ij}
  10. Sij <10.3 <
  11. V
  12. i
  13. j
  14. V_{ij}
  15. Vij < 1

根据检测到区域,将照片中车牌区域单独分割出来,为后面的字符分割做准备。

4.2 分割区域灰度化、二值化

4.1 中获得的车牌区域图片转化为灰度图像、二值图像,为后面的字符分割做准备。

4.3 车牌分割

二值化后的图像,在列方向,没有字符的区域,y方向上像素灰度和为0,在有字符的区域为灰度和非0,因此可根据灰度值在纵轴的投影对车牌二值图像进行分割;同理在行方向上也一样。根据此原理,可以将车牌中的字符单独分割出来,并且去除每个字符的上下方向上多余的边框。因此得到的字符分割结果,字符应该占满整个分割图像区域。为了便于后期的识别,因此将分割结果图片统一缩放为25*15大小。

4.4 车牌识别

本实验依照模版匹配进行识别。由于所给的模板中字符并没有占整个模板图片的区域,因此需要对模板进行去边框、缩放处理,原理与步骤 4.3 中类似。

将分割结果

  1. I
  2. I
  3. I 分别与模版
  4. I
  5. I
  6. I 进行比对,得出其差值
  7. I
  8. I
  9. |I-I'|
  10. ∣I−I′∣,则所得差值最小的模版即为识别结果。其中
  11. I
  12. I
  13. I 为分割后的字符图像,
  14. I
  15. I’
  16. I’ 为模版图像。由于 **opencv-python** 中图像是以 **numpy** 数组形式存储的,所以
  17. I
  18. I
  19. |I-I'|
  20. II′∣ 相当于直接将两矩阵相减取绝对值即可,取绝对值的函数为
  1. numpy.abs()

五、实验内容及代码

5.1 实验数据

本次实验将使用到一张待车牌识别的图像以及一个车牌模板文件,待进行车牌识别的图像如 图1 所示,车牌模板文件夹如 图2 所示:

在这里插入图片描述

图1. 待进行车牌识别的图像

在这里插入图片描述

图2(a). 车牌模板文件夹中的汉字模板

在这里插入图片描述

图2(b). 车牌模板文件夹中的数字模板

在这里插入图片描述

图2(c). 车牌模板文件夹中的英文字母模板

具体的数据我已打包分享至如下百度网盘链接:5-carNumber_免费高速下载|百度网盘-分享无限制 (baidu.com)

5.2 实验代码

  1. import os
  2. import cv2
  3. import numpy as np
  4. img = cv2.imread(r'./data/5.jpg')# 最终用于识别的图像# 1.车牌定位deflicense_region(image):
  5. r = image[:,:,2]
  6. g = image[:,:,1]
  7. b = image[:,:,0]# 求出三种阈值
  8. license_region_thresh = np.zeros(np.append(3, r.shape))# 创建一个空的三维数组用于存放三种阈值
  9. license_region_thresh[0,:,:]= r/b
  10. license_region_thresh[1,:,:]= g/b
  11. license_region_thresh[2,:,:]= b
  12. # 存放满足阈值条件的像素点坐标
  13. region_origin =[]for i inrange(image.shape[0]):for j inrange(image.shape[1]):if(license_region_thresh[0, i, j]<0.35and
  14. license_region_thresh[1, i, j]<0.9and
  15. license_region_thresh[2, i, j]>90)or(
  16. license_region_thresh[1, i, j]<0.35and
  17. license_region_thresh[0, i, j]<0.9and
  18. license_region_thresh[2, i, j]<90):
  19. region_origin.append([i, j])
  20. region_origin = np.array(region_origin)# 进一步缩小行的索引范围
  21. row_index = np.unique(region_origin[:,0])
  22. row_index_number = np.zeros(row_index.shape, dtype=np.uint8)for i inrange(region_origin.shape[0]):for j inrange(row_index.shape[0]):if region_origin[i,0]== row_index[j]:
  23. row_index_number[j]= row_index_number[j]+1
  24. row_index_out = row_index_number >10# 将误判的点去除
  25. row_index_out = row_index[row_index_out]# 进一步缩小列的索引范围
  26. col_index = np.unique(region_origin[:,1])
  27. col_index_number = np.zeros(col_index.shape, dtype=np.uint8)for i inrange(region_origin.shape[0]):for j inrange(col_index.shape[0]):if region_origin[i,1]== col_index[j]:
  28. col_index_number[j]= col_index_number[j]+1
  29. col_index_out = col_index_number >10
  30. col_index_out = col_index[col_index_out]# 得出最后的区间
  31. region_out = np.array([[np.min(row_index_out), np.max(row_index_out)],[np.min(col_index_out), np.max(col_index_out)]])return region_out
  32. region = license_region(img)# 显示车牌区域
  33. img_test = img.copy()# 拷贝时不能直接等号赋值
  34. cv2.rectangle(img_test, pt1=(region[1,0], region[0,0]), pt2=(region[1,1], region[0,1]),
  35. color=(0,0,255), thickness=2)
  36. cv2.imshow('car_license_region', img_test)
  37. cv2.waitKey(0)
  38. cv2.destroyAllWindows()# 2.分割区域灰度化、二值化
  39. img_car_license = img[region[0,0]:region[0,1], region[1,0]:region[1,1],:]
  40. img_car_license_gray = cv2.cvtColor(img_car_license, cv2.COLOR_BGR2GRAY)# RGB图像转化为灰度图像# otus二值化
  41. img_car_license_binary = cv2.threshold(img_car_license_gray,0,255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]# 3.车牌分割(均分割为25*15的图片)height=25,width=15# 模板分割函数,只针对单个字符,用于去除其周围的边缘,并resizedeftemplate_segmentation(origin_img):# 提取字符各列满足条件(有两个255的单元格)的索引
  42. col_index =[]for col inrange(origin_img.shape[1]):# 对于图像的所有列if np.sum(origin_img[:, col])>=2*255:
  43. col_index.append(col)
  44. col_index = np.array(col_index)# 提取字符各行满足条件(有两个255的单元格)的索引
  45. row_index =[]for row inrange(origin_img.shape[0]):if np.sum(origin_img[row,:])>=2*255:
  46. row_index.append(row)
  47. row_index = np.array(row_index)# 按索引提取字符(符合条件的行列中取min-max),并resize25*15大小
  48. output_img = origin_img[np.min(row_index):np.max(row_index)+1, np.min(col_index):np.max(col_index)+1]
  49. output_img = np.uint8(output_img)if col_index.shape[0]<=3or row_index.shape[0]<=3:
  50. output_img = origin_img[np.min(row_index):np.max(row_index)+1, np.min(col_index):np.max(col_index)+1]
  51. pad_row1 = np.int8(np.floor((25- output_img.shape[0])/2))
  52. pad_row2 = np.int8(np.ceil((25- output_img.shape[0])/2))
  53. pad_col1 = np.int8(np.floor((15- output_img.shape[1])/2))
  54. pad_col2 = np.int8(np.ceil((15- output_img.shape[1])/2))
  55. output_img = np.pad(output_img,((pad_row1, pad_row2),(pad_col1, pad_col2)),'constant',
  56. constant_values=(0,0))
  57. output_img = np.uint8(output_img)else:
  58. output_img = cv2.resize(output_img,(15,25), interpolation=0)return output_img
  59. # 对原始车牌抠图,抠出每一个字符
  60. temp_col_index =[]for col inrange(img_car_license_binary.shape[1]):if np.sum(img_car_license_binary[:, col])>=2*255:# 提取大于等于2255的列
  61. temp_col_index.append(col)
  62. temp_col_index = np.array(temp_col_index)
  63. flag =0# 值是7个字符的起始列
  64. flag_i =0# 值的变化范围:从0到6(对应车牌的7个字符)
  65. car_license_out_col = np.uint8(np.zeros([7,30]))# 7行的数组存储车牌上的7个需识别的字for j inrange(temp_col_index.shape[0]-1):if temp_col_index[j+1]-temp_col_index[j]>=2:# 提取的>=2255的列之间不是相邻的(可初步解决川的分割问题)
  66. temp = temp_col_index[flag:j+1]
  67. temp = np.append(temp, np.zeros(30-temp.shape[0]))# 补成30维的向量,方便最后赋值给car_license_out_col
  68. temp = np.uint8(temp.reshape(1,30))
  69. car_license_out_col[flag_i,:]= temp
  70. flag = j+1
  71. flag_i = flag_i+1
  72. temp = temp_col_index[flag:]
  73. temp = np.append(temp, np.zeros(30-temp.shape[0]))
  74. temp = np.uint8(temp.reshape(1,30))
  75. car_license_out_col[flag_i,:]= temp
  76. # 分别提取7个字符
  77. car_license_out_row = np.uint8(np.zeros([7,30]))for row inrange(car_license_out_row.shape[0]):# car_license_out_row.shape[0]
  78. temp = car_license_out_col[row,:]
  79. index =0for i inrange(temp.shape[0]):# 去除列索引中多余的0if temp[i]==0:
  80. index = i
  81. break
  82. col_temp = temp[0:index]
  83. temp_img = img_car_license_binary[:, np.min(col_temp):np.max(col_temp)+1]
  84. t = np.nonzero(np.sum(temp_img, axis=1))if row ==0:
  85. province1 = temp_img[t,:]# 汉字后续扩展成40*40
  86. province1 = province1[0,:,:]
  87. province1 = template_segmentation(province1)
  88. province1 = np.uint8(province1)if row ==1:
  89. province2 = temp_img[t,:]# 字母和数字后续扩展成40*40
  90. province2 = province2[0,:,:]
  91. province2 = template_segmentation(province2)
  92. province2 = np.uint8(province2)if row ==2:
  93. car_number1 = temp_img[t,:]
  94. car_number1 = car_number1[0,:,:]
  95. car_number1 = template_segmentation(car_number1)
  96. car_number1 = np.uint8(car_number1)if row ==3:
  97. car_number2 = temp_img[t,:]
  98. car_number2 = car_number2[0,:,:]
  99. car_number2 = template_segmentation(car_number2)
  100. car_number2 = np.uint8(car_number2)if row ==4:
  101. car_number3 = temp_img[t,:]
  102. car_number3 = car_number3[0,:,:]
  103. car_number3 = template_segmentation(car_number3)
  104. car_number3 = np.uint8(car_number3)if row ==5:
  105. car_number4 = temp_img[t,:]
  106. car_number4 = car_number4[0,:,:]
  107. car_number4 = template_segmentation(car_number4)
  108. car_number4 = np.uint8(car_number4)if row ==6:
  109. car_number5 = temp_img[t,:]
  110. car_number5 = car_number5[0,:,:]
  111. car_number5 = template_segmentation(car_number5)
  112. car_number5 = np.uint8(car_number5)
  113. cv2.imshow('province1', province1)
  114. cv2.imshow('province2', province2)
  115. cv2.imshow('car_number1', car_number1)
  116. cv2.imshow('car_number2', car_number2)
  117. cv2.imshow('car_number3', car_number3)
  118. cv2.imshow('car_number4', car_number4)
  119. cv2.imshow('car_number5', car_number5)
  120. cv2.waitKey(0)
  121. cv2.destroyAllWindows()# 4.车牌识别# 读取原始图片并生成模板的函数deftemplate_array_generator(template_path, template_size):
  122. template_img_out = np.zeros([template_size,25,15], dtype=np.uint8)
  123. index =0
  124. files = os.listdir(template_path)forfilein files:
  125. template_img = cv2.imdecode(np.fromfile(template_path +'/'+file, dtype=np.uint8),-1)
  126. template_img_gray = cv2.cvtColor(template_img, cv2.COLOR_BGR2GRAY)
  127. template_img_binary = cv2.threshold(template_img_gray,0,255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
  128. template_img_binary =255-template_img_binary # 模板给出的与车牌上的是相反的,所有用255相减进行匹配
  129. template_img_out[index,:,:]= template_segmentation(template_img_binary)
  130. index = index +1return template_img_out
  131. # 读取所有的汉字并生成模板
  132. Chinese_character =open(r'./data/5-carNumber./汉字.txt', encoding="gbk").read()
  133. Chinese_character = Chinese_character.split("\n")
  134. Chinese_char_template = template_array_generator(r'./data/5-carNumber./汉字',len(Chinese_character))# 读取所有的数字并生成模板
  135. Number_character =open(r'./data/5-carNumber./数字.txt', encoding="gbk").read()
  136. Number_character = Number_character.split("\n")
  137. Number_char_template = template_array_generator(r'./data/5-carNumber./数字',len(Number_character))# 读取所有的字母并生成模板
  138. Alphabet_character =open(r'./data/5-carNumber./英文.txt', encoding="gbk").read()
  139. Alphabet_character = Alphabet_character.split("\n")
  140. Alphabet_char_template = template_array_generator(r'./data/5-carNumber./英文',len(Alphabet_character))# 进行字符识别
  141. car_character = np.uint8(np.zeros([7,25,15]))
  142. car_character[0,:,:]= province1.copy()
  143. car_character[1,:,:]= province2.copy()
  144. car_character[2,:,:]= car_number1.copy()
  145. car_character[3,:,:]= car_number2.copy()
  146. car_character[4,:,:]= car_number3.copy()
  147. car_character[5,:,:]= car_number4.copy()
  148. car_character[6,:,:]= car_number5.copy()
  149. match_length = Chinese_char_template.shape[0]+Alphabet_char_template.shape[0]+Number_char_template.shape[0]
  150. match_mark = np.zeros([7, match_length])
  151. Chinese_char_start =0
  152. Chinese_char_end = Chinese_char_template.shape[0]
  153. Alphabet_char_start = Chinese_char_template.shape[0]
  154. Alphabet_char_end = Chinese_char_template.shape[0]+Alphabet_char_template.shape[0]
  155. Number_char_start = Chinese_char_template.shape[0]+Alphabet_char_template.shape[0]
  156. Number_char_end = match_length
  157. for i inrange(match_mark.shape[0]):# 7个需识别的字符for j inrange(Chinese_char_start, Chinese_char_end):# 所有的汉字模板
  158. match_mark[i, j]= cv2.matchTemplate(car_character[i,:,:], Chinese_char_template[j,:,:], cv2.TM_CCOEFF)# 所有的字母模板for j inrange(Alphabet_char_start, Alphabet_char_end):
  159. match_mark[i, j]= cv2.matchTemplate(car_character[i,:,:],
  160. Alphabet_char_template[j-Alphabet_char_start,:,:],
  161. cv2.TM_CCOEFF)# 所有的数字模板for j inrange(Number_char_start, Number_char_end):
  162. match_mark[i, j]= cv2.matchTemplate(car_character[i,:,:],
  163. Number_char_template[j-Number_char_start,:,:],
  164. cv2.TM_CCOEFF)
  165. output_index = np.argmax(match_mark, axis=1)
  166. output_char =[]for i inrange(output_index.shape[0]):if0<= output_index[i]<=28:
  167. output_char.append(Chinese_character[output_index[i]])if29<= output_index[i]<=54:
  168. output_char.append(Alphabet_character[output_index[i]-29])if55<= output_index[i]<=64:
  169. output_char.append(Number_character[output_index[i]-55])# 打印识别结果for i inrange(len(output_char)):if i ==0:print('province1:'+output_char[0])if i ==1:print('province1:'+output_char[1])if i ==2:print('car1:'+output_char[2])if i ==3:print('car2:'+ output_char[3])if i ==4:print('car3:'+ output_char[4])if i ==5:print('car4:'+ output_char[5])if i ==6:print('car5:'+ output_char[6])

车牌区域定位结果:

在这里插入图片描述

图3. 车牌区域定位结果

各车牌字符分割结果:

在这里插入图片描述

图4(a). 省份字符分割

在这里插入图片描述

图4(b). 字符1分割

在这里插入图片描述

图4(c). 字符2分割

在这里插入图片描述

图4(d). 字符3分割

在这里插入图片描述

图4(e). 字符4分割

在这里插入图片描述

图4(f). 字符5分割

在这里插入图片描述

图4(g). 字符6分割

车牌识别结果:

在这里插入图片描述

图5. 模板匹配下的车牌识别结果


本文转载自: https://blog.csdn.net/alan1ly/article/details/125423118
版权归原作者 糖糖不是堂 所有, 如有侵权,请联系我们删除。

“数字图像处理——实验五 基于图像分割的车牌定位识别”的评论:

还没有评论