1.对比了Halcon,Visionpro,VM等算法平台。发现OCV算法效果和输出结果都类似。都是提前训练OCV模型,运行时输出分数。
2.下面总结Halcon的OCV算法,并做优化。Halcon官方案例是针对单个字符区域做核验,只能输出单个字符的分数,如果字符很多,则需要创建N多的OCV模型,不适用工业项目。我稍做整合,利用一个模型,核验所有字符,并输出每一个字符的分数。
read_image (Image, ‘fonts/arial_a1’)
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width / 2, Height / 2 + 42, ‘black’, WindowHandle)
dev_set_part (-84, 0, Height - 1, Width - 1)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_display (Image)
dev_set_draw (‘margin’)
*创建空图像数组,用于OCV训练
gen_empty_obj (ImgArray)
*创建待训练的字符数组
OcvText:=[‘A’,‘B’,‘C’,‘D’,‘E’]
*创建OCV模型
create_ocv_proj (OcvText, OCVHandle)
dev_set_color (‘red’)
read_image (Image, ‘fonts/arial_a’ + 1)
binary_threshold (Image, Region, ‘max_separability’, ‘dark’, UsedThreshold)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 150, 99999)
sort_region (SelectedRegions, SortedRegions, ‘character’, ‘true’, ‘row’)
for I := 1 to 5 by 1
select_obj (SortedRegions, ObjectSelected, I)
shape_trans (ObjectSelected, RegionTrans, ‘rectangle1’)
dilation_rectangle1 (RegionTrans, RegionDilation, 15, 15)
reduce_domain (Image, RegionDilation, ImageReduced)
concat_obj (ImgArray, ImageReduced, ImgArray)
endfor
*训练OCV模型
traind_ocv_proj (ImgArray, OCVHandle, OcvText, ‘single’)
*
*运行测试训练好的OCV模型
gen_empty_obj (RegionArrayTest)
gen_empty_obj (ImgArrayTest)
for I := 1 to 9 by 1
gen_empty_obj (ImgArrayTest)
gen_empty_obj (RegionArrayTest)
read_image (Image, ‘fonts/arial_a’ + I)
binary_threshold (Image, Region, ‘max_separability’, ‘dark’, UsedThreshold)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 150, 99999)
sort_region (SelectedRegions, SortedRegions, ‘character’, ‘true’, ‘row’)
for J := 1 to 5 by 1
select_obj (SortedRegions, ObjectSelected, J)
shape_trans (ObjectSelected, RegionTrans, ‘rectangle1’)
dilation_rectangle1 (RegionTrans, RegionDilation, 15, 15)
reduce_domain (Image, RegionDilation, ImageReduced)
concat_obj (ImgArrayTest, ImageReduced, ImgArrayTest)
concat_obj (RegionArrayTest, RegionDilation, RegionArrayTest)
endfor
*字符核验
do_ocv_simple (ImgArrayTest, OCVHandle, OcvText, ‘true’, ‘true’, ‘true’, ‘true’, 5, Quality)
* Display quality
if (Quality > 0.9)
Color := 'green'
elseif (Quality > 0.7)
Color := 'yellow'
else
Color := 'red'
endif
dev_display (Image)
dev_set_color (Color)
dev_set_line_width (2)
dev_display (RegionArrayTest)
disp_message (WindowHandle, 'Check print quality of \'A\' (Image ' + I + ' of 9)', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Quality = ' + Quality$'.2f', 'image', 120, 12, 'black', Color)
if (I < 9)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor