Halcon study notes OCR series-inkjet font recognition

This article mainly introduces the OCR recognition of inkjet fonts encountered in the project I did.
The original picture is as follows:
Insert picture description here

read_image (Image, 'C:/Users/Administrator/Desktop/喷涂.png')
get_image_size (Image, Width, Height)
rgb1_to_gray (Image, GrayImage)
*首先看到图片,字体部分并没有很清晰的显示,所以需要通过一些预处理的手法提取OCR
scale_image (GrayImage, ImageScaled, 2.83333, -85)
*下面是一些常用的预处理算子,我一一试了下,效果的话都不太行,基于scale缩放后的图像足够我处理了
* gray_range_rect (ImageScaled, ImageResult, 3, 3)
* emphasize (ImageScaled, ImageEmphasize, Width, Height, 1)
*现在得到想要的图像,现在就是将需要读取OCR的区域提取出来
threshold (ImageScaled, Regions, 0, 80)
dilation_circle (Regions, RegionDilation, 3.5)
connection (RegionDilation, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ['height','area','width'], 'and', [24.26,0,0], [49.48,9476.07,76.65])
union1 (SelectedRegions, RegionUnion)

shape_trans (RegionUnion, RegionTrans, 'rectangle2')
area_center (RegionTrans, Area, Row, Column)
orientation_region (RegionTrans, Phi)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(180), HomMat2D)
affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
affine_trans_image (ImageScaled, ImageAffinTrans, HomMat2D, 'constant', 'false')
*上面是得到矫正后的图像
reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced)
threshold (ImageReduced, Regions1, 0, 50)
*这一步膨胀是为了将字的每一个点都连成一个整体,防止筛选特征的时候被剔除掉
dilation_rectangle1 (Regions1, RegionDilation1, 3, 7)
connection (RegionDilation1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['height','area'], 'and', [17.238,150.94], [50,4983.83])
union1 (SelectedRegions1, RegionUnion1)
*针对喷涂的字体来说这一步就是最关键的,先将字体部分提取出来,合并成一个整体去除不必要的噪声,
*通过求交集算子,求出阈值分割后的区域与筛选出来的区域之间的公共部分,这样就可以将每个字
*合成一个整体,而不是一个点一个点的。
partition_rectangle (RegionUnion1, Partitioned, 18, 30)
intersection (Partitioned, Regions1, RegionIntersection)
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
count_obj (SortedRegions, Number)
read_ocr_class_mlp ('DotPrint_0-9A-Z.omc', OCRHandle)
for Index := 1 to Number by 1
    select_obj (SortedRegions, ObjectSelected, Index)
    do_ocr_single_class_mlp (ObjectSelected, ImageAffinTrans, OCRHandle, 1, Class, Confidence)
    set_tposition (3600, 10, 30+10*Index)
    write_string (3600, Class)
endfor

The partition_rectangle operator is mainly to set a fixed value of length and width to separate each area, generally to separate two characters that are glued together. The picture after cutting:
Insert picture description here
intersection The picture after finding the intersection: the
Insert picture description here
final display result:
Insert picture description here

This case has embodied the ideas and methods of dealing with the characters that are stuck together and the character recognition of spraying. The personal habit of writing codes is relatively redundant, and readers can try to simplify them. In the next article, we will come to some special preprocessing methods to realize character extraction of some special products.

Guess you like

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