OCR之text_line_slant.hdev

*关闭更新
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

posted @ 2020-08-16 22:34  懒树懒  阅读(389)  评论(0)    收藏  举报