How to identify a grayscale image (with threshold)?

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?".
codeswitch
Posts: 15
Joined: 2009-09-07T09:33:45-07:00
Authentication code: 8675309

How to identify a grayscale image (with threshold)?

Post by codeswitch »

I know how to find out if an image is a true color image or a grayscale image.

But what I'd like to have is the solution on how to identify for example this image: http://www.pitopia.de/scripts/pictures/ ... D=2&view=1 as a grayscale image.

The whole picture is black and white, except for the eyes which are green. For people it's still black and white.

Is there maybe a solution anywhere on the web? I can't find anything useful. I hope you can inspire me somehow :).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to identify a grayscale image (with threshold)?

Post by fmw42 »

if there is any color in the image, any statistics such as class or type will tell you it is color and not grayscale.

I think you only hope is to analyze the histogram and decide if there is less than some total percent color other than black or white in the image.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to identify a grayscale image (with threshold)?

Post by anthony »

Read in the image, make a clone whcih you greyscale. now compare the original image with the grayscale clone. any differences present is non grayscale color.

see...
Sorting Images by Type, Gray-scale vs Color
http://www.imagemagick.org/Usage/compare/#type_general

I have updated that part with a 'saturation test'
Saturation Test
convert rose: granite: \
-colorspace HSB -channel G -separate +channel \
-format 'avg=%[mean] peak=%[maximum]' info:

rose: -> avg=28750.1 peak=59881
granite: -> avg=1918.87 peak=3490

Numbers are relative to QuantumRange (which in my IM Q16 is 65535)
Note that both have a avg that is about 1/2 that of the peak.
With rose: being very colorful, while granite: only has about 5% color.

The a large difference than 1/2 between the average and peak, would-
indicate small patches of color. Thresholding the saturation channel
can generate a mask of the color area.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
codeswitch
Posts: 15
Joined: 2009-09-07T09:33:45-07:00
Authentication code: 8675309

Re: How to identify a grayscale image (with threshold)?

Post by codeswitch »

Well, thank you very much, but it's a little too complicated for me. I hoped for an easy solution.

But never mind, I using PHP itself to do it. It's not perfect, but it'll do.

If anyone wants the code I'll post it here to be discussed.
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

Hi i am trying to have the code Anthony wrote above work but i keep getting error " convert: unable to open image 'avg-

i am not getting the results he has above. Any help will be appropriated.

I CD to the folder and I have rose and granite both in the ImageMagic dir and trying to run below.

convert rose: granite: \-colorspace HSB -channel G -separate +channel \-format 'avg=%[mean] peak=%[maximum]' info:
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

Over all i need to separate gray scale image from color but has some color pix in the mix.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to identify a grayscale image (with threshold)?

Post by snibgo »

What version IM? On what platform?

Code: Select all

convert rose: granite: \-colorspace HSB -channel G -separate +channel \-format 'avg=%[mean] peak=%[maximum]' info:
Why does your command contain backslashes?
divptl wrote:... and I have rose and granite both in the ImageMagic dir ...
That doesn't make sense. "rose:" and "granite:" (both with colons) are built into IM.
snibgo's IM pages: im.snibgo.com
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

I have the latest version of IM 7.0.6. I am attempting to do same what Anthony did. i kept the blackslashes and removed the line brake from below.
"
Saturation Test
convert rose: granite: \
-colorspace HSB -channel G -separate +channel \
-format 'avg=%[mean] peak=%[maximum]' info:

rose: -> avg=28750.1 peak=59881
"
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

I have the latest version of IM 7.0.6. I am attempting to do same what Anthony did. i kept the blackslashes and removed the line brake from below.
"
Saturation Test
convert rose: granite: \
-colorspace HSB -channel G -separate +channel \
-format 'avg=%[mean] peak=%[maximum]' info:

rose: -> avg=28750.1 peak=59881
"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to identify a grayscale image (with threshold)?

Post by fmw42 »

There is no %[maximum] it is %[max] (see http://www.imagemagick.org/script/escape.php). Try

Code: Select all

magick rose: granite: \
-colorspace HSB -channel G -separate +channel \
-format 'avg=%[mean] peak=%[max]\n' info:
avg=28750.1 peak=59881
avg=1918.71 peak=3490.03
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

Thank you it worked however my grayscale image has some colors in it and i will have to go on another rout. Is there a way to identify like Image generated out of a Patent software vs actual picture taken from camera ? i have no image property to relay on.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to identify a grayscale image (with threshold)?

Post by fmw42 »

See viewtopic.php?f=1&t=16201#p148602

Image

Code: Select all

convert mostly_gray.png -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:
4.2631

So this image contains 4% non-grayscale colors.

A true grayscale image with no color, such as a white to black gradient will have a result of zero.

Code: Select all

convert -size 256x256 gradient: -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:
0
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

Thank you that works. Any way to tell if its graphic vs picture ? Unfortunately with threshold i am still getting color images in to gray scale because they only have three colors .
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

On more question . do you know how would i go about implementing "convert mostly_gray.png -colorspace HCL -channel green -separate +channel -format "%[fx:100*mean]\n" info:" in c# ?
divptl
Posts: 20
Joined: 2017-08-07T14:00:24-07:00
Authentication code: 1151

Re: How to identify a grayscale image (with threshold)?

Post by divptl »

i want to utilize the output to move file if it's above or below threshold.
Post Reply