View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Fast/Robust Template Matching

4.7 | 16 ratings Rate this file 133 Downloads (last 30 days) File Size: 13.4 KB File ID: #24925 Version: 1.2
image thumbnail

Fast/Robust Template Matching


Dirk-Jan Kroon (view profile)


04 Aug 2009 (Updated )

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

| Watch this File

File Information

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.


This file inspired Microscopy Image Browser (Mib).

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
02 Jan 2017 Tiziano

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)?

25 Nov 2016 Daniel Bitencourt Pereira

09 Nov 2016 jiajia

jiajia (view profile)

22 Oct 2016 Konstantin

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?

22 May 2016 Jem

Jem (view profile)

help me :(

08 Mar 2016 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:


will cause a problem. Suggested edit:

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

09 Aug 2015 Aaron T. Becker's Robot Swarm Lab

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

20 May 2015 Ilya Belevich

27 Sep 2014 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!

27 Feb 2013 Chun-Huai

07 Nov 2012 Sara.Ezz

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.
Thanks in advance

Comment only
01 May 2012 pham bao

thanks for share. But I have a problem: "??? Maximum recursion limit of 500 reached. Use
to change the limit. Be aware that exceeding your available stack
space can
crash MATLAB and/or your computer.

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

Comment only
14 Apr 2012 university of waterloo

Does anyone know which paper or algorithm this code use?

Comment only
14 Mar 2012 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.

Comment only
23 Feb 2012 Florian

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.

Comment only
16 May 2011 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

Comment only
27 Dec 2010 Marcos Belmonte

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

27 Dec 2010 Marcos Belmonte

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.

13 Dec 2010 Marcos Belmonte

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:
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.

09 Dec 2010 Shahbaba2011

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

Comment only
09 Apr 2010 Steffen

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

29 Mar 2010 Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)

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...

Comment only
11 Oct 2009 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.

05 Sep 2009 Masoud

Masoud (view profile)

Perfect. It is fast and robust.
Thank you

05 Aug 2009 1.1

Fixed Range to 0..1

22 Feb 2011 1.2

Faster Multiple Template search

Contact us