Normalized Cross Correlation (NCC) is an excellent choice for finding a given pattern in an image given a known scale and orientation. Matlab's IP toolbox normxcorr2 function does this.
However there's no way to specify which pixels of the pattern are important. This is particularly problematic because the template (i.e. pattern) matrix inadvertently specifies a rectangular ROI. So if the pattern as it appears in the image has artifacts close enough to the pattern, it will be in range of the template window and so the response will be degraded. Assuming that the nature of the problem is such that such artifacts are not actually important to the pattern match, being able to de-weight pixels away from the pattern can mitigate this.
Note: This function is more expensive than normxcorr2. It uses 3 convolutions (technically correlation) between image and template sized matrices. The convolution implementation automatically switches between frequency and spatial domains and so the time is usually “reasonable” .
The WNCC formula (but not the implementation) is the same as NCC but it with weighted variance vs regular variance, i.e.
NCC = cov(x,y)/sqrt(cov(x,x)*cov(y,y))
WNCC = wcov(x,y,w)/sqrt(wcov(x,x,w)*wcov(y,y,w))
Where “wcov” is the weighted covariance w/r to the weights “w”
(see the comments in wncc for details)
In the example (picture) above, the “template” (i.e. pattern) is a cross. The “image” has such a pattern but within the rectangular region that contains it there are two bright artifacts that degrade the performance of NCC (bottom right). The “template weights” image shows that the WNCC matching will largely ignore anything not very close to a cross shaped ROI. In particular, this weighting will allow the artifacts to be ignored. The improved result (note colorbars) is shown in the WNCC image (bottom left).
Tested in 2008a, 2008b, 2008b, 2011a
Andrew Diamond (2023). WNCC - Weighted Normalized Cross Correlation (https://www.mathworks.com/matlabcentral/fileexchange/33340-wncc-weighted-normalized-cross-correlation), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!