Page 1 of 1
Comparing image similarity with offset
Posted: 2014-05-29T01:57:26-07:00
by sabrehagen
Hi,
I have two images. One is a screenshot of a desktop (source image), and the other is a screenshot of the same desktop, but through a remote access window (to-be-compared image). I need to compare the two images and work out what percentage of the image is the same. My issue is that an offset is created by the remote access window. The title bar of the remote access window creates an offset of 20 pixels or so in the second image, and the task bar cuts of almost 30 pixels from the bottom of the original image. I know I can trim the second image and do a sub image search, but this is not a route I'd like to take.
Is there a way to use imagemagick to compare the two images and work out what percentage of them is the same, even with the offset? Obviously the percentage by which they are the same would be a minimum of 90% or so given the addition of the title and footer bars, and the cut off of the original image.
Here's the source:
And here's the image to be compared:
Thanks for your help!
Re: Comparing image similarity with offset
Posted: 2014-05-29T03:21:09-07:00
by snibgo
The vertical offset is approximately 21 pixels. Is it always 21?
And what do you mean by "the same"? Pixels that look the same actually aren't, exactly.
Perhaps:
Code: Select all
convert m98sKS2.png LMFIygL.png -geometry +0-21 -compose Difference -composite x.png
convert m98sKS2.png LMFIygL.png -geometry +0-21 -compose Difference -composite -fuzz 10% -fill #fff +opaque #000 -format "%[fx:mean]" info:
0.0754876
The first command shows what is the same. Black pixels means they are the same.
The second command shows the proportion that are different. 7.584876 percent of pixels become white, so this percentage is at least 10% different.
Re: Comparing image similarity with offset
Posted: 2014-05-29T17:10:32-07:00
by sabrehagen
Thanks, I was interested to see the way you'd do it more for the experiment than the logic. It was interesting to see the way you combined the commands to achieve my spec; a good imagemagick learning experience
The logical way was the way I did it myself, and the way I purposely ruled out in my question above. I used the two commands:
Code: Select all
convert remote.bmp -crop 900x721+0+21 output.bmp
compare -metric rmse -subimage-search source.bmp output.bmp results-%d.bmp
1796.32 (0.02741) @ 0,0
I guess now I need some help interpreting the output. Does the above output mean that there are 1796 pixels different, which is 2.7% of the image, and the subimage was found at offset 0,0?
Re: Comparing image similarity with offset
Posted: 2014-05-29T18:33:25-07:00
by fmw42
FYI, this should work in one command, though is not what you were asking about.
Code: Select all
compare -metric rmse -subimage-search source.bmp remote.bmp[900x721+0+21] results-%d.bmp
The use of -compose difference, as described by user snibgo, is the most logical way to get the information you need. But I thought I would point out one way to combine your two commands.
Re: Comparing image similarity with offset
Posted: 2014-05-29T19:10:05-07:00
by sabrehagen
Thanks for that Fred, great to have a nice one liner. Are you able to assist with understanding the meaning of the output of that command?
Re: Comparing image similarity with offset
Posted: 2014-05-29T19:21:19-07:00
by fmw42
The first file will be the difference image with differences highlighted in red for the matching subregion. I believe the red is binary -- clear where a perfect match and red otherwise. It is blended with a contrast reduced version of the subsection or smaller image (not sure which without further testing). Thus if you want to see the red intensity change with difference, you should use -compose difference and composite that with the image and red.
The second file will be as large as the large image minus the dimensions of the small image. It's brightness represents the match score for each shift position of the small image relative to the large image for the upper left corner of the small image relative to the upper left corner of the large image. Brighter always means better match, regardless of the metric. (Some metrics like rmse are better when the value is smaller, so the match score image will be negated)
Is that what you are asking?
see
http://www.imagemagick.org/Usage/compare/
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compose/#difference
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
And my scripts: normcrosscorr, rmsecorr, dotproductcorr at the link below. They are very fast, but require the use of HDRI because they use FFT (real/imaginary components).
Re: Comparing image similarity with offset
Posted: 2014-05-30T01:27:33-07:00
by snibgo
sabrehagen wrote:compare -metric rmse -subimage-search source.bmp output.bmp results-%d.bmp
1796.32 (0.02741) @ 0,0
Does the above output mean that there are 1796 pixels different, which is 2.7% of the image, and the subimage was found at offset 0,0?
IM calculates a score for every possible position of output.bmp within source.bmp. For metric RMSE, the score is the square root of the mean square difference of the pixels. The best position is where this score is lowest.
"1796.32 (0.02741) @ 0,0" says the best score occurred at position 0,0 (which means where the images align at top-left corners), and the score was 1796.32 (out of 65535), which is 0.02741 (out of 1.0). The score is RMSE, not a pixel count.
"-metric AE" would give a pixel count, but the images are too dissimilar.
See
http://www.imagemagick.org/script/comma ... php#metric