halcon基于形状的模板匹配第二例:create_average_shape_model.hdev

该范例演示如何通过根据多个实例的均值图片创建模板,提高匹配时的匹配度。

大致分为两个部分:

第一个部分,采用自己模拟作画,画出一个模板,同时降低find_shape_model的匹配度至0.65,来找到一些样本;

第二个部分,对找到的这些样本对齐重叠,然后均值化,生成全新的模板,然后再提升匹配度参数至0.8,样本仍然全部找到;

对于实际使用中的启发是,实际运行过程中,由于待测物可能每个批次之间具有一定的差异性,而单纯采用一个实际的实体作为模板,可能会导致匹配度参数必须降低才能找到想要的东西,但是副作用是可能会找到一些本身就匹配不佳的错误目标,

而采用此方案可以降低这种风险。

* 
* This example demonstrates how to create a model out of a set
* of samples in order to achieve greater robustness. The idea
* is to create an average image out of a set of samples from a
* given object in order to average out distractors like surface
* texture, or variations in shape and/or size.
* 
* In order to carry out this operation, one first need to align
* the samples. In this particular example, sample objects are
* found and aligned by means of shape-based matching with an
* artificial model based on previous knowledge of the object's
* geometry
* 
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')
* 
MinScore1 := 0.65
MinScore2 := 0.8
create_reference_image (ReducedReferenceImage)
area_center (ReducedReferenceImage, Area, RowRef, ColumnRef)
get_image_size (ReducedReferenceImage, RefImgWidth, RefImgHeight)
dev_open_window (0, Width + 12, 3 * RefImgWidth, 3 * RefImgHeight, 'black', WindowHandleAvg)
set_display_font (WindowHandleAvg, 14, 'mono', 'true', 'false')
dev_display (ReducedReferenceImage)
disp_message (WindowHandleAvg, '1. Search artificial model', 'window', -1, -1, 'black', 'true')
disp_continue_message (WindowHandleAvg, 'black', 'true')
stop ()
* 
* Search artificial model in image with set of sample objects
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)
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')
stop ()
* 
* Average over all matches and calculate the average image
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[K], Column[K], Angle[K], HomMat2DTranslate)
    hom_mat2d_scale (HomMat2DTranslate, Scale[K], Scale[K], Row[K], Column[K], 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_set_window (WindowHandle)
    if (K > 0)
        dev_display_shape_matching_results (ModelID, 'green', Row[K - 1], Column[K - 1], Angle[K - 1], Scale[K - 1], Scale[K - 1], 0)
    endif
    dev_display_shape_matching_results (ModelID, 'yellow', Row[K], Column[K], Angle[K], Scale[K], Scale[K], 0)
    dev_set_window (WindowHandleAvg)
    dev_display (ImagePart)
    wait_seconds (0.2)
endfor
* Average template image
channels_to_image (Templates, MultiChannelImage)
mean_n (MultiChannelImage, ImageMean)
dev_set_window (WindowHandle)
dev_display_shape_matching_results (ModelID, 'green', Row[K - 1], Column[K - 1], Angle[K - 1], Scale[K - 1], Scale[K - 1], 0)
dev_set_window (WindowHandleAvg)
dev_display (ImageMean)
disp_message (WindowHandleAvg, 'Average object image', 'window', -1, -1, 'black', 'true')
disp_continue_message (WindowHandleAvg, 'black', 'true')
stop ()
* 
* Use the average image to create a model that resembles more closely
* the actual appearance of the smd
create_scaled_shape_model (ImageMean, 0, rad(-1), rad(2), 0, 0.77, 0.78, 0, 'point_reduction_low', 'use_polarity', 45, 30, ModelIDAvg)
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_display (ImageMean)
disp_message (WindowHandleAvg, 'Resulting shape based model', 'window', -1, -1, 'black', 'true')
get_shape_model_contours (ModelContoursAvg, ModelIDAvg, 1)
area_center (ImageMean, Area1, Row1, Column1)
vector_angle_to_rigid (0, 0, 0, Row1, Column1, 0, HomMat2D)
affine_trans_contour_xld (ModelContoursAvg, ContoursAffinTrans, HomMat2D)
dev_set_color ('yellow')
dev_display (ContoursAffinTrans)
disp_continue_message (WindowHandleAvg, 'black', 'true')
stop ()
* 
dev_set_window_extents (-1, -1, Width, Height)
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)

范例中一个固定写法:

dev_display_shape_matching_results (ModelID, 'green', Row, Column, Angle, Scale, Scale, 0)

* This procedure displays the results of Shape-Based Matching.
* 
NumMatches := |Row|
if (NumMatches > 0)
    if (|ScaleR| == 1)
        tuple_gen_const (NumMatches, ScaleR, ScaleR)
    endif
    if (|ScaleC| == 1)
        tuple_gen_const (NumMatches, ScaleC, ScaleC)
    endif
    if (|Model| == 0)
        tuple_gen_const (NumMatches, 0, Model)
    elseif (|Model| == 1)
        tuple_gen_const (NumMatches, Model, Model)
    endif
    for Index := 0 to |ModelID| - 1 by 1
        get_shape_model_contours (ModelContours, ModelID[Index], 1)
        dev_set_color (Color[Index % |Color|])
        for Match := 0 to NumMatches - 1 by 1
            if (Index == Model[Match])
                hom_mat2d_identity (HomMat2DIdentity)
                hom_mat2d_scale (HomMat2DIdentity, ScaleR[Match], ScaleC[Match], 0, 0, HomMat2DScale)
                hom_mat2d_rotate (HomMat2DScale, Angle[Match], 0, 0, HomMat2DRotate)
                hom_mat2d_translate (HomMat2DRotate, Row[Match], Column[Match], HomMat2DTranslate)
                affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DTranslate)
                dev_display (ContoursAffinTrans)
            endif
        endfor
    endfor
endif
return ()

中间一段:

hom_mat2d_identity+hom_mat2d_scale->hom_mat2d_rotate->hom_mat2d_translate->affine_trans_contour_xld

可以实现将模板“套”在实际图片上的操作,也可以实现ROI的重定位。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶醉鱼

感觉写的不错就给个打赏哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值