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 » 2009-10-06T10:06:07-07:00

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: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2009-10-06T11:31:51-07:00

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: 8869
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 » 2009-10-06T16:24:37-07:00

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
http://www.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 » 2009-10-07T08:37:44-07:00

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 » 2017-08-07T14:08:44-07:00

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 » 2017-08-07T14:15:23-07:00

Over all i need to separate gray scale image from color but has some color pix in the mix.

snibgo
Posts: 9385
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 » 2017-08-07T14:34:39-07:00

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 » 2017-08-08T06:04:36-07:00

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 » 2017-08-08T06:05:04-07:00

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: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2017-08-08T09:14:49-07:00

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 » 2017-08-08T10:20:21-07:00

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: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2017-08-08T10:22:53-07:00

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 » 2017-08-08T12:00:34-07:00

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 » 2017-08-08T12:58:12-07:00

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 » 2017-08-08T13:07:35-07:00

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

Post Reply