Page 1 of 3

ImageMagick Compare

Posted: 2012-03-28T12:36:06-07:00
by gaurav_kl
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.

Re: ImageMagick Compare

Posted: 2012-03-28T12:40:17-07:00
by Bonzo
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.

Re: ImageMagick Compare

Posted: 2012-03-28T13:39:00-07:00
by fmw42
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?

Re: ImageMagick Compare

Posted: 2012-03-28T15:44:25-07:00
by gaurav_kl
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.

Re: ImageMagick Compare

Posted: 2012-03-28T15:52:14-07:00
by fmw42
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

Re: ImageMagick Compare

Posted: 2012-03-28T16:05:05-07:00
by gaurav_kl
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.

Re: ImageMagick Compare

Posted: 2012-03-28T16:09:18-07:00
by fmw42
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

Re: ImageMagick Compare

Posted: 2012-03-28T16:23:18-07:00
by fmw42
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.

Re: ImageMagick Compare

Posted: 2012-03-28T16:41:35-07:00
by fmw42
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.

Re: ImageMagick Compare

Posted: 2012-03-28T17:34:04-07:00
by gaurav_kl
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 .

Re: ImageMagick Compare

Posted: 2012-03-28T19:12:40-07:00
by fmw42
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.

Re: ImageMagick Compare

Posted: 2012-03-28T21:06:21-07:00
by fmw42
see viewtopic.php?f=3&t=20630 regarding your malformed png images.

Re: ImageMagick Compare

Posted: 2012-03-29T16:06:33-07:00
by gaurav_kl
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 ?

Re: ImageMagick Compare

Posted: 2012-03-29T16:12:11-07:00
by fmw42
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.

Re: ImageMagick Compare

Posted: 2012-04-02T13:49:41-07:00
by gaurav_kl
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?