Comparing different-sized images

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Comparing different-sized images

Post by Drarakel »

I first thought that comparing different-sized images doesn't work.. But apparently, it does - and it can create problems sometimes (e.g. when using compare in a script on a large set of files - like here). If the two images differ greatly in size, compare hangs for several minutes (or longer).
(By the way: In the paragraph about "-compose difference", it's still suggested that compare with different-sized images doesn't work at all - perhaps this could be rephrased..)

An example:

Code: Select all

convert -depth 8 -size 1000x1000 xc:yellow2 y2.png
convert -depth 8 -size 502x505 xc:yellow3 y3.png
compare -debug cache -metric ae y2.png y3.png test.gif
2010-08-04T11:04:36+02:00 0:00.016 0.000u 6.6.3 Cache Magick[3272]: cache.c/DestroyPixelCache/1606/Cache
destroy
2010-08-04T11:04:36+02:00 0:00.016 0.000u 6.6.3 Cache Magick[3272]: cache.c/OpenPixelCache/4121/Cache
open y2.png[0] (heap memory, 1000x1000 7.629MiB)
2010-08-04T11:04:36+02:00 0:00.063 0.047u 6.6.3 Cache Magick[3272]: cache.c/DestroyPixelCache/1606/Cache
destroy
2010-08-04T11:04:36+02:00 0:00.063 0.047u 6.6.3 Cache Magick[3272]: cache.c/OpenPixelCache/4121/Cache
open y3.png[0] (heap memory, 502x505 1.934MiB)
2010-08-04T11:04:36+02:00 0:00.078 0.063u 6.6.3 Cache Magick[3272]: cache.c/OpenPixelCache/4121/Cache
open y2.png[0] (heap memory, 499x496 1.888MiB)

This is the last message for some time - I usually have to abort the process here. And this is a problem when using compare in a script.
I mean, this ability to compare an image with a potential sub-image is surely helpful in some cases.
But: Is there an option to turn this feature off? If no, then perhaps such an option could be created? :D

I'm also getting two images in the output GIF with a command like that (if the overall size is smaller and if the sizes of the two images don't differ that much, it doesn't take that long). Is that correct? The first image looks ok, but the second image looks odd - it has only the dimensions of the 'size difference' of the two images (in the above example, this second image would have the dimensions 499x496).

Running IM v6.6.3-2 Q16, on Windows XP.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Comparing different-sized images

Post by fmw42 »

I hope that I am interpreting your questions correctly. Forgive me if I state something that is obvious to you.

First, I think there must be a typo on http://www.imagemagick.org/Usage/compare/#difference as -compose difference must be followed with -composite. I make this mistake myself from time to time. I have just sent Anthony a note about this typo.

compare with two different size images is not the same nor does it use exactly the same syntax as with just same size images. Compare with different size images looks for the best matching subsection of the larger image that matches closest to the smaller image. Thus it does a compare for every possible position of the small image in the larger one. Thus it can be VERY slow. Two output images are generated, the difference image and the match score image. You give one output name and it generates two outputs (-0 and -1) or two frames depending upon the output format. The second image/frame is useful for locating the best match. For comparing images with different sizes, see my example at viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076 and the documentation at http://www.imagemagick.org/script/compare.php

Two same size image compares has the following syntax:

compare -metric rmse image1 image2 null:

This reports the match score which comes from an rmse of the red, green, and blue pixel value differences. The metric can be changed.

compare image1 image2 difference

produces an image which shows where the images are different.

and

compare -metric rmse image1 image2 difference

returns the match score and the difference image

With two images that are of different size, the syntax is

compare -metric rmse largeimage smallimage resultimage

which produces resultsimage-0 or frame [0] (an image that shows the difference, but is not too useful) and resultsimage-1 or frame [1], which shows the rmse value for each offset of the small image with respect to the larger image.

Note there is now an option -dissimilarity-threshold that can be used when the two images are very different. If the compare sees a result at a given offset that is larger than this value, then it aborts with a message about the images being too dissimilar. But if you increase the dissimilarity threshold to 100%, then it will run successfully. see http://www.imagemagick.org/script/comma ... -threshold

