## Halcon学习笔记之OCR系列-环形字符，斜体字

``````read_image (Image, 'C:/Users/Administrator/Desktop/3.bmp')
rgb1_to_gray (Image, GrayImage)
*差分高斯
diff_of_gauss (GrayImage, DiffOfGauss, 3, 1.6)
threshold (DiffOfGauss, Regions, 2, 12)
``````

``````read_image (Image, 'C:/Users/Administrator/Desktop/环形字符.png')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
emphasize (GrayImage, ImageEmphasize, Width, Height, 1)
threshold (ImageEmphasize, Regions, 0, 21)
connection (Regions, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
fill_up (SelectedRegions, RegionFillUp)
*分别两次膨胀
dilation_circle (RegionFillUp, RegionDilation, 20)
dilation_circle (RegionFillUp, RegionDilation1, 70)
*这里是求出两个膨胀区域的最小外接圆的圆心和半径，为之后的极坐标转换做准备
*求出两个膨胀区域的差异部分，就是求出一个字体圆环部分
difference (RegionDilation1, RegionDilation, RegionDifference)
reduce_domain (ImageEmphasize, RegionDifference, ImageReduced)
*这里就是极坐标转换的算子了，就是将环形部分拉直，方便读取OCR
mirror_image (PolarTransImage, ImageMirror, 'row')
gray_range_rect (ImageMirror, ImageResult, 7, 7)
binary_threshold (ImageResult, Region, 'max_separability', 'light', UsedThreshold)
connection (Region, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['height','area'], 'and', [54.14,912.03], [100,10000])
partition_rectangle (SelectedRegions1, Partitioned, 40, 70)
sort_region (Partitioned, SortedRegions, 'character', 'true', 'row')
invert_image (ImageResult, ImageInvert)
do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence)
dev_display (Image)
set_tposition (3600, 61, 63)
write_string (3600, Class)
``````

``````read_image (Image, 'C:/Users/Administrator/Desktop/斜体字练习.png')
dev_close_window()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
fast_threshold (Image, Region, 0, 128, 20)
*获取字体区域偏转的角度
text_line_slant (Region, Image, 45, -0.523599, 0.523599, SlantAngle)
*生成矩阵
hom_mat2d_identity (HomMat2DIdentity)
*这里是获取字体转正的矩阵，SlantAngle这个只是之前获取的字体斜了多少度，那么要转正的话就是纠正
*这个偏转角度，所以就要向相反方向偏正
hom_mat2d_slant (HomMat2DIdentity, -SlantAngle, 'x', 0, 0, HomMat2DSlant)
affine_trans_image (Image, ImageAffinTrans, HomMat2DSlant, 'nearest_neighbor', 'false')
fast_threshold (ImageAffinTrans, Region1, 0, 90, 20)
connection (Region1, ConnectedRegions1)

dilation_rectangle1 (Region1, RegionDilation, 2, 5)
connection (RegionDilation, ConnectedRegions)
partition_rectangle (ConnectedRegions, Partitioned, 32, 45)
intersection (Partitioned, ConnectedRegions1, RegionIntersection)
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
do_ocr_multi_class_mlp (SortedRegions, ImageAffinTrans, OCRHandle, Class, Confidence)

``````