Compare, including alpha channel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Caleb9849
Posts: 7
Joined: 2012-06-14T09:01:48-07:00
Authentication code: 13

Compare, including alpha channel

Post by Caleb9849 »

Hi folks,

I'd like to compare two images on the alpha channel as well as the RGB channels -- I don't care about getting an output image, but only a numerical output that is non zero if there is any difference in the red, green, blue, and/or alpha channel from one image to the next. I've tried all sorts of command line options for "compare" and "composite" and I'm just not quite getting exactly what I want. Is there a way I can do this?

Thanks in advance :)
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Compare, including alpha channel

Post by glennrp »

Use the signature:

Code: Select all

identify -verbose image1 | grep igna
identify -verbose image2 | grep igna
You could write the entire word "signature" but I always
forget whether it has the upper or lower case "s".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Compare, including alpha channel

Post by fmw42 »

Caleb9849 wrote:Hi folks,

I'd like to compare two images on the alpha channel as well as the RGB channels -- I don't care about getting an output image, but only a numerical output that is non zero if there is any difference in the red, green, blue, and/or alpha channel from one image to the next. I've tried all sorts of command line options for "compare" and "composite" and I'm just not quite getting exactly what I want. Is there a way I can do this?

Thanks in advance :)
Try -metric fuzz

see

http://www.imagemagick.org/Usage/compare/#statistics
Caleb9849
Posts: 7
Joined: 2012-06-14T09:01:48-07:00
Authentication code: 13

Re: Compare, including alpha channel

Post by Caleb9849 »

Thanks for the replies. I don't believe I was clear about what I need; I apologize. Please let me try to elucidate what I am after :)

I want to compare the entire image, pixel-by-pixel. However, when I do this, I don't want to "skip" transparent pixels (I *think* that's what's happening, but I may be misinterpreting what's going on). Rather, I want it to treat the alpha channel as one of the things to be compared, just like the red, green, and blue. To exemplify, I have two images, image1.tif and image2.tif. The content of these files is *exactly* the same:

Code: Select all

 --> diff image1.tif image2.tif
 --> file image1.tif
image1.tif: TIFF image data, little-endian
 --> file image2.tif
image2.tif: TIFF image data, little-endian
 --> diff image1.tif image2.tif
 -->
^ No output from diff, so the file content is exactly the same, in both the headers and the pixel data itself (I know this without even running diff, as I generated the two files the same way with no randomness involved). However, observe the result when I run this command, which I understand should give me a number signifying the amount of difference between the two images:

Code: Select all

 --> convert image1.tif image2.tif -compose Difference -composite -format '%[fx:mean*100]' info:
33.2047
??!! However, if I remove the translucency and make the images fully opaque (again, the same file content, just no transparency involved this time), then I get a 0, as I expect:

Code: Select all

 --> convert image1_opaque.tif image2_opaque.tif -compose Difference -composite -format '%[fx:mean*100]' info:
0
I would like something that gives a zero result when the difference is nothing, even when there is some transparency, and a non-zero result when there is any difference at all, in any channel -- including alpha -- of any pixel. Thanks again for the swift replies, and in advance for any further insight!

By the way, glennrp's suggestion of using the detailed image statistics, even though it is not exactly what I'm looking for, might serve the purpose well enough, so I'll use it if I can't come up with something more like what I want. It just seems to me as if there ought to be a way to do what I'm asking; I think it ought to be a simple task for such a large and flexible set of tools as IM.
Caleb9849
Posts: 7
Joined: 2012-06-14T09:01:48-07:00
Authentication code: 13

Re: Compare, including alpha channel

Post by Caleb9849 »

After more experimentation, it seems that "compare" with "-metric ae" does exactly what I want and reports the number of pixels which differ in any way. Maybe this is what fmw42 was getting at, but initially I misunderstood that. Thanks again for all the help, I think I'm good to go now :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Compare, including alpha channel

Post by snibgo »

For composition, alpha is not treated in the same way as the other channels. (It pre-multiplies those channels.) To prevent this behaviour, you need to disable sync, which you do with "-channel RGBA". Thus ...

Code: Select all

convert image1.tif image2.tif -channel RGBA -compose Difference -composite -format '%[fx:mean*100]' info:
... should return zero.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Compare, including alpha channel

Post by anthony »

You must remember transparent pixels really have no defined color.

Typically in image processing color channel values are multiplied by the alpha channel before processing so that when transparency is full color values are always zero regardless of what color they may have had 'hidden' by the transparency.

At this time I believe most of the compare metrics do not do this 'transparency handling' and just compare values directly, which is why comparing images with transparency has such problems. AE is for example 'absolute error difference' without regard to transparency

The only metric I know that handles (or supposed to handle) transparency properly is 'Fuzz' which uses the same metric as the -fuzz factor. See http://www.imagemagick.org/Usage/color_basics/#fuzz

If properly implemented any transparent pixel will be a fixed error (maximum for a single channel) distance from any opaque color, regardless of the values in the color channels for those two pixels. Also any two transparent pixels are a perfect match, regardless of what color is hidden by the transparency.

Note that this is implemented as a separate algorithm in "compare", as the normal -fuzz factor just tests if pixels are within the given 'threshold' and aborts early when a color is plainly not 'close enough'. While "compare" actually needs to know 'how close' the pixels actually are. As the "compare" is a separate implementation, I am not certain how well tested it is, but it should be working.


ASIDE....

The compare metric I am currently needing (for overlaps) is one that ignores the difference if one of the pixels is transparent. That is I am only interested in how well opaque pixels match, and ignore any parts that involve transparency. BUT I want the compare to fail (maximum error) if NO (or some minimal number) opaque pixels are compared to an opaque pixel. A sort of 'self masking' compare. And that is something that is not available at this time.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply