Skip to content

Reuse the model🔗

If you want to reuse created models or training results in other HALCON applications, all you need to do is to store the relevant information in files and then read them again.

The example program %HALCONEXAMPLES%\solution_guide\matching\reuse_model.hdev shows exemplarily how to reuse a model for a uniformly scaled shape-based matching. First, the model is created.

create_generic_shape_model (ModelID)
set_generic_shape_model_param (ModelID, 'iso_scale_min', 0.6)
set_generic_shape_model_param (ModelID, 'iso_scale_max', 1.4)

Then, the model is stored in a file using the operator write_shape_model. With the model, HALCON automatically saves the XLD contour, the point of reference, and the parameters that were used in the calls to set_generic_shape_model_param.

write_shape_model (ModelID, ModelFile)

Note that the model region, that is, the domain of the image, is not saved when storing the model. Thus, if you want to reuse it, too, you can store the template image with write_image.

ModelRegionFile := 'model_region_nut.png'
write_image (ImageROI, 'png', 0, ModelRegionFile) 

The model is read using the operator read_shape_model.

read_shape_model (ModelFile, ReusedModelID)

Now, the model can be used as if it was created in the application itself. We set parameters specifying the search, in this example the value for min_score, using set_generic_shape_model_param.

set_generic_shape_model_param (ReusedModelID, 'min_score', 0.8)

Model instances are searched and displayed on a new image.

read_image (SearchImage, 'rings_and_nuts')
find_generic_shape_model (SearchImage, ReusedModelID, MatchResultID, NumMatchResult)
get_generic_shape_model_result_object (Objects, MatchResultID, 'all', 'contours')
dev_display (Objects) 

Note that the information that is stored in a model and that can be queried after reading a model from file depends on the selected approach.