# Halcon学习笔记（五）几何定位+仿射+车牌识别

• Blob分析
• 模板匹配

### 仿射

#### 使用单位矩阵求仿射矩阵

• 单位矩阵
hom_mat2d_identity (HomMat2DIdentity)
• 平移矩阵
hom_mat2d_translate()
• 旋转矩阵
hom_mat2d_rotate(HomMat2DIdentity, -0.3, 256, 256, HomMat2DRotate)
输入是原矩阵，输出是旋转矩阵，旋转角度是顺时针0.3度，旋转中心点是(256,256)
• 缩放矩阵
hom_mat2d_scale (HomMat2DRotate, 1.5, 0.5, 256, 256, HomMat2DScale)
缩放点是(256,256),(1.5,0.5)表示x轴上缩放1.5倍，y轴上缩放0.5倍
• 仿射操作

affine_trans_region (Region, egionAffineTrans, HomMat2DScale, 'nearest_neighbor')

affine_trans_image()

affine_trans_xld()
……

#### 使用模板匹配

access_channel (ImageOrig, Image1, 1)

threshold (Image1, Region, 90, 255)

shape_trans (Region, Blister, 'convex')

orientation_region (Blister, Phi)

area_center (Blister, Area1, Row, Column)

vector_angle_to_rigid (Row1, Column1, Phi, Row2, Column2, 0, HomMat2D)

affine_trans_image (ImageOrig, Image2, HomMat2D, 'constant', 'false')

### 车牌识别

1、借助助手快速识别

read_image (Image, 'F:/Code/picTest/CarNumber/3.jpg')

rgb1_to_gray (Image, GrayImage)

2、Blob分析+mlp分类器实现

invert_image (GrayImage, ImageInvert)

threshold (GrayImage, Regions1, 96, 204)

connection (Regions1, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['area','row'], 'and', [0,164.27], [2945.62,191.47])
select_shape (SelectedRegions, SelectedRegions1, ['area','ratio'], 'and', [0,0], [200,2.0846])
select_shape (SelectedRegions1, SelectedRegions2, ['area','ratio','column'], 'and', [0,1.5793,138.85], [200,2,500])



3、定位与区域分割

• 读取图片
read_image (Image, 'F:/Code/picTest/CarNumber/4.jpg')

• 分解RGB通道
decompose3 (Image, Red, Green, Blue)

• 转化为HSV三种类型的图片
trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')

• 对其中最明显的图片进行阈值分割
threshold (Saturation, Regions, 0, 90)

• 打散
connection (Regions, ConnectedRegions)


• 选择车牌的大致区域
select_shape (ConnectedRegions, SelectedRegions, ['area','row'], 'and', [4041.92,186], [11826.4,354.42])

• 填充孔洞
fill_up (SelectedRegions, RegionFillUp)

• 剔除矩形外多余元素（视情况而定）
opening_rectangle1 (RegionFillUp, RegionOpening, 20, 30)

• 剔除多余元素
opening_circle (RegionOpening4, RegionOpening4, 3.5)

• 将形状转换成矩形
shape_trans (RegionOpening4, 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, ImageAffineTrans, HomMat2D, 'constant', 'false')

• 仿射矩阵应用于区域
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')

• 从图片中截取该矩形区域
reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)

• 将截取的区域转化为灰度图片
rgb1_to_gray (ImageReduced, GrayImage)
……后续操作即前面提到的Blob分析得到车牌字符，最后分类器识别字符

### 显示

smallest_rectangle1 (RegionOpening2, Row1, Column1, Row2, Column2)
count_obj (RegionOpening2, Number)
for i := 1 to Number by 1
disp_message (WindowHandle, Class[i-1], 'image', Row2[i-1], Column1[i-1], 'green', 'false')
endfor