This is a cut at a grayscale Normalized Cross Correlation Compare of two images of the same size. (From mathematics at
http://en.wikipedia.org/wiki/Cross-corr ... orrelation)
Bad values are near 0 and good values near 1. A perfect match should be 1. Change infile1 and infile2 to your two images.
To be more accurate, you could do each channel separately and then get the rms combination of the 3 channels rms=sqrt(ncc-r^2 + ncc-g^2 + ncc-b^2)/sqrt(3)
For your thumbnail images, I got:
a-1 vs b-1: 0.880103
a-2 vs b-2: 0.309664
a-3 vs b-3: 0.239163
a-4 vs b-4: 0.8076
a-1 vs a-1: 1.00407 (perfect match but slight error as it should not be larger than 1)
a-1 vs the negated version of a-1 (white is black and black is white): 0 (no correlation)
infile1="a-1.jpg"
infile2="b-1.jpg"
inname1=`convert $infile1 -format "%t" info:`
inname2=`convert $infile2 -format "%t" info:`
convert $infile1 -type grayscale ${inname1}g.png
convert $infile2 -type grayscale ${inname2}g.png
n1a=`convert ${inname1}g.png -format "%[fx:w*h]" info:`
n1b=`convert ${inname2}g.png -format "%[fx:w*h]" info:`
if [ $n1a -ne $n1b ]; then
echo "--- IMAGES NOT SAME SIZE ---"
else
m1a=`convert ${inname1}g.png -format "%[fx:mean]" info:`
m1b=`convert ${inname2}g.png -format "%[fx:mean]" info:`
s1a=`convert ${inname1}g.png -format "%[fx:standard_deviation]" info:`
s1b=`convert ${inname2}g.png -format "%[fx:standard_deviation]" info:`
if [ "$s1a" = "0" -o "$s1b" = "0" ]; then
echo "--- ONE OR BOTH IMAGES ARE CONSTANT VALUES (NCC=0/0) ---"
ncc=0
else
num=`convert ${inname1}g.png ${inname2}g.png -fx "(u-$m1a)*(v-$m1b)" miff:- |\
convert - -format "%[fx:mean]" info:`
ncc=`convert xc: -format "%[fx:$num/($s1a*$s1b)]" info:`
fi
fi
echo "ncc=$ncc"
Indenting for clarity:
Note due to the use of FX it will be slower for full size images. The thumbnails were adequately fast.
However, here is another version that avoids fx and so should be faster for large images.
infile1="1a.jpg"
infile2="1b.jpg"
inname1=`convert $infile1 -format "%t" info:`
inname2=`convert $infile2 -format "%t" info:`
convert $infile1 -type grayscale ${inname1}g.png
convert $infile2 -type grayscale ${inname2}g.png
n1a=`convert ${inname1}g.png -format "%[fx:w*h]" info:`
n1b=`convert ${inname2}g.png -format "%[fx:w*h]" info:`
if [ $n1a -ne $n1b ]; then
echo "--- IMAGES NOT SAME SIZE ---"
else
m1a=`convert ${inname1}g.png -format "%[fx:mean]" info:`
m1b=`convert ${inname2}g.png -format "%[fx:mean]" info:`
m1ab=`convert xc: -format "%[fx:$m1a*$m1b]" info:`
s1a=`convert ${inname1}g.png -format "%[fx:standard_deviation]" info:`
s1b=`convert ${inname2}g.png -format "%[fx:standard_deviation]" info:`
if [ "$s1a" = "0" -o "$s1b" = "0" ]; then
ncc=0
else
num=`convert ${inname1}g.png ${inname2}g.png -compose mathematics \
-set option:compose:args "1,-$m1a,-$m1b,$m1ab" -composite miff:- |\
convert - -format "%[fx:mean]" info:`
ncc=`convert xc: -format "%[fx:$num/($s1a*$s1b)]" info:`
fi
fi
echo "ncc=$ncc"