How to save the DIFFERENCES between two images ?

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?".
minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

How to save the DIFFERENCES between two images ?

Post by minglw » 2010-02-16T14:53:31-07:00

The subject may not be clear enough to capture what I want to do.

I have two images:
image1.png -- this image is a photo, and is a scenary image.
image2.png -- this is identical to image1.png except that we have a person in the middle of the image.
(the person is the only difference between the two images)

What I want to do is to create another image file with just the person from image2.png and nothing else.

The thing I want to do sounds straight forward and seems like there must be a way to do it using the ImageMagick tools, but I just don't know how.

I started with the compare.exe, it seems natural to me that this program should tell the differences and allow me to save the differences (in it's original color).

I've tried "compare":
compare.exe image1.png image2.png -compose Src person.png

And it shows the differences between the two images, I can see the person's outline (in red, the rest in white), however this is not what I want.

What I want is the person in it's full original color, not just a single color from the compare.

I've experimented with convert and composite but without success.

I also thought of using the output from the compare as a "mask" and then try to "crop" just the red (the person) portion
from image2.png, but I don't know how to do that either.

compare.exe maybe not be the right tool, but I would like to know which tool and how to "diff" out just the person, in it's FULL original color.

Can someone please tell me how to do it ?

Thanks!


Update: the summary to this topic is below. Scroll down to read it or click here: viewtopic.php?f=1&t=15584#p55309
Last edited by minglw on 2010-02-20T15:09:22-07:00, edited 1 time in total.

Bonzo
Posts: 2970
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to save the DIFFERENCES between two images ?

Post by Bonzo » 2010-02-16T15:04:14-07:00

There is a section on compairing images on Anthony's example site. I think you need to create a mask but do not ask me how.
http://www.imagemagick.org/Usage/compare/#difference

There is another example of extracting part of an image in one of his other sections but I can not find it at the moment.

snibgo
Posts: 11694
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to save the DIFFERENCES between two images ?

Post by snibgo » 2010-02-16T15:24:04-07:00

Can you post links to image1.png and image2.png?
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25032
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 » 2010-02-16T15:37:23-07:00

try this:

Image

Image

convert cyclops_question.png cyclops.png \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
cyclops_sep.png

Image

this creates a binary mask from the difference image and then puts it into the alpha channel of the first image so that the question is visible and surrounded by transparency, then it trims the transparent outside area away leaving only the question image.

Note this works only if the images are exactly the same except for the question overlay. So they must be gif, png or tiff image. If your images are jpegs (or otherwise compressed), then this will not work well due to compression differences. If that is the case you may need to adjust the -threshold XX%, to get rid of the jpeg compression noise (by trial and error).

minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw » 2010-02-16T17:56:47-07:00

Thanks fmw42!
Using your images and your command line works. It still seems a bit complicated to me to understand at this point. I have to study all the options in detail.

As you've mentioned, if the images are a bit different, then it wouldn't work.

Instead of two photos I have mentioned above, I tried it on two screen capture images.
The idea is still the same (image2 is the same as image1 except with some stuff added).

The goal is still the same, save the differences (in it's original color) to a file.

Here are the files: (first time I am using google docs because I don't know of a better place to upload it)
(if there's a better place to upload the images, please let me know.)

image1.png:

https://docs.google.com/leaf?id=0B-jgOG ... ZTc4&hl=en

image2.png:

https://docs.google.com/leaf?id=0B-jgOG ... MDI5&hl=en


if you run this:
compare.exe image1.png image2.png -compose Src differences.png

you can see the differences clearly.
Now, I just want the differences in it's original color (as in image2.png) saved to a file.
Last edited by minglw on 2010-02-17T08:10:29-07:00, edited 1 time in total.

snibgo
Posts: 11694
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to save the DIFFERENCES between two images ?

Post by snibgo » 2010-02-16T18:20:51-07:00

The links don't work. Perhaps you didn't tell GoogleDocs that anyone could have access.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25032
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 » 2010-02-16T18:25:36-07:00

I cannot get to your images. seems to want a user log in
if you run this:
compare.exe image1.png image2.png -compose Src differences.png

you can see the differences clearly.
Now, I just want to the differences in it's original color (as in image2.png) saved to a file.
I am not sure I understand here what you want from "differences in it's original color"? Can you explain a bit more?

compare.exe image1.png image2.png -compose Src differences.png
This cannot work as it is missing -composite? Do you mean

compare.exe image1.png image2.png -compose Src -composite differences.png


When I run this:

convert cyclops_question.png cyclops.png -compose Src -composite cyclops_tmp.png

I just get back the cyclops.png image. That is what -compose src does --- it just overlays image2 over image1 completely replacing image1 with image2 (if both are the same size). see http://www.imagemagick.org/Usage/compose/#src

minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw » 2010-02-16T19:38:44-07:00

I just changed the sharing in google docs so that everyone doesn't need to sign in to see it. Please try again.

and if it still doesn't work, is there another place I should upload images ?
(or perhaps, you can leave me an e-mail and I can send it to you.)


To answer fmw42's question:
"I am not sure I understand here what you want from "differences in it's original color"? Can you explain a bit more?"

I think what you've presented in your examples (cyclops_question.png cyclops.png) is correct.
In your case, I want the question mark (exactly as it appears in cyclops_question.png).

Except in my example (image1.png and image2.png), the differences are a bit bigger.
In the two images, the differences are the vegetables planted in the field.
What I mean by "differences in it's original color", is that I want to save just the vegetables (the differences between image1.png and image2.png) as it appears in image2.png onto another file.

User avatar
fmw42
Posts: 25032
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 » 2010-02-16T20:19:03-07:00

works for me with your images. you just have to have the first image in the sequence contain the extra data.

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
image12diff.png

Image

P.S. If you want the resulting diff image to be the same size as the two inputs, then leave the -trim out of the command line.

minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw » 2010-02-17T07:19:58-07:00

Thank you fmw42!
The resulting image you've shown is exactly what I wanted.

Here's what I get:
https://docs.google.com/leaf?id=0B-jgOG ... ZmU3&hl=en

I found one thing that's a bit strange (to me at least):
when I view the resulting image with ACDSEE, or HyperSnap or the paint program that comes with WinXP, I still see the background.

However, if I view the resulting image with IrfanView, it shows plants and the rest transparent just like your image.
BTW, IMDisplay correctly displays the transparencies as well.

Actually, this is part of my confusion. When I try your solution (after your first reply), I was viewing the result with ACDSEE.
Since I saw the background, I concluded that it doesn't work.

Why only IrfanView show the "background" transparent? (does that mean the other programs I've tried didn't implement transparency for PNG?)


Update: I did an experiment with PNG transparency. I edited the resulting image (PNG) and marked the background as transparent in HyperSnap and saves the result (also as PNG). Now, every program (ACDSEE, MS paint, Hypersnap, IMDisplay) can display the transparent background correctly.

Here's the image I edited with Hyersnap:
https://docs.google.com/leaf?id=0B-jgOG ... ZWI3&hl=en

I don't know how transparency works in PNG. The natural question I have is:
Are there different types of transparency in PNG files?

Please correct me if I am wrong.

Apparently the transparency used by IM's convert is different than the one used by Hypersnap.
(that's concluded from the fact that IM's convert's transparency can only be displayed correctly by IMDisplay and IrfanView,
but Hypersnap's transparency can be displayed by all other PNG viewers.)

Is it possible to have IM's convert to set the same transparent settings as Hypersnap ?
(so that other PNG viewers can show the transparent sections correctly)

If so, how?

User avatar
fmw42
Posts: 25032
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 » 2010-02-17T11:30:15-07:00

I am sure IM's png transparency is according to official png specs. I suspect your other Windows viewers are using some odd transparency technique. I have displayed IM created pngs in many programs (none on windows) mostly on Mac Unix and never had a problem with png transparency. You can try making your output image as PNG32:image12diff.png and see if that makes a difference, but I doubt it will. See http://www.imagemagick.org/Usage/formats/#png_formats

Or perhaps they do not like 16 bit results. Add -depth 8 just before your output.

Or see the extra controls at http://www.imagemagick.org/Usage/formats/#png_write
and try adding -define png:color-type=6

or both to the end of your command line just before the output image.

Your question about compatibility should be addressed to the IM folks via the developers forum to be sure they see it.

minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw » 2010-02-17T13:04:41-07:00

Thank you fmw42 for the info!

I tried PNG32, -depth 8, and -define png:color-type=6 in different combinations
before the last "-composite" and also after and it didn't make any differences.

I am using this package: ImageMagick-6.5.9-6
running it on WinXP with service pack 2.

I have to assume that all PNG viewers must be using the official PNG specs as a reference, otherwise, how can they properly decode and display the image?


Summary:

The question for the topic has been answered by fmw42. Thank you once again!
The command to use is this:

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
-delete 1 -alpha off -compose copy_opacity -composite -trim \
image12diff.png

Note: If you want the resulting diff image to be the same size as the two inputs, then leave the -trim out of the command line.

Note2: I found that not all image viewers can display the transparencies in the PNG file generated by convert.
For example, ACDSEE v5.0, MS Paint (from the WinXP OS), HyperSnap v6.70 can't display the transparent sections

Here's the resulting image generated by convert:
https://docs.google.com/leaf?id=0B-jgOG ... ZmU3&hl=en

However, if I set the background to be transparent in HyperSnap, then all other viewer mentioned above can display it.
Here's the image edited and saved from HyperSnap:
https://docs.google.com/leaf?id=0B-jgOG ... ZWI3&hl=en

The open question I have is:
Why does the transparency that HyperSnap sets works for all PNG viewers, but IM's convert's transparency does not ?



Summary Update:
---------------------

Glenn answered the open question I have above.
glennrp wrote:I can't say why some applications display the image properly and some don't, but I can tell you what's different. image12diff.png image is an RGBA PNG (i.e., PNG colortype 6, with alpha channel), while image12diffx_Hypersnap_saved.png image is an RGB PNG with a tRNS chunk (i.e., PNG colortype 2, with a single color marked transparent).

Both are spec-compliant PNGs. I guess some applications still don't handle all spec-compliant PNG files correctly.

Glenn

Glenn also provides more info on how IM works with PNG.
Please read the detail here: viewtopic.php?f=1&t=15584&start=15#p55396

With the info provided by Glenn, Fred came up with the following command:

convert image2.png image1.png -alpha off +repage \
\( -clone 0 -clone 1 -compose difference -composite -threshold 0 \) \
\( -clone 0 -clone 2 -compose multiply -composite \) \
-delete 0,1 +swap -alpha off -compose copy_opacity -composite -trim +repage \
image12diff2a.png

Here's the resulting image:
https://docs.google.com/leaf?id=0B-jgOG ... MDU2&hl=en

The resulting image's transparency can be displayed properly in all of the PNG viewers I've tried.
I have tried all these viewers on WinXP: IMDisplay, IrfanView, MS Paint, HyperSnap, ImageFox and ACDSEE.

Thank you Glenn and Fred for your help!
Last edited by minglw on 2010-02-20T15:05:55-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 25032
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to save the DIFFERENCES between two images ?

Post by fmw42 » 2010-02-17T13:16:22-07:00

The open question I have is:
Why does the transparency that HyperSnap sets works for all PNG viewers, but IM's convert's transparency does not ?
minglw



Post this question to the developers forum

User avatar
magick
Site Admin
Posts: 10982
Joined: 2003-05-31T11:32:55-07:00

Re: How to save the DIFFERENCES between two images ?

Post by magick » 2010-02-17T13:42:19-07:00

Why does the transparency that HyperSnap sets works for all PNG viewers, but IM's convert's transparency does not ?
The ImageMagick PNG decoder / encoder was written by the same person that maintains the PNG library (Glenn). We assume that ImageMagick creates proper PNG images with transparency unless proven otherwise.

minglw
Posts: 31
Joined: 2010-02-16T14:32:06-07:00
Authentication code: 8675308

Re: How to save the DIFFERENCES between two images ?

Post by minglw » 2010-02-17T14:09:59-07:00

Hi magick, in my summary above, I've links to two images, one created by convert, one created by HyperSnap.
I don't know if I can claim that's proof. However, any one running on WinXP can at least verify my claim.

The fact is, if you open the PNG generated by convert with MS paint, it still shows the background. It doesn't display it as transparent (usually transparencies are display as black)

However, for the file edited by HyperSnap it displays the background as transparent (black) in MS paint.

I am not saying what's generated by convert is wrong, I am simply questioning why the transparency info set by convert can't be displayed properly by other PNG viewers, but the transparency info set by Hypersnap can.

Post Reply