Halcon study notes OCR series-license plate recognition

The first step in license plate recognition is to find a car on the side of the road and take a picture of his license plate number as a picture for image processing.
To protect privacy, only the back part is exposed.

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210107152909995.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDUwNjMwNQ==,size_16,color_FFFFFF,t_70)

The following is the image processing process:

read_image (Image, 'C:/Users/Administrator/Desktop/dsBuffer.bmp.png')
*将彩色图片通过三通道分成三张图片,筛选对比效果最好的图片
*当然在现实应用中能用灰度相机拍的话没有必要用彩色相机,一般用到彩色相机了都是需要检测颜色相关的内容
decompose3 (Image, R, G, B)
*或者直接转成灰度图也可以,对于彩色图片两种都试一下,主要看效果
rgb1_to_gray (Image, GrayImage)

The corresponding image of the R channel:
Insert picture description here
the effect of directly converting to a grayscale image:
Insert picture description here
obviously, the image of the R channel has a higher contrast, so we next select the image of the R channel for OCR extraction and recognition.

*对比效果R通道的图像对比效果最好,接下来就是用R通道的图像提取识别OCR
dev_display (R)
*接下来就是抠图,可以直接框选ROI区域,也可以通过预处理得到OCR区域,下面我就是用预处理的方法来获取
fast_threshold (R, Region, 150, 255, 30)
connection (Region, ConnectedRegions)
*通过层层条件限制选出我们想要的OCR区域
select_shape (ConnectedRegions, SelectedRegions, ['width','area','height'], 'and', [68.43,2826.65,137.45], [200.01,10000,500])
union1 (SelectedRegions, RegionUnion)
shape_trans (RegionUnion, RegionTrans, 'rectangle2')
area_center (RegionTrans, Area, Row, Column)
orientation_region (RegionTrans, Phi)
*求出区域的方向后,需要根据Phi的绝对值是靠近0还是3.14,来决定Angle2这个值是填rad(0)还是rad(180)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
affine_trans_image (R, ImageAffinTrans, HomMat2D, 'constant', 'false')
reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced)
*由于halcon支持的ocr识别是需要将字体转正,不能偏斜,而且要满足白底黑字的情况下才能识别,所以我们需要将图像反转一下
invert_image (ImageReduced, ImageInvert)
threshold (ImageInvert, Region1, 0, 90)
connection (Region1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 4340.23, 10000)
*得到OCR区域后,开始读取ocr,由于这张图的拍摄角度有点问题,所以4和R字符明显还是处于斜着的状态
*但是读取的结果还行,只有4读取错误,其他的都是正确的,当然我们也可以把每个字都分割出来单独矫正
*位置,一个for循环的事情,可以自己私下去试试,主要这个就是一般的OCR识别的步骤。
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
do_ocr_multi_class_mlp (SelectedRegions1, ImageInvert, OCRHandle, Class, Confidence)

Cut out and inverted images:
Insert picture description here
Insert picture description here

The following are some trivial things that display the reading result in the image window, so I don’t bother to write it, and the processed result shows:
Insert picture description here
Mainly, for this font is not sticky, it is very clear, the general OCR recognition method is roughly That's it, then when we encounter some fonts that are stuck in extraction, or some fonts with sprayed typefaces, we need special processing methods, and I will continue to write them below. If you encounter a font that is not recognized by the font library in halcon, then you have to train yourself.

Guess you like

Origin blog.csdn.net/weixin_44506305/article/details/112313927