Page 1 of 1
compare -metric NCC returns 0.866025 on identical images
Posted: 2014-04-18T11:28:52-07:00
by drzee3
I have a (large) number of png images for which 'compare -metric NCC' produces unexpected values:
$ compare -metric RMSE img.png img.png diff.png
0 (0)
$ compare -metric NCC img.png img.png diff.png
0.866025
In other words, when comparing the image with itself, the NCC is 0.866025 - and this should be 1.
After converting the image to jpg, the NCC that 'compare' returns, is indeed 1:
$ convert img.png img.jpg
$ compare -metric RMSE img.jpg img.jpg diff.jpg
0 (0)
$ compare -metric NCC img.jpg img.jpg diff.jpg
1
Clearly, there is a problem with the NCC calculation on the png image. I have run the same test using IM 6.6.9-7 on Ubuntu 12.04; as well as IM 6.8.2-6 running on OSX; same result.
This test image can be found here:
https://dl.dropboxusercontent.com/u/5709165/img.png
Re: compare -metric NCC returns 0.866025 on identical images
Posted: 2014-04-18T12:46:39-07:00
by fmw42
I can confirm that it fails for PNG, TIFF and MIFF, but works for JPG and GIF on IM 6.8.9.0 Q16 Mac OSX Snow Leopard
Code: Select all
compare -metric ncc img.png img.png null:
0.866026
compare -metric ncc img.tif img.tif null:
0.866026
compare -metric ncc img.jpg img.jpg null:
1
compare -metric ncc img.gif img.gif null:
1
compare -metric ncc img.miff img.miff null:
0.866026
Re: compare -metric NCC returns 0.866025 on identical images
Posted: 2014-04-18T12:49:10-07:00
by fmw42
Further information. The problem is associated with the alpha channel in the image. If the alpha channel is removed, it works fine.
Code: Select all
convert img.png -alpha off img2.png
compare -metric ncc img2.png img2.png null:
1
or
Code: Select all
compare -metric ncc -alpha off img.png img.png null:
1
However, this does not work to include the alpha channel
Code: Select all
compare -metric ncc -channel rgba -alpha on img.png img.png null:
0.866026
Re: compare -metric NCC returns 0.866025 on identical images
Posted: 2014-04-18T15:13:52-07:00
by magick
Normalized cross-correlation, for each pixel, subtracts the mean and divides by the standard deviation. Since this image has a constant alpha channel, it returns a NCC of zero for the channel. The remaining channels return an NCC of 1. The NCC of the composite channels is sqrt(3/4) or 0.866026. A pure black channel returns an NCC of 0. If we are not computing the composite of all channels properly, let us know. Note, Fred's script normcrosscorr, returns 0.997194 as the NCC for this image.
Re: compare -metric NCC returns 0.866025 on identical images
Posted: 2014-04-18T16:33:04-07:00
by fmw42
In my script, normcrosscorr, I test for zero std (constant color) regions, which causes a divide by zero, since the std is in the denominator, and set the contribution to the NCC to 1 for that region (pixel in the larger image). That works reasonably well for matching a small image to large image where the large image has constant regions the size of the smaller image, but means that comparing two different same size constant color images will produce 1 rather than 0. It is a trade-off that depends upon the kind of images and kind of comparison one wants to do.
In essences, the NCC is not a good measure when there are constant color images or channels.