Functions or routines for "block diffs" of 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?".
Post Reply
slefevre
Posts: 1
Joined: 2017-02-08T21:19:36-07:00
Authentication code: 1151

Functions or routines for "block diffs" of images

Post by slefevre »

I'm looking to see what built-in tools ImageMagick might have for creating a "block diff" of images.

My use case is comparing before and after images of a webpage, after a site-update is made. When we do style sheet updates, or other changes, we want to make sure that we see the changes that we expect, and don't see changes that we don't expect. When we make changes to the global styles, there is a chance of side effects affecting pages that we aren't spot-checking after the update. If I could easily get a diff of images from a before and after site-scrape, that would help us determine correctness.

So basically I am looking to do something like this:
http://softwarerecs.stackexchange.com/q ... image-diff

In an updated web page, the images are going to be mostly the same, except for ranges of the image where the update has changed something.

I was starting to design out different algorithms to do comparison of the images, but then started to wonder if ImageMagick didn't have some built in tools to do this or something very close to this. So I'm posting this question.

I looked at the coalesce and other frame functions, but those only seem to apply to animations.

I looked at compare, but that only compares images of the same dimensions. If, say, we add a new section to a page, the before and after images might have the same "top" and "bottom", but the after image will be longer and have a new middle section inserted between the top and bottom of the before image.

Are there programs, utilities or apps that already do this work? Or should I forge ahead with rolling my own?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Functions or routines for "block diffs" of images

Post by fmw42 »

I am not sure I understand your "block compare" meaning. Your example images are the same size. So IM compare will show where the two section you have marked have been changed.

The IM compare function will compare two same sized images pixel-by-pixel and mark where they differ.

Code: Select all

compare -metric XXX image1 image2 diffimage
You will also get an overall compare value "averaged" over all the pixels' differences.

You can also compare two different size images and compare will find the best match location of the smaller image within the larger image and show any differences. This requires adding -subimage-search to the command line.

Code: Select all

compare -metric XXX -subimage-search bigimage smallimage diffimage
But diffimage here will have two outputs -0 and -1 or a two layer image depending upon the format. One image will be the difference and the other will be the similarity image, which is an image of the match value at each pixel offset of the small image relative to the large image.

See
http://www.imagemagick.org/script/compare.php
http://www.imagemagick.org/Usage/compare/

Here is one very old example of compare of two different size images. But the syntax needs now to have -subimage-search added to it. See viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Functions or routines for "block diffs" of images

Post by fmw42 »

Taking your two images, I can do the following:

Code: Select all

compare -metric MAE.png FSDKC.png diffimage.png
1994.08 (0.0304277) @ 5,0
And get diffimage-0.png:
Image

The numbers at the bottom:
1994.08 (0.0304277) @ 5,0

The first number is the MAE raw value. The one in parenthesis is the MAE normalized to the range 0-1. So it shows a 3% difference at the best match location which follows the @. So the best match occurs at a 5 pixel shift in x and 0 pixel shift in y for the small image compared to the larger one.
Post Reply