Page 1 of 2

Compare specific region in an image

Posted: 2008-07-28T20:19:30-07:00
by ourblu
Hi everyone. I was wondering how I would be able to compare only a specified region in an image? The images I am working with are dynamic, in that there are a few places in the image that change (Date/Time are one of them). I've thought about using imagemagick to draw a gray box over those areas, then compare against my baseline image (which would also have those changing regions grayed over). But ideally, I'd like to be able to call some function like this:

Example:
function compare( coordinates )

The coordinates would specify which area I want to compare. Everything else in the image would be ignored. I've been using the compare feature, and I've also read over the different options, but did not see anything regarding specifying coordinates. I've also performed several searches on this forum on "compare regions" or "compare specify" but have not had any success.

Thanks again. This forum is very helpful and has loads of information! Hope to be a regular here.

Re: Compare specific region in an image

Posted: 2008-07-28T20:48:49-07:00
by anthony
convert image1 image2 -crop WxH+X+Y +repage miff:- | compare -metric PAE - null:

where W,H,X,Y is the region to be compared!

Alternatively, you can generate a difference image and get your statistics from that!
See http://www.imagemagick.org/Usage/compare/#difference

Re: Compare specific region in an image

Posted: 2008-07-30T17:42:56-07:00
by ourblu
I'm sorry I didn't even say thank you. The second I read your reply I started working with it immediately. Thanks again Anthony! :)

Re: Compare specific region in an image

Posted: 2013-03-20T17:15:52-07:00
by Sandhya
I am new to imagemagick and looking for the same crop option to use it in UI automation.
The screenshot has "time" so many of my test cases fails.

I could not understand this command convert image1 image2 -crop WxH+X+Y +repage miff:- | compare -metric PAE - null:
image 1 - image to be cropped?
image 2 - reference image?
WxH+X+Y - How to know the co-ordinates?
Can I see the compared image? If so where will that be stored?
Please throw some light

Thanks,
Sandhya

Re: Compare specific region in an image

Posted: 2013-03-20T17:26:20-07:00
by fmw42
Sandhya wrote:
I could not understand this command convert image1 image2 -crop WxH+X+Y +repage miff:- | compare -metric PAE - null:
image 1 - image to be cropped?
image 2 - reference image?
WxH+X+Y - How to know the co-ordinates?
Can I see the compared image? If so where will that be stored?
Please throw some light
In the example above both image1 and image2 are cropped to the same size and outut as miff to standard out. Then they are both piped to compare to get the match score (from PAE metric) only. That way it is all done with one command line and no need to save an intermediate image. Otherwise it would be separated into a convert command followed on another line by the compare command.

The null: says to not create any output.

If you want to see an output image of the differences between the two images just replace null: with some output name.suffix

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

Good references for beginners are:
http://www.imagemagick.org/script/comma ... ptions.php
http://www.imagemagick.org/Usage/
http://www.imagemagick.org/Usage/reference.html

Re: Compare specific region in an image

Posted: 2013-03-20T17:57:35-07:00
by anthony
The same area of the two images are to be compared.

The crop crops the same area from both images and then feed it to compare.

The two images are the same size, as such there is no reference image, though the first is used as the 'shadow' image in the output image from compare but this is discarded ("null:") as we are only interested in the PAE statistics of the region.

The WxH+X+Y is the region of interest.

If you want to see the compared image, replace the null: with a image filename to use. (or show: to just display).

Re: Compare specific region in an image

Posted: 2013-03-20T18:04:29-07:00
by anthony
If the area of interest is not square, (say a circle around some point) you can mask the cropped region of the two images, so they are exactly the same in the un-interesting areas.

