# Using a PMAlign Tool to Fixture Images for the ViDi Red Analyze Tool

This topic describes how to convert the transforms produced by a PMAlign tool into transforms understood by ViDi tools. It also describes how to use a transform to modify a ViDi tool's region of interest in order to create a fixtured View that accomodates differences in translation and rotation between input images.

## Converting Transforms

The ROI for a VisionPro tool can be defined in absolute coordinates, or in coordinates relative to a pattern found by the PMAlign tool. This is done by defining a CogTransform2DLinear that maps points relative to the pattern to points pixels in the input image. By default, PMAlign tool Poses map points from Pattern Space to points in the input image's selected space; if the selected space is not Pixel space (say, the image is subsampled or calibrated), an additional transformation from the Selected Space to Pixel Space is necessary.

Tip:

###### API Example

    // Convert from pattern space into Pixel space because ViDi expects a linear transform defined in terms of pixels

    ICogTransform2D fromSelectedToPixelTransform = cogImage.GetTransform("#", ".");

    CogTransform2DLinear fromPatternToPixelLinearTransform = fromSelectedToPixelTransform.ComposeBase(<Pose>).LinearTransform(0, 0);

In this case, <Pose> is the transform returned by the PMAlign tool.

Similarly, the ROI (or View) for a ViDi tool can be defined in absolute coordinates, or in coordinates relative to the output of a previous tool, such as a Blue tool's Model. This is done by defining a System.Windows.Media.Matrix that maps points relative to the previous output to points pixels in the input image.

Naturally, it is possible to create a View relative to a matched PatMax pattern by converting the PMAlign tool's output from a CogTransform2DLinear into a pixel Matrix and mapping the View through this Matrix.

Note:

- The extension method ToMatrixTransform can be used to convert a CogTransform2DLinear into an equivalent System.Windows.Media.Matrix.
- The extension method ToLinear2DTransform can be used to perform the reverse conversion.

## Creating a Fixtured View

Before training a ViDi tool, it is necessary to define the Views on which the tool will operate. From the ViDi GUI, this is done by editing the rectangular ROI, applying the ROI to the training images, and then processing all of the training images (which applies the transform to each). From the API, the tool's RegionOfInterest must be set to be an IManualRegionOfInterest, and the size and transform used by the View must be specified manually. Then, the tool must process the image.

Note:

- The IManualRegionOfInterest interface exposes an AddView method, which accepts the Size of the View, as well as the Matrix transform.
  - Remember to transform the PMAlign tool Pose into Pixel Space.
  - Passing a Size of (0.0,0.0) will cause the new View to default to the IManualRegionOfInterest's size.
  - If you are not using the GUI and you want to move the ROI from its default position (origin at the center point of the matched pattern), remember to combine the final Pose Matrix with a Matrix describing how the ROI should move from its default position (see illustrated example below). The GUI handles this for you when you drag or rotate the ROI.
- There also exists an overload of AddView that will accept a CogTransform2DLinear instead of a Matrix.

## Summary

A ViDi tool can be fixtured with the PMAlign tool by doing the following:

1. Get the Pose from the CogPMAlignResult.
   1. Convert the Pose from CogTransform2DLinear to System.Windows.Media.Matrix using the provided extension method.
2. Get the RegionOfInterest from the ViDi tool and cast it to IManualRegionOfInterest.
3. Call the AddView method on the IManualRegionOfInterest to create the fixtured View, passing in a size and the Pose matrix.
4. Process the image.

For training, this procedure should be repeated for every training image before training the tool. This ensures that the tool will be trained on a fixtured View for each training image, rather than the entire training image. Of course, this involves first running each training image through the PMAlign tool to generate a transform for the View, as the fixture may move in each image.

For runtime, this procedure must be done for every incoming image so that the tool processes the fixtured View for that image, instead of the image itself. Again, this requires running the image through the PMAlign tool first.

Note:

- Processing a training image is done by calling the ITool.IToolDatabase.Process method, passing in a filename Filter corresponding to the training image to process.
  - The name passed to AddView should be the name of the image.
- Processing an input image at runtime is done by creating an ISample from the image and calling ISample.Process, passing in the ITool that should process the image.
  - The name passed to AddView should be the name of the Sample (i.e. ISample.Name).