| Customize Help

Defining and adding models to your Pattern Matching context

Once you have allocated a Pattern Matching context, using MpatAlloc(), you can begin to add models to your Pattern Matching context, using MpatDefine().

There are two types of models: models defined from a specified location in the model's source image, and models that MIL automatically defines from the model's source image. These models can be mixed in the same Pattern Matching context, using MpatDefine().

Models of both types should be distinct and not defined from a flat region (consistent pixel values with no edges present). Also, the model's source image should be of the best quality possible. When defining multiple models, they must be the same size and at the same resolution. If noisy, try to clean the image, using the image processing techniques discussed elsewhere in this user guide.

Automatic models

To have MIL define one or more distinct models from a model's source image, use MpatDefine() with M_AUTO_MODEL. Specify the width and height of the model(s), as well as the maximum displacement expected between the position of the model in the source image and the position of the model when found in the target image. These four measurements define an area in which MIL expects to find a model in the source image. The reference position (used to identify the start of the area) is placed at half the width and height of the extracted model. To change the reference position, use MpatControl() with M_REFERENCE_X and M_REFERENCE_Y.

You can only define M_AUTO_MODEL models that respect the following condition: (MaxPixelValue 2 * ModelWidth x ModelHeight) < 2 63 , where MaxPixelValue is the maximum pixel value (typically, 255) in the target image and the model. This restriction is imposed to avoid overflows in the internal 64-bit accumulators.

The total area of the model must be greater or equal to 4 pixels.

Regular models

To define a model from a specified location in a model's source image, use MpatDefine() with M_REGULAR_MODEL; this is referred to as a regular model. To create a regular model, pass M_REGULAR_MODEL, the model's required offset from the top-left corner of the model's source image, width, and height. A regular model is typically used in cases where the target image contains an inconsistent surrounding (such as an image of loose nuts and bolts lying on a metal sheet). If the eventual model can appear at an angle, enable angular search, using MpatControl() with M_SEARCH_ANGLE_MODE and set the range of angles at which it can appear using M_SEARCH_ANGLE.... If the model has a background that will be consistently found in the target image (such as when searching for a chip on an integrated circuit board), combine M_REGULAR_MODEL with M_CIRCULAR_OVERSCAN when calling MpatDefine().

To define a model from a location that you know is rotated, you can specify an image that is associated to an ROI that was set using a rotated rectangle in a 2D graphics list. To add a rotated rectangle to a 2D graphics list, you can call MgraRectAngle(); to set (associate) that rectangle as the ROI of an image buffer, you can call MbufSetRegion() with M_RASTERIZE or M_NO_RASTERIZE and M_FILL_REGION. Note, settings related to search angle and circular overscan still apply when using a rotated rectangular graphic (ROI).

Drawing models

Upon definition, the model is extracted from the selected region in the model's source image buffer and stored in a non-displayable model buffer. The model's source image buffer is then no longer needed. To view the portion of the image from which the model was extracted (referred to as the model image), use MpatDraw() with M_DRAW_IMAGE. MpatDraw() can also draw the various model features (such as M_DRAW_POSITION). Use the ControlFlag parameter to specify where in the destination buffer to draw the model information. You can start drawing at the top-left pixel of the destination buffer (M_DEFAULT) or at the same offset as was used to extract the model from the model's source image buffer (M_ORIGINAL).

You can use a previously allocated 2D graphics context (see Chapter 24: Generating graphics) to control the drawing color, or use the default 2D graphics context (M_DEFAULT). You can also choose to draw in the display's overlay buffer. By drawing into the display's overlay buffer, you can annotate an image non-destructively. For more information, see Chapter 23: Displaying an image.