convert -size 200x200 xc:white white200.gif
convert -size 100x100 xc:black black100.gif

compare -metric rmse white200.gif black100.gif tmp.gif
compare: images too dissimilar `white200.gif' @ error/compare.c/CompareImageCommand/926.

vs

compare -metric rmse -dissimilarity-threshold 100% white200.gif black100.gif tmp.gif
32767.5 (0.5) @ 0,0


As far as I know there is no way to turn off different image size matching and if you could it would be meaningless. There is no way to do one set of pixel-by-pixel comparisons with images that are different sizes.

If you have two images that are different sizes and don't want to do the compare, then I think the best thing if scripting is to test the size of the two images first and then skip the compare if that is what you are trying to do.

However you could also make it still work in both cases by providing a subsection of the first image that is the same size as the second image. Then it will only make one comparison. So for example:

compare metric rmse image1[subsection to size of image2] image2 null:

If the two images are the same size, then the subsection will do no harm. If the images are different sizes, then the subsection will find the single match score between the second image and the specied subsection of the first image. But I don't know if this is really what you want to do as it may not satisfy the problem in the rest of the first image.

If you have images of one color that you are testing, but the images are not always the same size (as per the link to the other post), then compare is not the function for that purpose. I think the better approach is to simply look at the std and test if 0 (for a solid color) and then compare the mean values of each channel to the colors wanted. This is probably going to be faster anyway than trying to use compare. see my post at viewtopic.php?f=1&t=16774#p61954

One can also do the different size image compare using normalized cross correlation via FFT from my script normcrosscorr, see examples at http://www.fmwconcepts.com/imagemagick/ ... mcrosscorr. This is often faster than using compare. but requires HDRI compile of IM.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Comparing different-sized images

Post by Drarakel »

fmw42 wrote:First, I think there must be a typo on http://www.imagemagick.org/Usage/compare/#difference as -compose difference must be followed with -composite.
I was referring to this sentence in the text about "compose -composite difference":
http://www.imagemagick.org/Usage/compare/#difference
Note that as the 'difference' compose method is associative, the order of the two images in the above examples does not matter, although unlike "compare", you can compare different sized images, with the destination image determining the final size of the difference image.
Looks like that (highlighted) detail is outdated?
I don't see a typo - but perhaps it already got corrected.

Regarding your explanations and examples about comparing with different sizes and about the second output image: It make more sense now. Thanks!
fmw42 wrote:As far as I know there is no way to turn off different image size matching and if you could it would be meaningless.
Not for me.

I know that with one-color images, there are some alternatives - I don't have to use compare with such images. But that was only an example.
And yeah, I could use some workarounds if I don't want compare to process images with different size. I could decrease the dissimilarity-threshold to avoid some cases.. Or I could set a time limit in ImageMagick (but this isn't working reliably at the moment - at least on Windows). Or I could check the sizes with identify before 'deciding' whether to use compare on these files. Or I could use this "image1[subsection to size of image2]" variant. (I didn't think of that last variant before. Could be useful sometimes. Thanks!) But it would be much simpler, if one could specify an additional option like that (in examples where one uses compare in a script that loops over a whole lot of images with some different sizes):
compare -only_same_size image1 image2 outputimage

Then I would get the "Magick: image size differs" message in the cases where the second image is smaller than the first image, too (and not only in the cases where the first image is smaller than the second one). :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Comparing different-sized images

Post by fmw42 »

I don't see a typo - but perhaps it already got corrected.
The first and third examples are missing -composite after the -compose difference. That is all I was getting at there.


when I wrote:
"As far as I know there is no way to turn off different image size matching and if you could it would be meaningless."
I meant there were no current arguments or options now within compare to do so :wink: and if one did, then trying to do a same size compare on different size images would not be meaningful. Of course, one could output a message if trying to do that illegal compare, if some argument or set option were provided as you suggest.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Comparing different-sized images

Post by Drarakel »

fmw42 wrote:The first and third examples are missing -composite after the -compose difference. That is all I was getting at there.
The first and the third example have "composite -compose difference" (not "convert -compose difference ..."). :wink:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Comparing different-sized images

Post by fmw42 »

Drarakel wrote:
fmw42 wrote:The first and third examples are missing -composite after the -compose difference. That is all I was getting at there.
The first and the third example have "composite -compose difference" (not "convert -compose difference ..."). :wink:

You are right. I don't use composite much and overlooked that at the beginning of the command. :oops:
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Comparing different-sized images

Post by anthony »

Drarakel wrote:
fmw42 wrote:But it would be much simpler, if one could specify an additional option like that (in examples where one uses compare in a script that loops over a whole lot of images with some different sizes):
compare -only_same_size image1 image2 outputimage

Then I would get the "Magick: image size differs" message in the cases where the second image is smaller than the first image, too (and not only in the cases where the first image is smaller than the second one). :)
I myself would not see a problem with this but would reverse it.

Make compare only compare difference sized images if say a "-subimage_search" is given.

That way the default action is "same sized images only" and you will never get two images unless you specify that that
is what you are doing. Note it will also mean that doing a sub-image search with two images the same size, will result in a single pixel 'second image'. :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Comparing different-sized images

Post by fmw42 »

Anthony wrote:I myself would not see a problem with this but would reverse it.

Make compare only compare difference sized images if say a "-subimage_search" is given.
It is OK with me (and probably the better approach). However, I would have to modify some scripts and some examples in my Fourier pages. With the other way, I might not have to modify my scripts.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Comparing different-sized images

Post by fmw42 »

Anthony wrote:That way the default action is "same sized images only" and you will never get two images unless you specify that that
is what you are doing. Note it will also mean that doing a sub-image search with two images the same size, will result in a single pixel 'second image'.
You mean in the second line -- if you specify the flag to compare different size images and only have same size images, then you get a single pixel second image.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Comparing different-sized images

Post by anthony »

fmw42 wrote:
Drarakel wrote:
fmw42 wrote:The first and third examples are missing -composite after the -compose difference. That is all I was getting at there.
The first and the third example have "composite -compose difference" (not "convert -compose difference ..."). :wink:

You are right. I don't use composite much and overlooked that at the beginning of the command. :oops:
Perhaps I should start NOT using "composite" in examples. The only thing "composite" can do that "convert" cant do directly is 'composite -tile' where the source image is tiled into the destination image. See http://www.imagemagick.org/Usage/compose/#tile

Actually I am not even certain how IM is doing this now, as years and years ago it was done by a special routine, but now could be done with 'virtual pixel' effects. Using virtual pixel as a composition OPTION would also allow much more advanved compositions. For example mirror tile compose, or even checkerboard tile overlay (with background or 'none').

It would have to be a expert option though, but will mean the last 'feature' of "composition" would then be built into "convert". I have added it to my ToDo list, but I have no idea when my next 'programming phase' will start. I have too many other things on my plate at the moment.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Comparing different-sized images

Post by Drarakel »

anthony wrote:I myself would not see a problem with this but would reverse it.

Make compare only compare difference sized images if say a "-subimage_search" is given.
Sure. That would be great, too! :D
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Comparing different-sized images

Post by magick »

Look for the -subimage-search option in ImageMagick 6.6.3-5 beta available by sometime tomorrow. Thanks.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Comparing different-sized images

Post by Drarakel »

Thanks!
I guess, "6.6.6-4 beta" was a typo, right? :D Anyway, it's not yet included in v6.6.3-4.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Comparing different-sized images

Post by fmw42 »

Drarakel wrote:Thanks!
I guess, "6.6.6-4 beta" was a typo, right? :D Anyway, it's not yet included in v6.6.3-4.
Magick said above 6.6.3-5 beta not 6.6.6-4 (unless he edited it)
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Comparing different-sized images

Post by Drarakel »

fmw42 wrote:he edited it
:wink:

Thanks again!
Post Reply