<a id="easyobject-template-matcher"></a>

# Object Template Matcher

The class [`EObjectTemplateMatcher`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html) is a tool designed to align and match the output of **EasyObject** to a reference template. It is designed to efficiently handle thousands of objects.

![](assets/images/easyobject/eobjectsort_01.svg)

<a id="easyobject-template-matcher-reference"></a>

## Creating the Reference Template

Use the method [`BuildTemplate`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a4a01295edbab5a9000b2ac1ef0edfccd) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#ac0b2b6e180fbeddc13bda1794bfdcab2) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a3eef04fc00fa6aba0d6d7abafd15922f) to create the reference template, with one of the following parameters:

- An [`ECodedImage2`](../../../doxygen_cpp/classeVision_1_1ECodedImage2.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECodedImage2.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECodedImage2.html), result of the method [`EImageEncoder::Encode`](../../../doxygen_cpp/classeVision_1_1EImageEncoder.html#a0212a8144f93eba43fbed584140a83cd) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EImageEncoder.html#a0956dbfef4ebce656f72e3471997e518) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EImageEncoder.html#a1292c1003f94e89af9600e99198aed4c).
- An [`EObjectSelection`](../../../doxygen_cpp/classeVision_1_1EObjectSelection.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectSelection.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectSelection.html), a selection (subset) of [`ECodedImage2`](../../../doxygen_cpp/classeVision_1_1ECodedImage2.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1ECodedImage2.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1ECodedImage2.html) objects.
- A list of positions, given by a vector of [`points`](../../../doxygen_cpp/structeVision_1_1EPoint.html) [<sub>.NET</sub>](../../../doxygen_cs/structeVision_1_1EPoint.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EPoint.html).

![](assets/images/easyobject/eobjectsort_02.webp)![](assets/images/easyobject/eobjectsort_03.webp)

<a id="easyobject-template-matcher-matching"></a>

## Matching the objects

To perform object matching after setting up the template:

- Use the method [`Match`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a79cb3bd14e6e2517db2c9149b167e648) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a4c46eb7eeb406ff9406ed62f5071e5d1) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#af5e617ef1dbed38ffb4c70f40e28d6e8) with an [`EObjectSelection`](../../../doxygen_cpp/classeVision_1_1EObjectSelection.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectSelection.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectSelection.html) as parameter.
  - The bounding box center of the objects is the position used for the matching with the template.
- Or use the more generic method [`Match`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a79cb3bd14e6e2517db2c9149b167e648) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a4c46eb7eeb406ff9406ed62f5071e5d1) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#af5e617ef1dbed38ffb4c70f40e28d6e8) with a `std::vector<EPoint>` as parameter.

Before matching, [`EObjectTemplateMatcher`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html) performs an optional global rigid alignment of the submitted positions with the defined template.

