隶属于:
C:\Users\Public\Documents\MVTec\HALCON-23.05-Progress\examples\hdevelop\OCR\Segmentation
此例,主要针对倾斜字体时候的字符角度text_line_slant获取和hom_mat2d_slant旋转,以及partition_dynamic字符分割。字符获取方式可借鉴。对原区域进行膨胀腐蚀并和原区域求交集,可有效获取字符区域。
读图
提取字符区域
可借鉴:可以让碎字符也连接起来
threshold (ImageRectified, Region, 0, 100)
erosion_circle (Region, RegionErosion, 1.5)
dilation_rectangle1 (RegionErosion, RegionDilation, 1, 20)
connection (RegionDilation, ConnectedRegions)
intersection (ConnectedRegions, Region, RegionIntersection)
获取外界矩形
partition_dynamic (RegionIntersection, Characters, 30, 20)
分割字符
获取字符角度
text_line_slant (Image, Image, 50, rad(-45), rad(45), SlantAngle)
矩阵
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_slant (HomMat2DIdentity, -SlantAngle, ‘x’, 0, 0, HomMat2DSlant)
仿射变换
affine_trans_image (Image, ImageRectified, HomMat2DSlant, ‘constant’, ‘true’)
角度转正
字符区域
partition_dynamic (RegionIntersection, Characters, 30, 20)
分割字符
获取外界矩形
dev_update_off ()
read_image (Image, 'dot_print_slanted')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width * 3, Height * 3, 'black', WindowHandle)
dev_set_draw ('margin')
dev_set_colored (12)
dev_set_line_width (3)
for J := 0 to 1 by 1
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
if (J == 1)
* Correct slant
text_line_slant (Image, Image, 50, rad(-45), rad(45), SlantAngle)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_slant (HomMat2DIdentity, -SlantAngle, 'x', 0, 0, HomMat2DSlant)
affine_trans_image (Image, ImageRectified, HomMat2DSlant, 'constant', 'true')
else
copy_obj (Image, ImageRectified, 1, 1)
endif
*分割字符
threshold (ImageRectified, Region, 0, 100)
erosion_circle (Region, RegionErosion, 1.5)
dilation_rectangle1 (RegionErosion, RegionDilation, 1, 20)
connection (RegionDilation, ConnectedRegions)
intersection (ConnectedRegions, Region, RegionIntersection)
partition_dynamic (RegionIntersection, Characters, 30, 20)
smallest_rectangle1 (Characters, Row1, Column1, Row2, Column2)
dev_clear_window ()
dev_display (ImageRectified)
gen_rectangle1 (CharRectangles, Row1, Column1, Row2, Column2)
dev_display (CharRectangles)
if (J == 1)
disp_message (WindowHandle, 'Segmentation with slant correction', 'window', 12, 12, 'forest green', 'true')
else
disp_message (WindowHandle, 'Segmentation without slant correction', 'window', 12, 12, 'red', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor