Shape-based Matching CAD Models with HALCON
HALCON’s Shape Based Matching is typically used for position recognition or alignment. For that, a pattern is trained once, and then searched for in subsequent images. There are a few things that can go wrong in the training process, especially when performed by personnel not so familiar with machine vision: The image quality could be poor (over-/underexposure, defocus, blur) or the features chosen could be inadequate, e.g., edges running in one direction only. In other words, there are good reasons to fully automate the training.
Creating models directly from CAD files does just that and eliminates human error. The CAD file first needs to be imported and converted into an XLD contour, then scaled in order to match the pixel resolution (see Figure 1). Subsequently, a shape model can be created directly from the contour. Translated into HALCON operators, this looks as follows:
read_contour_xld_dxf (Contours, ‘MyCADFile.dxf’, , , DxfStatus)
hom_mat2d_scale(HomMat2DIdentity, 1./PixelSize, 1./PixelSize, 0, 0, HomMat2DScale)
affine_trans_contour_xld(Contours, ContoursAffineTrans, HomMat2DScale)
create_shape_model_xld(ContoursAffineTrans, … , ModelID)
The variable PixelSize refers to the size of one image pixel in the native unit of the CAD file and has to be determined through calibration. After that, the model can be searched for with find_shape_model_xld (Figure 2). In addition, it might be helpful to define the polarity along the contours in the model, that is whether the gray value changes from dark to bright or vice versa. For this purpose, you can match the model to an image, and use set_shape_model_metric to store the polarity permanently in the model.
Since no image is used for the training, the parameter MinContrast has to be set manually when creating the model. In most cases, values between 5 and 10 should be sufficient, otherwise a suitable value can also be estimated from a test image with determine_shape_model_params or estimate_noise.