![The template.](assets/images/easyobject/eobjectsort_04_a.webp)![The alignment of the submitted selection.](assets/images/easyobject/eobjectsort_04_b.webp)
- An alignment consists of a translation and a rotation.
- Use the method [`SetAlignment`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a653194e0dbd62b478352dd37fa8d1af5) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a21bc7bf90ff4d11b1a4bd10cc3aa2039) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a307d42d9861b7c7a9f0f0b9a1a5d858c) to enable one of the alignment processes.
- The available alignment processes are:
  - [`EObjectTemplateMatcherAlignment_Fast`](../../../doxygen_cpp/namespaceeVision.html#a81458ea753417ef107374ec1a6bb2921) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aed07b56499fef7c9b8df6371ac6ded23) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcherAlignment.html#a8bd09c8dede86c6e0f18dfc37d3e2719): a fast and global alignment but that is not robust enough for all use cases.
  - [`EObjectTemplateMatcherAlignment_Local`](../../../doxygen_cpp/namespaceeVision.html#a81458ea753417ef107374ec1a6bb2921) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aed07b56499fef7c9b8df6371ac6ded23) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcherAlignment.html#a8660108e95e2d452248ad1b137c8002f): a local alignment that works well if the angle between the template and the selection is small and if there are not too many missing or extra objects.
  - [`EObjectTemplateMatcherAlignment_Robust`](../../../doxygen_cpp/namespaceeVision.html#a81458ea753417ef107374ec1a6bb2921) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aed07b56499fef7c9b8df6371ac6ded23) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcherAlignment.html#a7960edb43202f3dd16b25cdc88402862): a slower but more robust algorithm.

#### TIP
For [`EObjectTemplateMatcherAlignment_Fast`](../../../doxygen_cpp/namespaceeVision.html#a81458ea753417ef107374ec1a6bb2921) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aed07b56499fef7c9b8df6371ac6ded23) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcherAlignment.html) and [`EObjectTemplateMatcherAlignment_Robust`](../../../doxygen_cpp/namespaceeVision.html#a81458ea753417ef107374ec1a6bb2921) [<sub>.NET</sub>](../../../doxygen_cs/namespaceeVision.html#aed07b56499fef7c9b8df6371ac6ded23) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcherAlignment.html), you can also specify a maximum angle with the method [`SetMaximumAngle`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#afd26d7aff798db085ac2593e90696849) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#acc5b2831115f1c39c63ab8e9b4f890a7) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#ab54e6690d679a6e0108b754fe32a59a7). This is especially useful when the objects form a (nearly) symmetric pattern.

After the optional alignment, [`EObjectTemplateMatcher`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html) matches the submitted positions with the reference template.

- It uses the shortest distance criterion to pair these positions with the template.
- You can set the maximum distance to constraint the search. This can speed up processing.

<a id="easyobject-template-matcher-results"></a>

## Retrieving the Matching Results

- Use one of these methods to retrieve the matching results:
  - [`GetSelectionIndexes`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a764ce35384e568a66f1ed95904c52d27) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a9598afa6b3b41a1d7710b75eec3ba3ca) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a9fb12629a946ad32b88d4009c28b6e6f) returns, for each position in the template, the paired index in the selection. The value `-1` is used if the object in the template has no correspondence in the selection. Alternatively, use the method [`GetMatched`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a92c1a2015737f1593608c9a7668902d1) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a3bf2680db82db7a8a31c5d4d17bc81ef) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a7e025ad6d0772dce5c92edc841341331) to retrieve the corresponding objects or points.
  - [`GetTemplateIndexes`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a7d31628bde96f6860628e8ab9e713ea7) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#aa85de23a1c8c823f011f4c544cdbf587) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a3c745e7d1f8afd79aaeeefa208776087) returns, for each position in the selection, the paired index in the template. The value `-1` is used if the object in the selection has no correspondence in the template. Alternatively, use the method [`GetMatched`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a92c1a2015737f1593608c9a7668902d1) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a3bf2680db82db7a8a31c5d4d17bc81ef) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a7e025ad6d0772dce5c92edc841341331) (with the argument `fromSelection` set to `false`) to retrieve the corresponding objects or points.
  - [`GetUnpairedObjects`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a8e07234d5e474e922d1c93e324b8a724) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a302ab8f9affc2877e158dd5e5f87d593) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a73a02425ed76a928e6634e3fa73ef4d4) returns the positions in the template and in the selection that have not been paired. Alternatively, use the methods [`GetMissing`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#ab10b1ba1537307cb658fb82089bbb103) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#abd85f14d47904bd092098b07d80e73ba) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a1535d8f7179c5717c5aeae1681345730) and [`EObjectTemplateMatcher::GetExtra`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#aafdfb04c74804dd3fc78be0ee4a1d8bd) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#aca43db1348f533e67029289d45bc7506) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#a43fe47b729eeb300594684627a8ced47) to retrieve the corresponding objects or points.
- Use the method [`GetNumberOfPairedObjects`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a9a92016d2d00081ec011107211c8bf90) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#a3ead838203a0bbcebc7367dadbf09dff) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#ae185ea2b023c6854c2b806b965ab4f90) to get the total number of paired objects.
- Use the methods [`Save`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a309f9fb69af94ef593c7dc935416088b) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#aa89abff5a6780c45f085227352653270) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#adfdaf4ad3539f5b20f5af2b802908d34) and [`Load`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html#a6a39cba3c83e96117a1c00304485d508) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html#ad6a4403214deb616d19362a3396b43fe) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html#aca3152774f50a670b33f4cdd040462c0) to store and retrieve the configuration of an [`EObjectTemplateMatcher`](../../../doxygen_cpp/classeVision_1_1EObjectTemplateMatcher.html) [<sub>.NET</sub>](../../../doxygen_cs/classeVision_1_1EObjectTemplateMatcher.html) [<sub>Python</sub>](../../../doxygen_python/classeVision_1_1EObjectTemplateMatcher.html) object, including the template.

![image](assets/images/easyobject/eobjectsort_05.webp)
