Halcon 学习笔记--仿射变换与车牌定位(6)
Halcon仿射方式:
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)其中Row, Column, Phi是所选择区域中心坐标以及相对于水平方向夹角,rad(180)为要旋转的角度这个角度是任意值
hom_mat2d_identity( : : : HomMat2DIdentity)形成单位矩阵:
hom_mat2d_rotate( : : HomMat2D, Phi, Px, Py : HomMat2DRotate)
hom_mat2d_scale( : : HomMat2D, Sx, Sy, Px, Py : HomMat2DScale)
hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate)
二、车牌识别
dev_close_window() read_image (Image, 'E:/欣奕华/项目/Halcon/STUDY/网络课程笔记/3.仿射变换/车牌1.jpg') get_image_size (Image, Width, Height) dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_display (Image) draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2) gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2) area_center (Rectangle, Area, Row1, Column1) orientation_region (Rectangle, Phi1) vector_angle_to_rigid (Row1, Column1, Phi1, Row1, Column1, 3.14, HomMat2D) affine_trans_region (Rectangle, RegionAffineTrans1, HomMat2D, 'nearest_neighbor') affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false') reduce_domain (ImageAffinTrans, RegionAffineTrans1, ImageReduced) rgb1_to_gray (ImageReduced, GrayImage) dev_display (GrayImage) threshold (GrayImage, Regions, 0, 66) connection (Regions, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, ['area','width','height'], 'and', [1701.88,0,0], [5000,53.05,114.71]) sort_region (SelectedRegions, SortedRegions, 'character', 'true', 'column') area_center (SortedRegions, Area1, Row2, Column2) read_ocr_class_mlp ( 'Industrial_0-9A-Z_NoRej.omc', OCRHandle) do_ocr_multi_class_mlp (SortedRegions, GrayImage, OCRHandle, Class, Confidence) for I := 0 to 5 by 1 disp_message (WindowHandle, Class[I], 'image', Row2[I], Column2[I], 'black', 'true') endfor
上述代码是自己写的,下面是网络课程,主要看他的注释:
*1采集图像 read_image (Image, 'E:/欣奕华/项目/Halcon/STUDY/Lesson ten_OCR/1.jpg') dev_close_window () dev_open_window (0, 0, 512, 512, 'black', WindowHandle) dev_display (Image) *2预处理之车牌定位,一般定位有两种,一个是blob像素团块定位,一个是模板匹配定位,然后几何变换转正 decompose3 (Image, Red, Green, Blue) trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv') *注意这里的颜色通道转换是为了方便图像分割,也就是车牌定位,这里用的比较通用简单的blob,在实际项目中需要考虑光照等的影响进行微调优化 *这里的二值化是进行一个blob车牌定位 threshold (Saturation, Regions, 182, 255) opening_rectangle1 (Regions, RegionOpening, 6, 6) shape_trans (RegionOpening, RegionTrans, 'rectangle2') *接下来求这个区域的角度和中心点,便于仿射变换转正 orientation_region (RegionTrans, Phi) area_center (RegionTrans, Area, Row, Column) *开始求解仿射变换之旋转矩阵,这里要注意是转到180度还是0度,需要注意你求解角度时的极轴方向,具体可以看链接视频 vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D) *将图像和区域都做这个旋转变换,然后抠图,再进行图像分割 affine_trans_image (Image, ImageAffinTrans, HomMat2D, 'constant', 'false') affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor') reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced) rgb1_to_gray (ImageReduced, GrayImage) invert_image (GrayImage, ImageInvert) threshold (GrayImage, Regions2, 92,135) opening_rectangle1 (Regions2, RegionOpening1, 3, 3) connection (RegionOpening1, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 568.08, 1372.46) *字符就被提取了,注意这里我暂时不是识别汉字,要识别汉字也是可以的,可以看视频链接 *进行字符排序方便识别后观察,因为人都是习惯从左到右 sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column') *4识别显示,注意这里识别用的halcon自带字库,同时带NoRej的表示非拒绝,识别要求不严格 read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle) *注意ocr套路,一般可以多个区域一起识别,也可以单个区域识别,注意上面的工作就是为了得到这个区域 do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence) area_center (SortedRegions, Area1, Row1, Column1) for index := 0 to 5 by 1 disp_message (WindowHandle, Class[index], 'window', Row1[0], Column1[index], 'black', 'true') endfor