File Exchange

image thumbnail

Generalized Normalized Cross Correlation

version (5.3 KB) by Dirk Padfield
Computes the correct NCC at all locations regardless of the relative size of A and TEMPLATE


Updated 19 Apr 2012

View Version History

View License

normxcorr2_general computes the normalized cross-correlation of matrices TEMPLATE and A. The resulting matrix C contains correlation coefficients and its values may range from -1.0 to 1.0.

Limitations of normxcorr2:
The documentation of normxcorr2 states that, "The matrix A must be larger than the matrix TEMPLATE for the normalization to be meaningful." It is implemented following the details of the paper "Fast Normalized Cross-Correlation", by J. P. Lewis, Industrial Light & Magic. This approach assumes the template is small relative to the image and proceeds to calculate the normalization across the entire template. This leads to correct computations wherever the template is wholly overlapping with the image, but the computation is incorrect in the borders of the output (the border size is proportional to the template size). This problem is therefore worse for larger templates to the point that, when the template is the same size as the image, the only correct value is at the center pixel (where the images are fully overlapping). Thus, if normxcorr2 is used for such things as registering images of the same size, the result will be incorrect.

The new normxcorr2_general:
normxcorr2_general is more general than normxcorr2 in that it gives correct results everywhere regardless of the relative size of A and TEMPLATE. It accomplishes this by computing the normalized correlation only in the overlap regions between the two matrices. Thus, the result is correct for all locations of correlation. The result is the same as if the NCC were carried out in the spatial domain (which would take a long time to compute for large matrices).

Cite As

Dirk Padfield (2020). Generalized Normalized Cross Correlation (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (22)


Hard to believe this basic functionality has been overlooked in Matlab. Was pulling my hair out with clearly incorrect registrations prior to using this, thank you.

Loïc Toraille

Dirk Padfield

In response to Y Fu and wen-feng Huang, you are right that I originally only made available the translation code of the masked registration algorithm; this was primarily because that was the novel contribution of the paper. I have now uploaded the masked translation, rotation, and scale registration code at

Dirk Padfield

In response to Brian Sweis, with regard to your masking question, I wrote a TIP paper "Masked object registration in the Fourier domain" on how to quickly compute masked correlation that you can find along with the code at This approach/code enables you to specify a mask with any arbitrary pixels turned on that you want, and all pixels that are turned off will be ignored in the computation. The code does not include a "maxLag" parameter, but the only purpose that I can think of for such an option is to reduce the computation time. Since the masked correlation algorithm computes the correlation map in the Fourier domain, the computation time is not an issue. You can just crop the correlation map to the desired size after the computation. But if there is some other need for maxLag, please let me know.

Loïc Toraille

Brian Sweis

Two requests: (1) any way for this to handle NaNs like the original corr with 'rows','complete? and (2) any way to specify the number of lags in either the column and / or row directions separately as opposed to doing the entire dimensions of the two matricies like the original xcorr MaxLag?

Daniel Andre


wen-feng Huang

the same issue as Y Fu. I try to estimate the spatial transformation (translation, rotation, and scale) between images. However, the code on your website only return the translation parameter. Could you please release the source code? Thanks for your reply and this excellent work.


The values of TEMPLATE cannot all be the same.
I get this error and I don't know how to get around

Y Fu

Dear Professor
My research is focused on Image Registration. I have recently read your paper "Masked Object Registration in the Fourier Domain ". I find a part of coda on your website However, the code can only get translate parameters between images. I try to validate your simulation results(translation,rotation and scale between images) in my study, but no progress. I am wondering if you could kindly send me all the sources source program and the necessary information about this paper. I promise they will be used only for research purposes.
Thank you very much for your kind consideration and I am looking forward to your early reply.

Leo Shi

Hysis Shepard


Dirk Padfield

In response to Karlien's question, I answered this in detail in the paper "Masked Object Registration in the Fourier Domain", which you can find on my website


Works well. Could you comment on why you are using the rotated template to calculate the local sums of T and T^2? This isn't completely clear to me from the paper of J.P. Lewis. I do understand you need to use the rotated template in the convolution but why should you use it in the local sum calculations as well? Thanks in advance for your answer.

Ghulam Rasool

Robert McLeod

Excellent, better than the built-in normxcorr2


How to Run this Code????
I'm new to this , please some one help me ????


Please indicate that the Image Processing Toolbox is required as it uses functions thereof (at least iptchecknargin).

John B.

Seems to work well. Seems faster than normxcorr2_mex for larger arrays, which is no longer on Matlab Central but is around on the internet.

J. Kristian Sveen

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!