处理流程
1、识别字符水平旋转角度
2、图片仿射变换
3、二值化 连通域 筛选噪音区域
4、切割字符区域
5、获取字符矩形框
6、隔断字符矩形框
7、筛序过滤字符连通域
8、字符识别与显示
完整代码
* dotprt.hdev: Segmentation of a dot printing
*
**** 1、加载并显示图片*****************
dev_update_window ('off')
read_image (Needle, 'needle1')
dev_close_window ()
get_image_size (Needle, Width, Height)
dev_open_window (0, 0, 2 * Width, 2 * Height, 'black', WindowID)
dev_display (Needle)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
**** 2、识别文字角度*****************
* 设置文字处理区域
* *
Row1 := 50
Column1 := 90
Row2 := 250
Column2 := 370
gen_rectangle1 (Rectangle1, Row1, Column1, Row2, Column2)
* 计算文字角度 35:文字的高度
* -0.523599, 0.523599 搜索起始与结束角度
text_line_orientation (Needle, Needle, 35, -0.523599, 0.523599, OrientationAngle)
**** 3、仿射变换 调整图片角度*****************
* 计算矩形中心点
Px := Column1 + (Column2 - Column1) / 2
Py := Row1 + (Row2 - Row1) / 2
* 计算仿射变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, Px, Py, HomMat2DRotate)
* 使用变换矩阵 对图片执行仿射变换
affine_trans_image (Needle, Rotated, HomMat2DRotate, 'constant', 'false')
* 显示旋转后的图片
dev_display (Rotated)
disp_continue_message (WindowID, 'black', 'true')
stop ()
**** 4、二值化连通域分析 筛选其它噪音*****************
* 二值图
threshold (Rotated, RawSegmentation, 0, 105)
* 连通域分析
connection (RawSegmentation, ConnectedRegions)
* 面积筛选
select_shape (ConnectedRegions, MinSizeRegions, 'area', 'and', 6, 99999)
* 合并筛选后的连通域
union1 (MinSizeRegions, RemovedNoise)
* 显示
dev_display (Rotated)
dev_set_color ('green')
dev_set_draw ('fill')
dev_display (RemovedNoise)
disp_continue_message (WindowID, 'black', 'true')
stop ()
**** 5、 在已经去除噪音连通域 区域切割字符*****************
* 在连通域(RemovedNoise) 切割(53, 75, 260, 356) 矩形区域内的 连通域
clip_region (RemovedNoise, RawSegmentation, 53, 75, 260, 356)
* 显示
dev_display (Rotated)
dev_display (RawSegmentation)
disp_continue_message (WindowID, 'black', 'true')
stop ()
**** 6、 获取字符区域矩形框*****************
* 闭运算 链接字符内的点
closing_circle (RawSegmentation, ClosedPatterns, 6)
* 开运算 断开字符与字符
opening_rectangle1 (ClosedPatterns, SplitPatterns, 1, 5)
* 连通域分析
connection (SplitPatterns, ConnPatterns)
* 过滤非字符 面积过滤
select_shape (ConnPatterns, CharCandidates, 'area', 'and', 150, 5999)
* 外接正矩形
shape_trans (CharCandidates, CharBlocks, 'rectangle1')
* 显示
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display (Rotated)
dev_display (CharBlocks)
disp_continue_message (WindowID, 'black', 'true')
stop ()
**** 7、 按照字符大小隔断 字符区域矩形框*****************
* 隔断链接在一起的字符区域 宽度为25的分割矩形框
partition_rectangle (CharBlocks, CharCandidates, 25, 100)
* 矩形框分割字符连通域(分割矩形框与字符连通域 交集区域 )
intersection (CharCandidates, RawSegmentation, Characters)
* 闭运算
closing_circle (Characters, IntermedCharacters, 2.5)
* 显示
dev_set_colored (12)
dev_display (Rotated)
dev_set_draw ('fill')
dev_display (IntermedCharacters)
disp_continue_message (WindowID, 'black', 'true')
stop ()
**** 8、筛选过滤字符区域 *****************
* 创建字符连通域
gen_empty_obj (Characters)
* 统计字符个数
count_obj (IntermedCharacters, NumIntermediate)
* 筛选面积过小的连通域
dev_display (Rotated)
for i := 1 to NumIntermediate by 1
dev_set_color ('red')
* 选择字符区域
select_obj (IntermedCharacters, Char, i)
dev_display (Char)
* 连通域分析
connection (Char, CharParts)
* 面积过滤
select_shape (CharParts, CharCandidates, 'area', 'and', 40, 99999)
* 合并
union1 (CharCandidates, Char)
dev_set_color ('green')
dev_display (Char)
* 连通域合并到 字符数组中
concat_obj (Characters, Char, Characters)
endfor
* 形状过滤 高度过滤 筛选掉+号
select_shape (Characters, Heigh, 'height', 'and', 24, 50)
* 字符区域排序
sort_region (Heigh, FinalCharacters, 'character', 'true', 'row')
* 显示
dev_set_color ('red')
dev_set_draw ('margin')
dev_display (Rotated)
dev_display (FinalCharacters)
dev_set_color ('green')
dev_set_line_width (3)
dev_set_shape ('rectangle1')
dev_display (FinalCharacters)
dev_set_shape ('original')
dev_set_line_width (1)
dev_set_draw ('fill')
**** 9、字符识别 *****************
* 加载点阵打印字符集
read_ocr_class_mlp ('./DotPrint_NoRej.omc', OCRHandle)
* 执行字符识别
do_ocr_multi_class_mlp (FinalCharacters, Rotated, OCRHandle, Class, Confidence)
* 字符区域外接矩形
smallest_rectangle1 (FinalCharacters, Row11, Column1, Row2, Column21)
* 显示识别结果
disp_message (WindowID, Class, 'image', Row2, Column1, 'green', 'false')
dev_update_window ('on')