ImageMagick Compare

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.
gaurav_kl
Posts: 14
Joined: 2012-03-28T12:28:56-07:00
Authentication code: 8675308

ImageMagick Compare

Post by gaurav_kl » 2012-03-28T12:36:06-07:00

I am working on a project where I am trying to highlight the difference in 2 very similar pages of "scanned" text .
I tried to use the ImageMagick Compare for this but it needs the image to be of exactly same width and height.ALso
tried to use "differeemce" but it also recognizes the changes in "color" differences which I dont need.
Can someone tell what will be best suited for my purpose.
Also if I can use ImageMagick for this purpose ,is there a way I can use the ImageMagick library if my application is a webapp
built in HTML/Javascript.

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

Re: ImageMagick Compare

Post by Bonzo » 2012-03-28T12:40:17-07:00

With your colour problem you could change the images to monocrome first.

I use Imagemagick with php and it works OK; I belive there is a way to work with Javascript as well but I have never used it.

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T13:39:00-07:00

In order to do any comparison of two images, they need to be the same size and scaled and rotated properly. Compare can handle two images of different size, but it will find the best match location of the smaller one with the larger one. If you use -compose difference -composite, then you would need to shift the smaller image so that it is positioned well to match the larger one (see -geometry for offsetting). Scale and rotation are important for both methods. There is no scale and rotation invariant comparison method in IM.

Can you provide links to your two images, so that we have a better understanding of your problem?

gaurav_kl
Posts: 14
Joined: 2012-03-28T12:28:56-07:00
Authentication code: 8675308

Re: ImageMagick Compare

Post by gaurav_kl » 2012-03-28T15:44:25-07:00

Hi ,thanks for replying .
The links for the 2 images are
http://imgur.com/azunh
http://imgur.com/FeAYq

I have manually highlighted in pink the difference in both.
So if I get to align the 2 images properly can I use ImageMagick library to do the highlighting.

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T15:52:14-07:00

try cropping the two images so that they are the same size and shifted to match as well as possible. Then you can either use compare to get the difference image. The more red, the more difference. Or you can use -compose difference -composite and then threshold the image to make the most difference white and the rest black. You can then use that as a mask to color only the area in the images that are different.

You can also always crop one image to remove some or all white border (using -fuzz XX% -trim +repage) and then use compare with one larger image and the other smaller image and it will find the best match and give you the difference image (first resulting image).

see
http://www.imagemagick.org/Usage/compare/
http://www.imagemagick.org/script/compare.php
Last edited by fmw42 on 2012-03-28T16:05:36-07:00, edited 1 time in total.

gaurav_kl
Posts: 14
Joined: 2012-03-28T12:28:56-07:00
Authentication code: 8675308

Re: ImageMagick Compare

Post by gaurav_kl » 2012-03-28T16:05:05-07:00

Thanks.
Is "compare" more suited for my purpose than "difference" as I found that "difference" uses
"difference" in color as a criteria.
Also can you tell if there is a way to use ImageMagick through JavaScript.
I saw as Bonzo told there are ways for it to be used from PHP.

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T16:09:18-07:00

gaurav_kl wrote:Thanks.
Is "compare" more suited for my purpose than "difference" as I found that "difference" uses
"difference" in color as a criteria.
Either should be suitable. If you use -compose difference and the color is confusing, then just convert your image to grayscale either before or after the difference.

gaurav_kl wrote:Also can you tell if there is a way to use ImageMagick through JavaScript.
I saw as Bonzo told there are ways for it to be used from PHP.
I do not know how to use it from Javascript, but it should be doable with the right kind of exec call. Do a google search from Javascript and Imagemagick. That should find something.

There are also APIs that can be used. see
http://www.imagemagick.org/script/api.php

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T16:23:18-07:00

Note that if your two images are not to the same scale and rotation, then this will not work as there will be significant differences everywhere. You somehow will need to "register" your two images together so that they nearly perfectly overlay each other. Unfortunately, IM does not have such automatic programs. You could manually pick numerous control points and then warp one image to the other using -distort polynomial. Use a polynomial that is of first order (no squared terms or cross terms) to get a warp that includes: scale, skew, rotation and translation.

