File Exchange

## Fast/Robust Template Matching

version 1.2.0.0 (13.4 KB) by Dirk-Jan Kroon

### Dirk-Jan Kroon (view profile)

Template Matching with SSD Block Matching and Normalized CC, (color) 2D and 3D

Updated 22 Feb 2011

TEMPLATE_MATCHING is a CPU efficient function which calculates matching score images between template and (color) 2D image or 3D image volume.

It calculates:
- The sum of squared difference (SSD Block Matching), robust template matching.
- The normalized cross correlation (NCC), independent of illumination, only dependent on texture

The user can combine the two images, to get template matching which works robust with his application.

Both matching methods are implemented using FFT based correlation.

Try the examples.

Please report: bugs, successes and other comment.

### Cite As

Dirk-Jan Kroon (2020). Fast/Robust Template Matching (https://www.mathworks.com/matlabcentral/fileexchange/24925-fast-robust-template-matching), MATLAB Central File Exchange. Retrieved .

chengkai qian

nice work, thx

Fabrizio Mure'

### Fabrizio Mure' (view profile)

Thank you for sharing, it's a very useful code.
May you write some comment for 2D normalized cross correlation I_NCC computation? I didn't find something similar to formula
I_NCC= 0.5+(Icorr-meanIT)./ (2*stdT*max(Idata.stdI,stdT/1e5));reported in your code.

Barry Wang

### Barry Wang (view profile)

ANDREA BELTRAMELLO

vivian wang

### vivian wang (view profile)

Thank you, but I want to know how to use this method in 3D points, the format is M*3, so how to change the M*3 to a*b*c dimension?

iqra rashid

### iqra rashid (view profile)

Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware
that exceeding your available stack space can crash MATLAB and/or your computer.

Error in imjpgbaselineinfo>recover_valid_marker
how to remove this error

bamboopu

bamboopu

### bamboopu (view profile)

Thank you. Can you help me? I want to know the mathematical theory of this code. Is there a paper to explain it?

Jayendra Bhalodiya

Subhan ahmed

Zarrin Wu

### Zarrin Wu (view profile)

Thanks for sharing! I want to use this code to develop 3D correlation. However, when I check your code with 2D image and compare the results with 'normxcorr2' code, they are different. And I have checked 'normxcorr2' gives the right result.

OpenBMaster

### OpenBMaster (view profile)

Yasin Ayhan Cimbek

shaojiang wu

### shaojiang wu (view profile)

Diluxshan Ariyanayagam

### Diluxshan Ariyanayagam (view profile)

Does this code detect the template which is slightly deformed from the original image?

huang samuel

### huang samuel (view profile)

very fast , i liked it

Tiziano

### Tiziano (view profile)

How can I allow in the 3D case the matching only in the z-direction (and so prohibit the matching the x- and y-direction)?

Daniel Bitencourt Pereira

jiajia

Konstantin

### Konstantin (view profile)

Excellent Code. Thank you.
I use it to find ellipses in a 3d image. The shape and the orientation of the ellipses are not exact equal. Is it possible to find ellipses which are similar (shape and orientation) to the template?

Jem

help me :(

2one

### 2one (view profile)

Excellent code but slight issue if you have a M x N x 3 3D grayscale image volume then the line:

if(size(T,3)==3)

will cause a problem. Suggested edit:

if(size(T,3)==3) & (size(I,3)==3)

Aaron T. Becker's Robot Swarm Lab

### Aaron T. Becker's Robot Swarm Lab (view profile)

Excellent work -- used this to track a blurry sphere in a video sequence.

Ilya Belevich

Mai

### Mai (view profile)

Is the 2D version of the normalized cross-correlation in this function the size as normxcorr2? If you have a paper describing this method, I would like to have it! Thank you!

Chun-Huai

Sara.Ezz

### Sara.Ezz (view profile)

Thanks so much. I was wondering how to change the code so it would detect more than one match Using Idata which I seem clueless on how to set it.

pham bao

### pham bao (view profile)

thanks for share. But I have a problem: "??? Maximum recursion limit of 500 reached. Use
set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack
space can

Error in ==> imbmpinfo>decodeCompression"
I'm using Matlab 7.8.0(R2009a)
Can you help me !

university of waterloo

### university of waterloo (view profile)

Does anyone know which paper or algorithm this code use?

Shashi

### Shashi (view profile)

Please indicate the paper you used to implement this. And added to it, if the template size is smaller than the image size, what changes are to be made in the code.

Florian

### Florian (view profile)

Please indicate that it requires the image processing toolbox or avoid using functions from the toolbox. padarray is used here and it's from the im.proc. toolbox.

Carlos

### Carlos (view profile)

Hi, You used any paper to develop this code? Can you give me the name of this paper?

Thanks a lot

Marcos Belmonte

### Marcos Belmonte (view profile)

The problem is that z is logical and I want it to be the number of the slice.

Marcos Belmonte

### Marcos Belmonte (view profile)

Hi, I have a problem with the NCCC three-dimensional, with the code I have written above, it plots a * in all the slices not only in the slices that match the template.
What should I do? Thanks in advance.

Marcos Belmonte

### Marcos Belmonte (view profile)

Hi, thanks for sharing your code, it's great. I have a 3D image with a sphere, and another sphere as a template. To locate the maximum cross correlation I write:
[I_SSD,I_NCC]=template_matching(T,I);
[x,y,z]=ind2sub(size(I_NCC),find(I_NCC==max(I_NCC(:))));
daspect([1 1 1]);
figure,isosurface(I,0); hold on; plot3(y,x,z,'r*'); title('MAX NCCC3D')

Is it correct?
I don't know if the real coordinates are displayed well because I don't understant very well the normalized cross-correlation in 3D.

Thanks very much.
Regards

Shahbaba2011

### Shahbaba2011 (view profile)

Thanx for sharing your code. How can I detect same template apping more than once in image.

Steffen

### Steffen (view profile)

I tried only the 3D normalized cross-correlation. It works very well. Thanks for sharing.

Dirk-Jan Kroon

### Dirk-Jan Kroon (view profile)

*Tony
The submitted function supports 3D, SSD and color which is not in the function 'normxcorr2'. Also normxcorr2 is only a little bit faster when the template is only a few pixels in size...

Tony

### Tony (view profile)

The function 'normxcorr2' available in matlab is faster than the code you submitted. Please profile it and see for yourself. But thanks for sharing the code.

Masoud

### Masoud (view profile)

Perfect. It is fast and robust.
Thank you

 22 Feb 2011 1.2.0.0 Faster Multiple Template search 5 Aug 2009 1.1.0.0 Fixed Range to 0..1
##### MATLAB Release Compatibility
Created with R2009a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux