Detect constant color channel using ImageMagick

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

Detect constant color channel using ImageMagick

Post by appu »

Hi All,

I am using ImageMagick-6.8.6-Q16 for my image comparison activities in my project. I need to verify the whether my applications correctly renders the images by doing image comparisons.

Initially I used NCC approach in grayscale mode. But observed that it is not suited for constant color channel i.e. an image with a constant opaque color. In constant color case, the NCC accuracy is below 50%. So I checked the RMSE approach and it returns promising results. Similar colors have very low RMSE error.

Now I need to identify whether the current image to be tested is constant color channel(eg. img1) OR an image with some objects painted on it. (eg. img2)

Constant color img1 : Green.png http://i41.tinypic.com/206lfle.png
Images with objects img2: cartoon.png http://i43.tinypic.com/34ha06g.png


Please help me to resolve this issue.Thanks in Advance.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Detect constant color channel using ImageMagick

Post by snibgo »

I'm not sure what you are asking, but:

Code: Select all

convert in.png -format "%%[fx:minima.r] %%[fx:maxima.r] %%[fx:minima.g] %%[fx:maxima.g] %%[fx:minima.b] %%[fx:maxima.b]" info:-
(Windows script syntax. For Unix, don't double the "%".)

This spits out 6 numbers. For a constant colour, the first pair will be equal, as will the second pair, and the third. In a script, extract the numbers and compare.

You can probably write an expression that returns a simple 0 or 1.

There are other ways that might be quicker, such as making a copy the same size but completely the colour of one pixel from in.png, and comparing the two images.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect constant color channel using ImageMagick

Post by fmw42 »

Just use the standard_deviation value. If zero, then it is a constant color.

convert in.png -format "%[fx:standard_deviation]" info:

That should get the overall std of all the channels.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Detect constant color channel using ImageMagick

Post by snibgo »

Ah, yes, of course, far better. Thanks.
snibgo's IM pages: im.snibgo.com
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Detect constant color channel using ImageMagick

Post by appu »

Thanks fmw42, snibgo for your suggestions. But when I tried your approach, I got the following results

convert highlight.png -format "%[fx:standard_deviation]" info:
0.0819818

convert highlight.png -format "%[fx:minima.r] %[fx:maxima.r] %[fx:minima.g] %[fx:maxima.g] %[fx:minima.b] %[fx:maxima.b]" info:-
0.784314 0.988235 0.698039 0.92549 0.254902 0.576471

So I understood that these are not pure constant color channels. They have a small gradient in them, which visually seems like a constant color channel. Can you please help me identify gradient cases also ?

sample image tested : Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect constant color channel using ImageMagick

Post by fmw42 »

Note that those values are in the range 0 to 1. So rather small. But with a gradient, if the same hue, then you can convert to HSL and extract the hue channel and test the same way.

convert 2eg9zec.png -colorspace HSL -channel r -separate +channel -format "%[fx:standard_deviation]" info:
0.00363458

So this is a 0.36% difference from a pure hue.

Thus your gradient is not a perfect hue, but is very close.
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Detect constant color channel using ImageMagick

Post by appu »

That worked. You are awesome Fred ! :)
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Detect constant color channel using ImageMagick

Post by appu »

Hi Fred,

I'm newbie to image processing stuffs. In the earlier reply, you have extracted channel r from the image to get the hue.

Code: Select all

convert 2eg9zec.png -colorspace HSL -channel r -separate +channel -format "%[fx:standard_deviation]" info:
Can you please explain why you extracted channel r and not channel b or channel g ?
If there are any reference links, please share it.

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Detect constant color channel using ImageMagick

Post by snibgo »

When an image has 3 channels, they are referred to as "R", "G" and "B", even when they represent something else.

After converting the colorspace to HSL, the first channel represents Hue. The second represents Saturation, and the thirds represents Lightness.
snibgo's IM pages: im.snibgo.com
appu
Posts: 29
Joined: 2013-09-20T10:10:02-07:00
Authentication code: 6789

Re: Detect constant color channel using ImageMagick

Post by appu »

OK. Got it. Thanks Snibgo for the clarification.
Post Reply