This will not effect a PAE (Peak Absolute Error) but will effect averages or other 'global' comparision error returns. That is the masked areas being identical will 'reduce' any overall 'global' error of two regions we are interested in :-( This is why it is is still important to crop just the area of interest (more control of results).

At this time there is no 'read masking' ability to specifically tell compare not to compare pixels in some areas. Such a feature would make 'global' error comparisions or odd-shaped images much more useful.

For example when finding a specific 'shaped image' in a larger image.

Re: Compare specific region in an image

Posted: 2016-03-02T21:54:35-07:00
by saikrishna
I did try the above code snippet but i still get a Difference when running the command, i have give the region X and Y which should be ignored while comparing

convert ActivityScreenError.png ActivityScreen.png -crop 1080x1920+205+77 +repage miff:- | compare -metric PAE - null:

Output::14906 (0.227451)%

I was expecting to return a 0 difference.

Can someone pls help me on this ?

ActivityScreenError.png http://postimg.org/image/3r4gk9kmp/

ActivityScreen.png http://postimg.org/image/siv4focr7/

Re: Compare specific region in an image

Posted: 2016-03-02T22:17:13-07:00
by fmw42
First, in the future, please do not tack onto an old topic with a new problem. Second, please always provide your IM version and platform.

The two subsections are not identical as one can see by cropping them and just looking at the min, max, mean, std of each channel using identify -verbose. They are visually about the same, but not identical.

What makes you think they should be identical? Where did the full images come from? How were they created?

You can see that someone has written some very faint writing onto one of the images by doing:

Code: Select all

convert ActivityScreenError.png ActivityScreen.png -crop 1080x1920+205+77 +repage miff:- | compare -metric PAE - compare.png
Look at compare.png and you will see the writing in red.
I did try the above code snippet but i still get a Difference when running the command, i have give the region X and Y which should be ignored while comparing
NOTE: -crop keeps the region, it does not ignore the region. So you are comparing the region that you wanted to ignore. To ignore regions, you would have to mask out that region or write the same color to that region in both images using -draw

Re: Compare specific region in an image

Posted: 2016-03-02T22:49:37-07:00
by saikrishna
fmw42 wrote:First, in the future, please do not tack onto an old topic with a new problem. Second, please always provide your IM version and platform.
I'm sorry about posting on old post, this was ma first post and will see to that next one will come with all required details ..
Version: ImageMagick 6.9.3-0 Q16 x86_64 2016-02-19 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib
The two subsections are not identical as one can see by cropping them and just looking at the min, max, mean, std of each channel using identify -verbose. They are visually about the same, but not identical.

What makes you think they should be identical? Where did the full images come from? How were they created?
The Image was captured by android automated tests.
You can see that someone has written some very faint writing onto one of the images by doing:

Yah, it was me who put that faint written, i was trying to ignore that region when compare so for testing it i did that.

Code: Select all

convert ActivityScreenError.png ActivityScreen.png -crop 1080x1920+205+77 +repage miff:- | compare -metric PAE - compare.png
Look at compare.png and you will see the writing in red.
I did try the above code snippet but i still get a Difference when running the command, i have give the region X and Y which should be ignored while comparing
NOTE: -crop keeps the region, it does not ignore the region. So you are comparing the region that you wanted to ignore. To ignore regions, you would have to mask out that region or write the same color to that region in both images using -draw
I was trying to ignore that faint region written and then compare the image

Re: Compare specific region in an image

Posted: 2016-03-02T23:24:54-07:00
by fmw42
-crop keeps the region, it does not ignore the region. So you are comparing the region that you wanted to ignore.

Imagemagick does not have a direct compare of images with ignore regions.

To ignore regions, you would have to mask out that region or write the same color to that region in both images using -draw. Then compare the two new images.

Try this: (unix syntax, since you did not specify your platform as requested)

Code: Select all

convert \
\( ActivityScreenError.png -size 1080x1925 xc:black -geometry +205+77 -compose over -composite \) \
\( ActivityScreen.png -size 1080x1925 xc:black -geometry +205+77 -compose over -composite \) \
miff:- | compare -metric PAE - null:
0 (0)
or

Code: Select all

convert \
\( ActivityScreenError.png -size 1080x1925 xc:black -geometry +205+77 -compose over -composite \) \
\( ActivityScreen.png -size 1080x1925 xc:black -geometry +205+77 -compose over -composite \) \
-metric PAE -compare -format "%[distortion]" info:
0

Re: Compare specific region in an image

Posted: 2016-03-02T23:47:13-07:00
by saikrishna
Thanks a lot :) this worked .. i'm on Mac OSX .. i would prefer to use a region to mask and then compare because my tests have the date section.

How do i specify the mask region ? Can you please help me on this..

Re: Compare specific region in an image

Posted: 2016-03-02T23:49:49-07:00
by fmw42
See the correction above. I have masked the image by composing a black area over the region you want to exclude. I forgot the +geometry to place the region in the correct location.

I do not understand about the "date section"?

Re: Compare specific region in an image

Posted: 2016-03-03T00:57:01-07:00
by saikrishna
Regarding the date section please refer this image for example https://github.com/rajdeepv/nakal/blob/ ... urrent.png: Departs and Arrives will hold a time which will keep changing and will diff from baseline image.

So i wanted to mentioned a region in co-ordinates and ignore them while comparing..

Re: Compare specific region in an image

Posted: 2016-03-03T01:09:26-07:00
by saikrishna
i add another faint text in the image in other region and i ran the code above it still shows 0 difference

ActivityScreenError1
http://postimg.org/image/r0a0kwv29/
convert \
\( ActivityScreenError1.png -size 1080x1925 xc:black -geometry +205+77 -compose over -composite \) \
\( ActivityScreenError.png -size 1080x1925 xc:black -geometry +205+77 -compose over -composite \) \
miff:- | compare -metric PAE - null: