一、流程:
车牌识别的流程一般是:
1) 采集
2)预处理:
- 增强对比度:有scale_image(灰度变换)、emphasize、equ_histo_image(直方图均衡化)、灰度图进行形态学等方法
- 滤波降噪
3) 定位、矫正车牌
4)字符分割
5)字符提取
6) 字符识别
7)显示
一、实现:
dev_close_window ()
dev_open_window (0, 0, 680, 320, 'black', WindowHandle)
read_image (Image, 'C:/Users/Administrator/Desktop/车牌.jpg')
*1.预处理
rgb1_to_gray (Image, GrayImage1)
scale_image (GrayImage1, ImageScaled, 6.71053, -1456)
*decompose3 (Image, Red, Green, Blue)
*trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')
*2.基于颜色的blob分析,定位、矫正车牌
threshold (ImageScaled, Regions, 73, 255)
closing_circle (Regions, RegionOpening1,1)
connection (RegionOpening1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['row','height','area'], 'and', [140.31,28.622,131.63], [500,50,1000])
union1 (SelectedRegions, RegionUnion)
shape_trans (RegionUnion, RegionTrans, 'rectangle2')
*图像矫正
orientation_region (RegionTrans, Phi)
area_center (RegionTrans, Area, Row, Column)
vector_angle_to_rigid (Row, Column, Phi, Row, Column,0, HomMat2D)
affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false')
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced)
invert_image (ImageReduced, ImageInvert)
*3.字符分割
rgb1_to_gray (ImageReduced, GrayImage)
scale_image (GrayImage, ImageScaled1, 6.71053, -1456)
threshold (ImageScaled1, Regions1, 73, 255)
closing_circle (Regions1, RegionOpening2,1.5)
connection (RegionOpening2, ConnectedRegions1)
*4.字符提取
select_shape (ConnectedRegions1, SelectedRegions1, ['row','height','area'], 'and', [144.286,28.622,113.78], [200,50,500])
*'character':按照 字母排序 Column:按照列的方式
sort_region (SelectedRegions1, SortedRegions, 'character', 'true', 'column')
count_obj (SortedRegions, Number)
for i := 1 to Number by 1
select_obj (SortedRegions, ImageAffinTrans, i)
endfor
*4.1训练:形成trf文件,字符图像和字符相关联
words:=['陕','K','7','5','5','5','5']
TrainFile:='C:/Users/Administrator/Desktop/训练文件/word.trf'
for i := 1 to Number by 1
select_obj (SortedRegions, ObjectSelected, i)
append_ocr_trainf (ObjectSelected, ImageReduced, words[i-1], TrainFile)
endfor
FontFile:='C:/Users/Administrator/Desktop/训练文件/word.ocr'
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
NumHidden:=20
*创建神经网络分类器
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle1)
*训练分类器
trainf_ocr_class_mlp (OCRHandle1, TrainFile, 200, 1, 0.01, Error, ErrorLog)
*保存omc文件
write_ocr_class_mlp (OCRHandle1, FontFile)
*清除句柄
clear_ocr_class_mlp (OCRHandle1)
*TrainFile:
*5.字符识别
read_ocr_class_mlp ('C:/Users/Administrator/Desktop/训练文件/word.ocr', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, ImageReduced, OCRHandle, Class, Confidence)
dev_display (ImageAffinTrans)
*6.显示
smallest_rectangle1 (SortedRegions, Row11, Column1, Row2, Column21)
for i := 1 to Number by 1
disp_message (WindowHandle, Class[i - 1], 'image', Row2[i - 1], Column1[i - 1], 'blue', 'false')
endfor
注:字符识别时我用的是自己训练生成的.ocr文件,如果要用halcon自带的.ocm文件识别字符时,需要将do_ocr_multi_class_mlp算子中第二个输入图片里的字体背景转换成暗字体(用invert_image )
戳戳小手帮忙点个免费的赞和关注吧,嘿嘿。 |