Ask Your Question
0

Why is opencv's Template Matching method TM_SQDIFF so fast?

asked 2016-01-15 03:05:22 -0600

randomGirl gravatar image

updated 2020-11-02 17:23:34 -0600

I have tried implementing my own code for the Sum of Squared Differences (SSD) for my template matching using the algorithm given in http://docs.opencv.org/2.4/modules/im...

I compare the template with the original image pixel by pixel (using 4 for loops) up to (size of original image - size of template image). However, the time taken can be up to 1000 times (10000ms - depends on image dimension) longer compared to opencv's TM_SQDIFF (10ms, constant for every image dimensions).

I then tried to compare template to original image in block size, first with template size, then down focus to the area with minimum SSD, time is significantly reduced. Thus, I suspect that Opencv actually computes the difference between template and original image in block size.

I have read through the open source, templmatch.cpp but still do not get the clue. I am finding the code complicated even though i read them line by line. Also, some of the called functions, I could not find where is it. For example the Caller callers[method], dst(parameters), dft(parameters), part(parameters), sqsum[] etc.

I know I could just implement my template matching method using opencv's library but it is part of my assignments to find out the reason.

Anyway, my main question is what makes opencv's TM_SQDIFF so much faster? No much explanation in the opencv document as well.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-01-15 17:06:18 -0600

Tetragramm gravatar image

Hopefully this will help you understand. http://www.jot.fm/issues/issue_2010_03/column2.pdf

In short, using the Discrete Fourier Transform (DFT), you can compute the whole cross correlation two DFTs and a multiplication. If all you are doing is cross correlation, you're done. Computing the SQDIFF or CCOEFF or normalizing requires some more work, which happens in the common_matchTemplate function.

edit flag offensive delete link more

Comments

Hello Sir, thanks for your reply. What are the some more work that you are referring to? Basically what I understand from the algorithm we just look for the minimum sum of squared difference and that will indicate the location of the template. Is it just this?

randomGirl gravatar imagerandomGirl ( 2016-01-16 08:54:57 -0600 )edit

I've taken the common_match_template function and removed all the parts that don't run when you aren't using SQDIFF. http://pastebin.com/zUEHn7et Hopefully that's easier to follow.

The second thing is this, which explains the relationship between cross correlation and the sum of squared differences. Also, why speeding up the first helps speed up the second. http://www.cse.psu.edu/~rtc12/CSE486/lecture07.pdf

Tetragramm gravatar imageTetragramm ( 2016-01-16 10:34:55 -0600 )edit

That's very much information! I have read through the pdf files and kind of get the idea behind. It helps a lot! Thanks again!

randomGirl gravatar imagerandomGirl ( 2016-01-17 00:45:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-15 03:05:22 -0600

Seen: 7,127 times

Last updated: Jan 15 '16