see
http://www.imagemagick.org/Usage/distorts/#polynomial

You will also get differences if your two sets of text have different darknesses for the text. So it might be best to threshold the text images first to binary.

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T16:41:35-07:00

This worked reasonably well for me.

convert azunh.png -shave 20x20 -fuzz 10% -trim +repage -threshold 70% azunh_test.png
convert FeAYq.png -threshold 70% FeAYq_test.png

compare -metric rmse -subimage-search 1FeAYq_test.png azunh_test.png similarity_test.png

Look for the red area in similarity_test-0.png


P.S. I think that your png images are malformed in some way, since I get error/warning messages with the compare step.


wx=0.280560, wy=0.295820, rx=0.631070, ry=0.346230
gx=0.292570, gy=0.596170, bx=0.127380, by=0.064110
wx=0.280560, wy=0.295820, rx=0.631070, ry=0.346230
gx=0.292570, gy=0.596170, bx=0.127380, by=0.064110
6290.2 (0.0959824) @ 44,19
compare: Ignoring incorrect cHRM value when sRGB is also present `1FeAYq_test.png' @ warning/png.c/MagickPNGWarningHandler/1754.

gaurav_kl
Posts: 14
Joined: 2012-03-28T12:28:56-07:00
Authentication code: 8675308

Re: ImageMagick Compare

Post by gaurav_kl » 2012-03-28T17:34:04-07:00

Thanks.Its working fine on my machine without any error for "Compare".
I could not follow why you did "+repage" as I could not see any visible difference on removing +repage.

I think this works fine for my purpose the only thing I have to work on is doing automatic registration .

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T19:12:40-07:00

I could not follow why you did "+repage" as I could not see any visible difference on removing +repage.
When you do a -trim or -crop, IM leaves the virtual canvas around for gif, png, tiff image types (not for jpg). So you need to remove the virtual canvas to be sure other operators that are sensitive to the virtual canvas info are not affected. The +repage removes the virtual canvas info.

see
http://www.imagemagick.org/Usage/crop/#crop_repage

P.S. The compare is doing the shifting only. Any scale or rotate needs to be done prior. For text images (as well as others), you can use -deskew to automatically rotate/deskew the image so that the lines of text are horizontal. But there is no automatic scaling. You would have to do trial and error with -resize to minimize the compare rmse error to do something like that.

see
http://www.imagemagick.org/script/comma ... php#deskew

or my script, unrotate, at the link below.

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-28T21:06:21-07:00

see viewtopic.php?f=3&t=20630 regarding your malformed png images.

gaurav_kl
Posts: 14
Joined: 2012-03-28T12:28:56-07:00
Authentication code: 8675308

Re: ImageMagick Compare

Post by gaurav_kl » 2012-03-29T16:06:33-07:00

Thanks.
I am not sure if this is the right place to ask this.
Is there any chance that openCV library is more suited for my purpose as it may have better JS support .
As I found that openCV library also has an automatic difference highlighter ?

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

Re: ImageMagick Compare

Post by fmw42 » 2012-03-29T16:12:11-07:00

You would probably be better asking this on an OpenCV forum. IM has very very limited use of OpenCV and most of us do not know whether it interfaces with Javascript. If anyone on this forum knows, I am sure they would be happy to answer.

Did you search google about IM and javascript? I found this:

http://www.hacksparrow.com/node-js-imag ... ation.html


By the way, I presume you mean server-side Javascript. Otherwise, calling IM from Javascript on the client side, if possible, would require the client to have a copy of IM.

gaurav_kl
Posts: 14
Joined: 2012-03-28T12:28:56-07:00
Authentication code: 8675308

Re: ImageMagick Compare

Post by gaurav_kl » 2012-04-02T13:49:41-07:00

Hey.
Thanks for the post.
I was kind of confused earlier but now its pretty clear to me.
The project I am working on has server-side code in JAva so I
think it will be easier for me to use the Java-interface for ImageMagick.
Specifically I think I can use Im4java and dont need Jmagick if I just need to run the commands which you had
mentioned in your previous posts?

Post Reply