Halcon creates templates and performs template matching

When performing image processing, creating a template and analyzing the image reference point by matching the template can greatly improve the accuracy and efficiency of ROI selection.

The following describes the rapid learning application of template matching through examples.

 

First create a template , read the image first, create a template
for the read image and save it. To create a template, select the features or areas that exist in all images to ensure that the program can perform image analysis processing normally during actual processing.

The operators beginning with draw in Halcon all require humans to manually draw on the image,

code show as below:

1 * Read image
 2 read_image (Image, ' H: / NEW IMAGE / 21.tiff ' )
 3 * Manually draw ROI on the image, that is, select the drawn template area
 4  draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
 5 * Generate a rectangle from the drawn template area
 6  gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
 7 * Extract the image of the area as a template image
 8  reduce_domain (Image, Rectangle, ImageReduced1)
 9 * Create a template
 10 create_shape_model (ImageReduced1 , ' auto ' ,- 0.39 , 0.79 , 'auto ' , ' auto ' , ' use_polarity ' , ' auto ' , ' auto ' , ModelID)
 11 * Save the created template as a file at the specified path
 12 write_shape_model (ModelID, ' E: /modle1.shm ' )

After the template is created, the following handler only needs the template file saved at the end of the above code. The code is as follows:

1 * Read the image
 2 read_image (Image1, ' H: / NEW IMAGE / 13.tiff ' )
 3 * Read the template file
 4 read_shape_model ( ' E: /modle1.shm ' , ModelID1)
 5 * Find the template in the image, and return to the center coordinates of the template
 . 6 find_shape_model (Image1, ModelID1, - 0.39 , 0.78 , 0.5 , . 1 , 0.5 , ' least_squares ' , 0 , 0.9 , Row1, Column1, Anglel, Score1)
 . 7 * matches to the template
 8 if (| Row1 | == 1 )
 9 * Show template in image
 10      dev_display_shape_matching_results (ModelID1, ' red ' , Row1, Column1, Angle1, 1 , 1 , 0 )
 11 endif

The above is a simple template creation and template matching, I hope to help everyone, thank you.

Guess you like

Origin www.cnblogs.com/ybqjymy/p/12735784.html