Page 1 of 1

ImageMagick - use compare operation to generate diff image

Posted: 2016-06-02T06:37:46-07:00
by iKen
Hi everybody,

I want to use "compare" operation to compare two images with -subimage-search option and generate the diff image with the following command:

Code: Select all

compare -subimage-search big_image.png small_image.png diff.png
Image

But the diff image is not good enough if compare with http://huddle.github.io/Resemble.js/

Here is the result of diff image that is generated by Resemble.js

Image

Any advice would be greatly appreciated.

Re: ImageMagick - use compare operation to generate diff image

Posted: 2016-06-02T09:27:45-07:00
by fmw42
This is because IM draw red over the areas of any difference. This is similar to what you get from your js tool when you select ignore nothing.

You also did not select -metric and I am not sure what the default metric is.

You can get a better result by getting the match coordinates, cropping the large image at those coordinates of the size of the small image and then using -compose difference -composite with the small image. This allow you to see the amount of the changes. You can then threshold out as much as you want similar to the js ignore less and compose that over your original. See http://www.imagemagick.org/Usage/compare/.

See also the -compose src options at that link.

If you post your two input images, we could give you better pointers or code.

Here is an example of a compare without the subimage-search for two equal sized images. But you can do the equivalent from what I said above for a case of sub-image search by getting the coordinates and then cropping and doing the following.

I have taken the same two images from the js example:

Image
Image

Code: Select all

compare -metric rmse People.jpg People2.jpg people_compare.png
Image

Code: Select all

convert People.jpg \
\( -clone 0 People2.jpg -compose difference -composite \
-threshold 5% -fill red -opaque white -transparent black \) \
-compose over -composite people_compare2.png
Image

Adjust the 5% in the threshold as desired.

Re: ImageMagick - use compare operation to generate diff image

Posted: 2016-06-02T13:51:09-07:00
by snibgo
In additon to what Fred says, the output from "compare -subimage-seach" is two images. The first shows red (or whatever colour you want) where pixels are at all different. The second is grayscale, showing how well the subimage matched at that position. White means exact match, black means as different as can be.

Re: ImageMagick - use compare operation to generate diff image

Posted: 2016-06-02T14:51:36-07:00
by fmw42
I will add something else. compare is very slow for large images. So there are two things you can do:

1) Use multi-resolution processing. That is, scale the images down and do a compare. Get the coordinates and scale them up. Then crop the large image at those coordinates plus some buffer and do a full resolution compare.

Here is an example (unix syntax) for doing that and then the above processing using the same images.

First we extend the one image with gray to make it larger so that -subimage-search is needed for the compare

Code: Select all

convert People.jpg -gravity center -background gray -extent 750x750 People_big.jpg
Image

Next we scale the images down to 1/4 size and do the compare and scale up the coordinates by 4

Code: Select all

coords=`convert People_big.jpg People2.jpg -resize 25% miff:- |\
compare -metric rmse -subimage-search - null: 2>&1 | tr -cs "0-9.," " " | sed 's/ [ ]*/ /g' | cut -d\  -f3`

xx=`echo "$coords" | cut -d, -f1`
yy=`echo "$coords" | cut -d, -f2`
xx=$((4*xx-10))
yy=$((4*yy-10))
ww=520
hh=520
Then we crop the large image at those coordinates plus some buffer (10 on all sides) and compare again. Note, it probably would have been better to make the buffer a multiple of scale, so perhaps 16 or 20, or the equivalent of 4 or 5 pixels at the scaled down size)

Code: Select all

coords=`convert People_big.jpg -crop ${ww}x${hh}+${xx}+${yy} +repage miff:- |\
compare -metric rmse -subimage-search - People2.jpg null: 2>&1 | tr -cs "0-9.," " " | sed 's/ [ ]*/ /g' | cut -d\  -f3`
xx2=`echo "$coords" | cut -d, -f1`
yy2=`echo "$coords" | cut -d, -f2`
ww2=500
hh2=500
xx2=$((xx2+xx))
yy2=$((yy2+yy))
Then we crop the large image to the same size as the small image at those coordinates and do the difference processing with the threshold.

Code: Select all

convert People_big.jpg -crop ${ww2}x${hh2}+${xx2}+${yy2} +repage \
\( -clone 0 People2.jpg -compose difference -composite \
-threshold 5% -fill red -opaque white -transparent black \) \
-compose over -composite people_compare3.png
Image

Note that snibgo's web site at http://im.snibgo.com/ has similar processing scripts as above for Windows.

2) One can use my FFT correlation scripts, normcrosscorr or rmsecorr, if you are on Linux, Mac OSX or Windows with Cygwin and have IM compiled in HDRI mode (default for IM 7). See my links below. Those will speed up the full resolution compare by about 2 orders of magnitude. Then do the difference processing as above. See my link below for the scripts and more details at http://www.fmwconcepts.com/imagemagick/ ... mcrosscorr and the paper linked below that.

NOTE: it is always a good idea to identify your IM versions and platform when asking questions on this forum, since syntax may differ.

Re: ImageMagick - use compare operation to generate diff image

Posted: 2016-06-02T16:10:55-07:00
by fmw42
See viewtopic.php?f=2&t=29827, which shows an existing solution for the amount of color to overlay directly in compare. Suggested by snibgo