Halcom 发表于 2020-2-12 20:36:31

基于多模板平均值的匹配方法

基于多模板平均值的匹配方法:多个模板,取个平均模板,增强鲁棒性
第一步:采用程序或者手工,绘制一个轮廓模型,标准的轮廓模型:create_reference_image (ReducedReferenceImage)
Row1 := 20
Row2 := 64
Column1 := 20
Column2 := 106
Column11 := 38
Column12 := 88
gen_contour_polygon_rounded_xld (TemplateShape, , , , 1)
gen_contour_polygon_rounded_xld (TemplateLeft, , , , 1)
gen_contour_polygon_rounded_xld (TemplateRight, , , , 1)
gen_image_const (BlacKBacKground, 'byte', Column2 + 20, Row2 + 20)
paint_xld (TemplateShape, BlacKBacKground, ReferenceImage1, 90)
paint_xld (TemplateLeft, ReferenceImage1, ReferenceImageTmp, 150)
paint_xld (TemplateRight, ReferenceImageTmp, ReferenceImage, 150)
concat_obj (TemplateShape, TemplateLeft, Tmp)
concat_obj (Tmp, TemplateRight, DispTemplate)
gen_rectangle1 (TemplateROI, Row1 - 10, Column1 - 10, Row2 + 10, Column2 + 10)
reduce_domain (ReferenceImage, TemplateROI, ReducedReferenceImage)
第二步:采用绘制好的模板,初定位实物图像,得到很多定位出的图像:
dev_update_off ()
dev_close_window ()
read_image (Image, 'smd/smd_samples')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
dev_set_line_width (2)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* 创建人工程序绘制的模板
create_reference_image (ReducedReferenceImage)
* 模板匹配定位(带scale)
create_scaled_shape_model (ReducedReferenceImage, 0, rad(-1), rad(2), 0, 0.77, 0.78, 0, 'point_reduction_low', 'use_polarity', 30, 3, ModelID)
* get_shape_model_params (ModelID, NumLevels, AngleStart, AngleExtent, AngleStep, ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)
MinScore1 := 0.65
find_scaled_shape_model (Image, ModelID, rad(0), rad(180), 0.77, 0.78, MinScore1, 0, 0, 'interpolation', 2, 0.7, Row, Column, Angle, Scale, Score)
* 显示匹配定位的结果
dev_set_window (WindowHandle)
dev_display (Image)
dev_set_color ('green')
dev_display_shape_matching_results (ModelID, 'green', Row, Column, Angle, Scale, Scale, 0)
disp_message (WindowHandle, 'MinScore: ' + MinScore1, 'window', -1, -1, 'black', 'true')
第三步:获取平均模板图像,通过扣取定位的结果,求解多个结果的平均值,即平均模板,增强鲁棒性;
* 获取全部的匹配定位结果
area_center (ReducedReferenceImage, Area, RowRef, ColumnRef)
get_image_size (ReducedReferenceImage, RefImgWidth, RefImgHeight)
dev_open_window (0, Width + 12, 3 * RefImgWidth, 3 * RefImgHeight, 'black', WindowHandleAvg)
dev_set_window (WindowHandleAvg)
dev_set_line_width (2)
get_shape_model_contours (ModelContours, ModelID, 1)
gen_empty_obj (Templates)
for K := 0 to |Row| - 1 by 1
    * 仿射变换--平移
    vector_angle_to_rigid (RowRef, ColumnRef, 0, Row, Column, Angle, HomMat2DTranslate)
    * 仿射变换--缩放
    hom_mat2d_scale (HomMat2DTranslate, Scale, Scale, Row, Column, HomMat2DScale)
    * 仿射变换--逆变换
    hom_mat2d_invert (HomMat2DScale, HomMat2DInvert)
    affine_trans_image (Image, ImageAffinTrans, HomMat2DInvert, 'constant', 'false')
    crop_rectangle1 (ImageAffinTrans, ImagePart, 0, 0, 80, 130)
    concat_obj (Templates, ImagePart, Templates)
    * 显示结果
    dev_display_shape_matching_results (ModelID, 'yellow', Row, Column, Angle, Scale, Scale, 0)
    dev_set_window (WindowHandleAvg)
    dev_display (ImagePart)
endfor
* 将多通道的Templates,求解其平均值 Average template image
channels_to_image (Templates, MultiChannelImage)
mean_n (MultiChannelImage, ImageMean)
dev_set_window (WindowHandleAvg)
dev_display (ImageMean)
第四步:采用平均模板图像构建find_shape_model进行模板定位匹配,得到结果如上图所示。
* 采用平均模板图像进行模板定位匹配
create_scaled_shape_model (ImageMean, 0, rad(-1), rad(2), 0, 0.77, 0.78, 0, 'point_reduction_low', 'use_polarity', 45, 30, ModelIDAvg)
MinScore2 := 0.8
find_scaled_shape_model (Image, ModelIDAvg, rad(-1), rad(2), 0.77, 0.78, MinScore2, 0, 0, 'interpolation', 2, 0.5, Row, Column, Angle, Scale, Score)
* 显示匹配定位的结果
dev_set_window (WindowHandleAvg)
dev_display (Image)
dev_set_color ('green')
dev_display_shape_matching_results (ModelIDAvg, 'green', Row, Column, Angle, Scale, Scale, 0)
disp_message (WindowHandleAvg, 'MinScore: ' + MinScore2, 'window', -1, -1, 'black', 'true')
* 清理内存
clear_shape_model (ModelIDAvg)
clear_shape_model (ModelID)

















页: [1]
查看完整版本: 基于多模板平均值的匹配方法