compare -metric NCC returns 0.866025 on identical images

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
drzee3
Posts: 1
Joined: 2014-04-18T11:10:42-07:00
Authentication code: 6789

compare -metric NCC returns 0.866025 on identical images

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compare -metric NCC returns 0.866025 on identical images

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compare -metric NCC returns 0.866025 on identical images

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: compare -metric NCC returns 0.866025 on identical images

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compare -metric NCC returns 0.866025 on identical images

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