Image comparison - grayscale issue

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
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Image comparison - grayscale issue

Post by appu »

I'm using IM for automating my application. For that I used image comparison to check whether certain Menu items are highlighted or not.

Used NCC metric in grayscale mode for comparison. Some times the images rendered will have brightness issue i.e. due to the rendering h/w. So used NCC in grayscale mode which address this brightness issue gracefully. After experimenting observed a 90%+ accuracy level for NCC grayscale compariosn for match cases.

input img1 : Image
input img2: Image

Here img1 is the highlighted menu item compared against with the current image - ie. img2. Here NCC in RGB colormode returns : 73% match.

grayscale img1: Image
grayscale img2: Image

In NCC grayscale colorspace returns 91.6% accuracy. So the image comparison is passed. Actually it is not an expected behaviour for my scenario. I need to signal pass only if the text is 'Setup' and it is highlighted with yellow.

Is this a limitation in grayscale mode for color comparison ?

Is their any other ways in ImageMagick to better solve this issue.

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

Re: Image comparison - grayscale issue

Post by fmw42 »

For a fair comparison between your two grayscale images, you should negate one before the comparison. One image white on black and the other is black on white

Test for yellow before you convert to grayscale.

You can convert to two colors and get the two colors from -unique-colors. Then check that you have something close to your desired yellow and something close to black for the two colors.
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Image comparison - grayscale issue

Post by appu »

Thanks Fred for the reply. Can you please explain little bit more on the below statement(with commands)
You can convert to two colors and get the two colors from -unique-colors. Then check that you have something close to your desired yellow and something close to black for the two colors.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image comparison - grayscale issue

Post by fmw42 »

Actually a better method is the following. Rather than getting unique colors, which does not work well as tested, just get the mean color of the image.

Code: Select all

convert 2v0g83r.png -alpha off -depth 8 -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:
srgb(184,160,55)


or slightly better is to correct the mean for the black text

Code: Select all

convert 2v0g83r.png \
\( -clone 0 -alpha off -depth 8 -fuzz 40% -fill black -opaque black -scale 1x1 \) \
\( -clone 0 -alpha off -depth 8 -fuzz 40% -fill black -opaque black -fill white +opaque black \) \
-delete 0 +swap -compose divide -composite -format "%[pixel:u.p{0,0}]\n" info:
srgb(168,149,54)

So now use a test to compare the mean yellow from a reference that you know is good to the mean of any image. If the values are not close enough, no point with doing the grayscale compare.

Code: Select all

compare -metric rmse xc:yellow xc:"srgb(168,149,54)" null:
21868.2 (0.333687)

Change the yellow color to your real good yellowish color standard.
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Image comparison - grayscale issue

Post by appu »

Thanks Fred for the inputs. I'm trying out the options, you have mentioned in my project.

Can you please clarify the command

Code: Select all

"%[pixel:u.p{0,0}]\n"

Does that mean, it returns the (0,0) pixel value ?

Also scaling internally does the mean operation ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image comparison - grayscale issue

Post by fmw42 »

Code: Select all

"%[pixel:u.p{0,0}]\n"
returns the actual color at pixel (0,0)

see
http://www.imagemagick.org/Usage/transform/#fx_other
accessing pixels section near the bottom of http://www.imagemagick.org/script/fx.php


the scale computes the mean of each channel as a single pixel, but you need the above to extract the color values (unless you are using Q8). Alternately you could send the single pixel to txt: output, but that would then require further filtering to get the color.
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Image comparison - grayscale issue

Post by appu »

OK Fred.

Code: Select all

the scale computes the mean of each channel as a single pixel, but you need the above to extract the color values (unless you are using Q8). Alternately you could send the single pixel to txt: output, but that would then require further filtering to get the color.
Currently I'm using ImageMagick-6.8.6-Q16 in Windows 7 platform. Also used Im4Java for interfacing Java to IM command line.

I couldn't find a way to directly query the

Code: Select all

 -format "%[pixel:u.p{0,0}]\n" info:
image information. But http://im4java.sourceforge.net/docs/dev ... html#utils exposes a set of generic image properties. I couldn't find a way to get the pixel color from Info class.

But I tried the following commands in IM4Java

convert input1.png -alpha off -depth 8 -scale 1x1 input1-scaled.png
convert input2.png -alpha off -depth 8 -scale 1x1 input2-scaled.png

Later compared
compare -metric rmse input1-scaled.png input2-scaled.png null:

This gives exactly the same result as the one you have mentioned. Can you please confirm whether the approach is correct or not.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Image comparison - grayscale issue

Post by fmw42 »

Yes that would also work to get the color of the yellow in the test image. But I thought you needed to compare that to some standard yellow color. If you do then

compare -metric rmse input1-scaled.png xc:"standardyellowcolor" null:

would work.

If you have a master image of the correct color, then

compare -metric rmse input1-scaled.png input2-scaled.png null:

where one of the scaled images comes from the test image and the other from the master image.
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Image comparison - grayscale issue

Post by appu »

Yes Fred, I've the master image against which I am testing the current image.
Thanks for the confirmation.
Post Reply