halcon倾斜字体矫正

演示用图(在画图工具上用斜体写的字体,倾斜度不是很大):

halcon代码:

*倾斜字体矫正read_image (Image, 'D:/Halcon-WorkSpaces/Test/Image/test3.png')*自动检索文本行的斜率,第三个参数为字体拉升长度 >=原字符长度text_line_slant (Image, Image, 140, -rad(45), rad(45), SlantAngle)*生成二维变换的齐次变换矩hom_mat2d_identity (HomMat2DIdentity)*向生成的二维变换的齐次变换矩阵中添加斜率hom_mat2d_slant (HomMat2DIdentity, -SlantAngle, 'x', 0, 0, HomMat2DSlant)*对图像应用任意仿射2D变换。affine_trans_image (Image, ImageAffinTrans, HomMat2DSlant, 'nearest_neighbor', 'true')dev_clear_window ()dev_display (ImageAffinTrans)

执行效果:

案例2,系统自带的工程

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

* Segment characters

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

(0)

相关推荐