Shape template matching based on Halcon learning [v] find_cocoa_packages_max_deformation.hdev routine

Posted by 8ennett on Wed, 26 Jan 2022 12:15:09 +0100

*This sample program demonstrates how to use shape based matching to find slightly deformed objects, using the parameter "maximum deformation";

*Note that to find deformed objects, you can use parameters to apply shape based matching "maximum deformation" or - apply local deformable matching;

*In this example, the task is to use shape based matching and the parameter "maximum deformation". You can compare it with the example "find deformable cocoa bags", which uses local deformation matching to solve the same task. Shape based matching is significantly faster, but local deformable matching is more robust to different types of images

dev_update_off ()
*Read image
read_image (ModelImage, 'food/cocoa_package_model')
*close window
dev_close_window ()
*Open window adaptive image
dev_open_window_fit_image (ModelImage, 0, 0, -1, -1, WindowHandle)
*Set display font
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')

Step 1: create a template

*Create and display shape models
create_shape_model (ModelImage, 'auto', rad(-20), rad(40), 'auto', 'auto', 'use_polarity', [40,60,'auto_min_size'], 10, ModelID)

*Get the outline of the template
*Note: the position of the template will return to the origin
get_shape_model_contours (ModelContours, ModelID, 1)

*Get the row and column coordinates of the area
area_center (ModelImage, Area, Row, Column)

*affine transformation 
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2DTranslate)

*Set a series of parameters
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (ModelImage)
dev_display (ContoursAffineTrans)

disp_message (WindowHandle, 'Model image and contours', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

2. Start template matching

*There are 13 pictures
NumImages := 13
*Start for loop
for Index := 1 to NumImages by 1
    *Read picture
    read_image (Image, 'food/cocoa_packages_' + Index$'02')

    *Reduce image resolution to improve speed
    dev_resize_window_fit_image (Image, 0, 0, -1, -1)
    *display picture    
    dev_display (Image)
    disp_message (WindowHandle, 'Search...', 'window', 12, 12, 'black', 'true')
    *Time before finding template
    count_seconds (S1)

    *Find the deformation model in the search image
    *And display the results

    *Start looking for templates
    find_shape_model (Image, ModelID, rad(-20), rad(40), 0.6, 0, 0.5, ['least_squares','max_deformation 16'], 0, 0.4, Row, Column, Angle, Score)
    *Time after finding template
    count_seconds (S2)
    Time := (S2 - S1) * 1000

    *Set color
    dev_set_color ('green')
    *Show matching results
    dev_display_shape_matching_results (ModelID, 'green', Row, Column, Angle, 1, 1, ModelID)
    disp_message (WindowHandle, |Score| + ' matches found in ' + Time$'3.1f' + ' ms', 'window', 12, 12, 'black', 'true')

    *Show results below picture
    disp_message (WindowHandle, 'Score: ' + Score$'.2f', 'image', 350, Column - 80, 'black', 'true')

    *If the index is less than the number of pictures, continue
    if (Index < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()

3. Clear the template

clear_shape_model (ModelID)

Note: find template parameters here

*Start looking for templates
    find_shape_model (Image, ModelID, rad(-20), rad(40), 0.6, 0, 0.5, ['least_squares','max_deformation 16'], 0, 0.4, Row, Column, Angle, Score)

1. In some applications, the accuracy requirements are very high. In these cases, the attitude of the model can be determined by least square adjustment. In contrast to "interpolation", this mode requires additional calculation time. Different least squares adjustment modes ('least_squares', 'least_squares_high' and 'least_squares_very_high') can be used to determine the accuracy of the minimum distance being searched. However, the higher the selection accuracy, the longer the sub-pixel extraction will take. In general, SubPixel should be set to "interpolation". "Least_squares" should be selected if least squares adjustments are required, as this will result in the best trade-off between runtime and accuracy.

2. In some cases, objects with slight deformation relative to the model cannot be found, or even if they are found, the accuracy is very low. For such objects, the maximum allowable object deformation can be additionally passed in the parameter SubPixel. Deformation must be specified in pixels. This can be done by passing the optional parameter value "max_deformation", followed by an integer value between 0 and 32 (in the same string), which specifies the maximum deformation. For example, if the shape of an object can deform up to 2 pixels relative to the shape stored in the model, you must pass the value "max_deformation 2" in SubPixel. When the value "max_deformation 0" is passed, the object must not be deformed during search, which is the same as not setting "max_deformation".

3. Note that setting the maximum deformation value high usually results in an increase in running time. In addition, the higher the selected deformation value, the higher the risk of discovering wrong model instances. These two problems mainly appear when searching for small objects or objects with fine structure. This is because this object will lose its characteristic shape when it is highly deformed, and the characteristic shape is very important for powerful search. It should also be noted that for high deformation, if there is clutter near the object, the accuracy of partially occluding the object may be reduced. Therefore, the maximum deformation should be as small as possible and set higher only when necessary.

Topics: image processing image identification