一、前言
? ? 本篇主要总结使用halcon 做缺陷检测的一个常用思路,当一张图片拍摄多个待检测工件,常用方法是由预先建立好的模版图像,采用 形状匹配+查分方法抠下每个工件,逐个检测。
? ? 形状匹配比灰度值匹配要更为可靠
二、代码
??代码是对一张有10个茶杯的图片进行演示
取第一个茶杯ROI作为模版,提取边缘作为形状匹配对的模版
??
set_system ('clip_region', 'false') //设置剪切为false
read_image (Image, 'E:/halcon-study/test_shape_match/002.jpg')
rgb1_to_gray(Image,ImageGray)
***对截取的ROI滤波去除干扰
mean_image (ImageGray, ImageMean, 3, 3)
***选取ROI区域
*gen_rectangle2 (ModelRegion, 24.29, 20.63, rad(0), 40, 60)
gen_rectangle1 (ModelRegion, 94, 45, 177,131)
reduce_domain (ImageMean, ModelRegion, ImageROI)
***创建模型
create_shape_model (ImageROI, 1, -rad(120), rad(240), 'auto', 'auto', 'use_polarity', 70, 65, ModelID)
inspect_shape_model (ImageROI, ShapeModelImage, ShapeModelRegion,1, 70)
ModelFile := 'model.shm'
write_shape_model (ModelID, ModelFile)
clear_shape_model (ModelID)
*读取形状模板
read_shape_model (ModelFile, ReusedModelID)
*获取新装模板的轮廓,区域坐标等信息
*并在最后变换至原图去
get_shape_model_contours (ModelContours, ReusedModelID, 1)
get_shape_model_params (ReusedModelID[0], _, AngleStart, AngleExtent, _, _, _, _, _, mincon)
minScore := 0.7
*匹配数量,先验知识
numMatches := 10
*max_deformation 5表示的是允许变形5个像素值,0-32,0表示不允许变形,数字越大越耗时间
SubPix := ['least_squares','max_deformation 5']
NumLevels := 4
bFind := false
for Index4 := 1 to 2 by 1
if(Index4 > 1)
if(bFind)
break
endif
NumLevels := 2
endif
for minScoreSelect := minScore to 0.2 by -0.1
*AngleStart,AngleExtent必须使用文件导出的
find_shape_models (ImageMean, ReusedModelID, AngleStart, AngleExtent, minScoreSelect,\
numMatches, 0.0001, SubPix, min2(NumLevels,7),\
0.9, RowCheck, ColumnCheck, AngleCheck, Score, Model)
if(|ColumnCheck| == numMatches)
bFind := true
break
endif
endfor
endfor
*由模版外部轮廓生成眼膜mask
select_obj (ModelContours, ModelCont0, 1)
get_contour_xld (ModelCont0, Row, Col)
gen_region_polygon (Region, Row, Col)
shape_trans (Region,RegionTrans,'convex')
gen_empty_obj (RegionWaits)
*将模型轮廓变换为检测到的位置
dev_display (Image)
for I := 0 to |Score| - 1 by 1
hom_mat2d_identity (HomMat2D)
hom_mat2d_rotate (HomMat2D, AngleCheck[I], 0, 0, HomMat2D)
hom_mat2d_translate (HomMat2D, RowCheck[I], ColumnCheck[I], HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
affine_trans_region( RegionTrans,RegionTransNew ,HomMat2D ,'false' )
reduce_domain( ImageGray ,RegionTransNew , ImageReduce )
concat_obj (RegionWaits, ImageReduce, RegionWaits)
dev_set_color ('green')
dev_display (RegionTransNew)
dev_set_color ('red')
dev_display (TransContours)
endfor
stop ()
三、效果
? ?最后将边缘匹配到各个茶杯的效果
并且可以把每个茶杯使用掩膜仿射变换到各个匹配好的茶杯位置,并由reduce_domain 抠取下来保存在?RegionWaits 中
?