# \[Project\] Fast and rotation invariant template matching

**URL:** <https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895>\
**Category:** Uncategorized\
**Created:** [May 24, 2022, 12:36pm UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895 "2022-05-24T12:36:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![den8017981](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/den8017981/32/5276_2.png) [@den8017981](https://forum.opencv.org/u/den8017981)\
**Post date:** [May 24, 2022, 12:36pm UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/1 "2022-05-24T12:36:03Z")

</div>

Lots of people have complained about `cv::matchTemplate ()` because it is not a rotation invariant function, which means it is not practical.

Therefore, I improved and combined this function with a search strategy, and create a project.Also, this project passed some cases in real world assembly lines.

[Details and the source code of this project](https://github.com/DennisLiu1993/Fastest_Image_Pattern_Matching)

The formula of similarity is as followed:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/4d1b7bd86224eed635dd927b746e8b59e94da443.jpeg)

Some practical tests:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6a13d6461fb079c852c03534f8cc0182266a730b.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/48732586f51752d07b76866a600f6e2150732082.jpeg)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/ab9f0b72d5bb15022c8baec84d99ea815f30e6ec.png)

[It can also used as OCR](https://www.youtube.com/watch?v=lM0NK6xVNfg)

If you think my project useful and practical, give me a star on [github](https://github.com/DennisLiu1993/Fastest_Image_Pattern_Matching)

Current Status:  
I am doing researching on scale invariant version, perhaps next month can get a positive result.  
If you have any test data that can verify scale issue, please contact me.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 24, 2022, 5:51pm UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/2 "2022-05-24T17:51:27Z")

</div>

what does the code even do with the pyramid?

all I can tell from the code is that it tries `matchTemplate` on a number of rotated versions of the template. that shouldn’t be fast at all, considering a single `matchTemplate` call on any usefully large image takes a while.

and **please** stop posting that stuff to every thread you can find.

---

<div class="post-metadata">

**Author:** ![den8017981](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/den8017981/32/5276_2.png) [@den8017981](https://forum.opencv.org/u/den8017981)\
**Post date:** [May 25, 2022, 1:18am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/3 "2022-05-25T01:18:46Z")

</div>

1. what does the code even do with the pyramid?  
**speed up the whole process by reducing possible searching area**

2. on a number of rotated versions of the template. **not the rotated templates but the rotated source, which is the key element of this method**

3. that shouldn’t be fast at all, considering a single `matchTemplate` call on any usefully large image takes a while.  
**I just don’t get it. I made some tests in comparison with commercial image processing libraries, and the results demonstrated its speed.**  
**Of course, this method will become slow when image size increases**

4. stop posting  
**OK**

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 25, 2022, 10:39am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/4 "2022-05-25T10:39:40Z")

</div>

how hard would it be, to seperate the algorithm from the gui app ?  
as it is now, only VS users can even build it

---

<div class="post-metadata">

**Author:** ![den8017981](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/den8017981/32/5276_2.png) [@den8017981](https://forum.opencv.org/u/den8017981)\
**Post date:** [May 25, 2022, 11:40am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/5 "2022-05-25T11:40:51Z")

</div>

1. separating from gui is easy.  
only the part of showing results will be affected

2. vector can be replaced with some containers in other language

3. Uncertainty: direct access to Mat’s elements

---

<div class="post-metadata">

**Author:** ![den8017981](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/den8017981/32/5276_2.png) [@den8017981](https://forum.opencv.org/u/den8017981)\
**Post date:** [June 3, 2022, 4:00pm UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/6 "2022-06-03T16:00:15Z")

</div>

20220602 update:

Using SIMD version of image convolution to replace matchTemplate () with parameter CV\_TM\_CCORR

For many cases, it significantly speed up the process. (For small template, it is slight slow)  
See [this commit](https://github.com/DennisLiu1993/Fastest_Image_Pattern_Matching/commit/359696dd1a9150715864020c8f3883fbda630862)

---

<div class="post-metadata">

**Author:** ![FahNos](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fahnos/32/6303_2.png) [@FahNos](https://forum.opencv.org/u/FahNos)\
**Post date:** [October 19, 2022, 8:15am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/7 "2022-10-19T08:15:31Z")

</div>

@den8017981 Could you explain about this formular:  
double dAngleStep = atan (2.0 / max (pTemplData-\>vecPyramid[iTopLayer].cols, pTemplData-\>vecPyramid[iTopLayer].rows)) \* R2D;

Thank you,

---

<div class="post-metadata">

**Author:** ![den8017981](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/den8017981/32/5276_2.png) [@den8017981](https://forum.opencv.org/u/den8017981)\
**Post date:** [October 19, 2022, 8:28am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/8 "2022-10-19T08:28:00Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a1ca6296e837394c39b3415ce8fcae4d622319c9.png)  
This image can explain, just a little math.

---

<div class="post-metadata">

**Author:** ![FahNos](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fahnos/32/6303_2.png) [@FahNos](https://forum.opencv.org/u/FahNos)\
**Post date:** [October 24, 2022, 7:12am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/9 "2022-10-24T07:12:43Z")

</div>

@den8017981 Could you explain more about this function:  
void CMatchToolDlg::MatchTemplate() - SIMD: inline int IM\_Conv\_SIMD()

Thank you

---

<div class="post-metadata">

**Author:** ![den8017981](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/den8017981/32/5276_2.png) [@den8017981](https://forum.opencv.org/u/den8017981)\
**Post date:** [October 25, 2022, 3:33am UTC](https://forum.opencv.org/t/project-fast-and-rotation-invariant-template-matching/8895/10 "2022-10-25T03:33:56Z")

</div>

@ use this [document](https://www.intel.com/content/dam/develop/external/us/en/documents/18072-347603.pdf) provided by intel as a reference
