# Shape-based matching with MVTec HALCON: process the results, align ROIs or images

Hello and welcome. In the matching tutorial for beginners,
we created a simple matching program similar to this one. In this tutorial, we examine options for processing
the results of the matching function. First, you might want to count how many matches
you found. One way to do this is to determine the length
of one of the result tuples, for example, Score. You can do this quickly by enclosing Score
in vertical bars. Then, we use disp_message to display a message
containing the string ‘Matches: ‘, followed by the number of matches that we just calculated. Additionally, you might want to display the
score next to each match. Since Score has a lot of decimal places,
we might want to round it before visualizing it. In the HDevelop User’s Guide, chapter 8.5.8
String Operations, you can find some examples on string conversion. To round Score to two decimal places, we type
this line. Note that ScoreRounded is no longer a number,
but a string. Then, we display the message next to every
match. We have to set the CoordSystem to ‘image’,
since the content of the message is related to the image. Now, when stepping through the program,
the number of matches is displayed, and the visualization of the score gives you
a good impression of how well the objects match the shape model
used for searching. Next, I will show you how to re-align the
region of interest used to create the shape model with the matching
result. To do this, we need to tell the original region
how to move from where it was created to where it should be according to the matching. In HDevelop, we can do this with a transformation
matrix. Double-click on find_shape_model,
and then Help. Here, you can find an example showing how
to create a similar matrix. We need the operator vector_angle_to_rigid. The operator needs two sets of coordinates:
The original coordinates, and the ‘destination’ coordinates.
First, we need the coordinates of the original
region of interest.
We insert the operator area_center to get
them.
Then, we adjust the respective parameters
of vector_angle_to_rigid. The ‘destination coordinates’ of vector_angle_to_rigid
are the coordinates found by the matching. When executing the operator, we get a transformation
matrix. Next, we need to actually move the region. The movement is an affine transformation;
we need the operator affine_trans_region. The input parameters are already set correctly;
we transform the region ROI_0 according to HomMat2D.
The resulting region RegionAffineTrans is
our desired result. Lastly, we insert the dev_display operator
to visualize the region. When running the program,
we can see that the region is transformed correctly. But there is a problem:
When multiple models are found, we run into an exception. vector_angle_to_rigid can’t compute a transformation
matrix when multiple coordinates are passed,
like they are here. Instead, we have to use a for-loop
to loop through all matches separately. Control tuples begin with the index ‘zero’,
so we loop from zero to NumMatches-1. Then, we cut and paste the operators vector_angle_to_rigid,
affine_trans_region, and dev_display inside this loop,
and correct the formatting. Lastly, we adjust Row, Column, and Angle
to access the appropriate values. Now, the region is transformed for every match
separately. Alternatively, in some applications,
you might instead want to rotate the image such that the object is always oriented the same. To do this, we again use vector_angle_to_rigid. We want to move the image from where it was found to the reference position, defined by ROI_0. In this case, the coordinates found by the
matching are the origin, and the coordinates of ROI_0 are the destination. Next, we use the operator affine_trans_image
to transform the image accordingly. For better visualization, we clear the window
first, and then display the result. Lastly, we cut and paste the stop into the
loop so the program stops after every transformation
of every match in each image. When running the program,
we can see that all matches appear in the same orientation. This concludes our video. You should now be able to re-align
your regions of interest or images after matching, and do some basic things with the results. In the next video, we will take a closer look
at some advanced parameters of the matching operators. Thank you for watching.