一、描述
检测下列图像中硬刷数字是否完好。
二、方法
通过处理一个完好的印刷图像,将其中的字体当做模版,通过模版去比较其他图片是否完好(通过差值的面积大小来判定)。
模版图片如下:
运行结果
三、Halcon代码
*读入标准模板图片(图片名字为standard.bmp)
read_image (Image, './standard.bmp')
*设置图像窗口
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
*选中模板区域(ROI)
gen_rectangle1 (ROI_0, 201, 244, 385, 526)
reduce_domain (Image, ROI_0, ImageReduced)
*选中模板区域数字
threshold (ImageReduced, Regions, 106, 255)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 163.12, 10000)
union1 (SelectedRegions, RegionUnion)
*求模板区域坐标
area_center (RegionUnion, Areastandard, Rowstandard, Columnstandard)
*扣取模板区域图像,创建模板需要的是图像,不是区域
reduce_domain (ImageReduced, RegionUnion, ImageReduced1)
*创建模板
create_shape_model (ImageReduced1, 'auto', rad(0), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
*读入图像路径,循环遍历每个图像
list_files ('./', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*模板匹配
find_shape_model (Image, ModelID, 0, rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
*判断匹配是否成功
if (|Score| > 0)
*搜寻当前图像匹配成功的区域并求它的坐标和角度
dev_display_shape_matching_results (ModelID, 'red', Row, Column, Angle, 1, 1, 0)
*将模板区域仿射变换到匹配成功的区域
vector_angle_to_rigid (Rowstandard, Columnstandard, 0, Row, Column, Angle, HomMat2D)
affine_trans_region (RegionUnion, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
dev_display (Image)
*blob方法提取当前图像的数字,并变成一个联通域
threshold (Image, Regions1, 106, 255)
connection (Regions1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 163.12, 10000)
union1 (SelectedRegions1, RegionUnion1)
*仿射变换后的模板区域与当前区域求差集,即缺陷部分
difference (RegionAffineTrans, RegionUnion1, RegionDifference)
*开运算去除很小点的噪声
opening_circle (RegionDifference, RegionOpening, 3)
*求缺陷的面积
area_center (RegionOpening, Area, Row1, Column1)
*缺陷面积大于阈值,说明有缺陷
if(Area>100)
*设置缺陷区域的颜色
dev_set_color ('blue')
*显示缺陷区域
dev_display (RegionOpening)
*显示NG
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle,'NG', 'window', 15, 40, 'yellow', 'false')
*缺陷面积小于阈值,说明没缺陷
else
*显示OK
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle,'OK', 'window', 15, 40, 'green', 'false')
endif
*没有匹配到模板,直接NG
else
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle,'NG', 'window', 15, 40, 'yellow', 'false')
endif
endfor
完整项目代码下载地址:Halcon检测硬刷字体缺陷项目资源-CSDN文库
四、主要算子介绍
1.create_shape_model(Template : : NumLevels, AngleStart, AngleExtent, AngleStep, Optimization, Metric, Contrast, MinContrast : ModelID)
create_shape_model (ImageReduced1, 'auto', rad(0), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
创建模版
Halcon算子—create_shape_model参数解析及优化-CSDN博客
2.find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels, Greediness : Row, Column, Angle, Score)
find_shape_model (Image, ModelID, 0, rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
匹配模版