possible bug compare -metric ncc IM 6.8.6.9 Q16

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

possible bug compare -metric ncc IM 6.8.6.9 Q16

Post by fmw42 »

I have recently fixed (the normalization issue) in my script, normcrosscorr, that does normalized cross correlation in the real-imaginary FFT domain. I tried to compare its results and timing to compare -metric ncc and found that under certain circumstances compare -metric ncc gives erroneous match locations.

The circumstances are when the large image has a totally flat (constant) color the size of the small image such as the upper right corner of the large image where it is perfectly flat white. The standard deviation for that subsection will be zero causing a divide by zero in the formulae.

In actuality, in my FFT NCC, the divide by zero is handle OK, but values within some threshold are the ones that are causing the issue. I might imagine that this will be the same for the compare -metric ncc.

ncc=(Small-MeanS)*subimage((Large-MeanL))/(Ns*StdS*subimage(StdL))

where subimage(StdL) is the standard deviation of the subimage in the large image of size equal to the small image.

See http://en.wikipedia.org/wiki/Cross-correlation for the full expression of the NCC.

Any way, I just fixed my FFT version by testing the standard deviation of the subsection to see if it is too close to zero (both positive and negative differences).

I used -fx "(abs(u)<0.002)?1:u", which worked fine.

I reimplemented this via a mask composition with a white image to avoid using the slower -fx. However, this might be a nice new function (a bit similar to -perceptible, except it would have a user specified value to set the result to in addition to the threshold value.

In any case, I just wanted to let you know about this. Here are my tests.

Large Image: wizardsq.png
Image

Small Image (subsection 50x50+130+80) : wizard_sub_130_80.png
Image


FFT NCC: normcrosscorr

time imh normcrosscorr wizard_sub_130_80.g wizardsq.png null:

Match Coords: (130,80) And Score In Range 0 to 1: (1.00003)

real 0m20.700s
user 0m17.173s
sys 0m4.012s


Compare NCC:

time compare -metric ncc -subimage-searchizardsq.png wizard_sub_130_80.png null:

0 @ 196,0

real 4m1.259s
user 4m0.775s
sys 0m0.435s


Compare RMSE:

time compare -metric rmse -subimage-search wizardsq.png wizard_sub_130_80.png null:

0 (0) @ 130,80

real 0m51.298s
user 0m50.964s
sys 0m0.326s
Post Reply