halcon template matching - matching multiple different objects at the same time

Scenes

insert image description here
The requirement is to search and match two different types of objects in one view at the same time.

Effect

insert image description here

Implementation steps

1. Read the image

read_image (ModelImage, 'rings_and_nuts')

2. Generate two ROIs separately

gen_circle (ModelROIRing, 121, 299, 55)
gen_circle (ModelROINut, 324, 279, 52)
reduce_domain (ModelImage, ModelROIRing, ImageROIRing)
reduce_domain (ModelImage, ModelROINut, ImageROINut)

3. Create Isotropic Scaling Shape Templates Separately

create_scaled_shape_model (ImageROIRing, 'auto', -rad(22.5), rad(45), 'auto', 0.8, 1.2, 'auto', 'none', 'use_polarity', 60, 10, ModelIDRing)
create_scaled_shape_model (ImageROINut, 'auto', -rad(30), rad(60), 'auto', 0.6, 1.4, 'auto', 'none', 'use_polarity', 60, 10, ModelIDNut)

4. Obtain the contour representation of the shape model separately

inspect_shape_model (ImageROIRing, PyramidImage, ModelRegionRing, 1, 30)
get_shape_model_contours (ShapeModelRing, ModelIDRing, 1)`
inspect_shape_model (ImageROINut, PyramidImage, ModelRegionNut, 1, 30)
get_shape_model_contours (ShapeModelNut, ModelIDNut, 1)

5. Connect the two template outlines XLD

concat_obj (ShapeModelRing, ShapeModelNut, ShapeModels)

6. Save template related information

保存两个模板轮廓的个数到数组
count_obj (ShapeModelRing, NumContoursRing)
count_obj (ShapeModelNut, NumContoursNut)
NumContoursInTuple := [NumContoursRing,NumContoursNut]

保存两个模板的句柄到数组
ModelIDs := [ModelIDRing,ModelIDNut]

保存两个模板轮廓的起始索引到数组
StartContoursInTuple := [1,NumContoursRing + 1]

After the above work is completed, you can start searching for matches on the new image

7. Read the new image

read_image (ModelImage, 'rings_and_nuts')

8. Search for matches

find_scaled_shape_models (SearchImage, ModelIDs, [-rad(22.5),-rad(30)], [rad(45),rad(60)], [0.8,0.6], [1.2,1.4], 0.7, 0, 0, 'least_squares', 0, 0.8, RowCheck, ColumnCheck, AngleCheck, ScaleCheck, Score, ModelIndex)

8. Apply any affine 2D transformation to the searched XLD contours

for i := 0 to |Score| - 1 by 1
	*获取搜索到的模型索引
    Model := ModelIndex[i]
    *生成仿射变换矩阵
    vector_angle_to_rigid (0, 0, 0, RowCheck[i], ColumnCheck[i], AngleCheck[i], MovementOfObject)
    hom_mat2d_scale (MovementOfObject, ScaleCheck[i], ScaleCheck[i], RowCheck[i], ColumnCheck[i], MoveAndScalingOfObject)
   *根据搜索到的模型索引获取模板金字塔中的XLD轮廓
    copy_obj (ShapeModels, ShapeModel, StartContoursInTuple[Model], NumContoursInTuple[Model])
   *XLD轮廓应用仿射变换
    affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MoveAndScalingOfObject)
endfor

9. Clear the handle

clear_shape_model (ModelIDRing)
clear_shape_model (ModelIDNut)

Guess you like

Origin blog.csdn.net/weixin_44901043/article/details/123574803