Percentage of a color

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?".
DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Percentage of a color

Post by DavideDavide »

Hi guys,
Is it possible to detect the percentage of a color in one image? For example the percentage of red(s) or green(s).
TIA
Davide

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

Re: Percentage of a color

Post by snibgo »

Yes, but you need to define exactly what you want. You might think about examples. If your query is "percentage of red", then what is the correct answer for a pixel that is #f00, or #800, or #0f0, or #8f0, etc?

Another question: can your query colour be any arbitrary colour such as #123? Or is it only one of red, green or blue?
snibgo's IM pages: im.snibgo.com

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

snibgo wrote:
2020-05-04T05:49:58-07:00
Yes, but you need to define exactly what you want. You might think about examples. If your query is "percentage of red", then what is the correct answer for a pixel that is #f00, or #800, or #0f0, or #8f0, etc?

Another question: can your query colour be any arbitrary colour such as #123? Or is it only one of red, green or blue?
No arbitrary color, red and green are fine.

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

Re: Percentage of a color

Post by snibgo »

What about my first question?
snibgo's IM pages: im.snibgo.com

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

snibgo wrote:
2020-05-04T14:27:35-07:00
What about my first question?
#f00, or #800: red
#0f0, or #8f0: green

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

Re: Percentage of a color

Post by snibgo »

But you want percentages. I suppose #f00 and #800 are "100% red". Is that correct?

And #0f0 has 0% red.

What about #8f0? What percentage of red should that be? How is that percentage calculated?
snibgo's IM pages: im.snibgo.com

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

snibgo wrote:
2020-05-05T05:45:19-07:00
But you want percentages. I suppose #f00 and #800 are "100% red". Is that correct?

And #0f0 has 0% red.
Right!


snibgo wrote:
2020-05-05T05:45:19-07:00
What about #8f0? What percentage of red should that be? How is that percentage calculated?
0% of red, but I don't know how to calculate it.

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

Re: Percentage of a color

Post by snibgo »

Okay, from your answers, I think you are interested the hue, and want to ignore lightness and saturation. Red, green and blue are equally-spaced on the colour circle, 120 degrees apart, or 0.3333 on a scale of 0.0 to 1.0. Red is at both 0.0 and 1.0. I add 50% to make it at 0.5. I assume a colour contains some red if that colour is closer to red than to green or blue. I assume the "percentage of red" declines from 100% to zero at halfway between red and green, or red and bue. This interval is one-sixth on either side of 50%. One-sixth of 100% is 16.6667%.

Windows BAT script:

Code: Select all

rem Red:
%IMG7%magick ^
  toes.png ^
  -colorspace HCL ^
  -channel 0 -separate +channel ^
  -evaluate AddModulus 50%% ^
  -range-threshold 33.3333,50,50,66.6667%% ^
  -format %%[fx:mean]\n ^
  info:

rem Green:
%IMG7%magick ^
  toes.png ^
  -colorspace HCL ^
  -channel 0 -separate +channel ^
  -evaluate AddModulus 33.3333%% ^
  -range-threshold 33.3333,50,50,66.6667%% ^
  -format %%[fx:mean]\n ^
  info:

rem Blue:
%IMG7%magick ^
  toes.png ^
  -colorspace HCL ^
  -channel 0 -separate +channel ^
  -evaluate AddModulus 83.3333%% ^
  -range-threshold 33.3333,50,50,66.6667%% ^
  -format %%[fx:mean]\n ^
  info:
The outputs are:

41.9103
32.4509
1.51347
snibgo's IM pages: im.snibgo.com

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

snibgo wrote:
2020-05-05T08:23:15-07:00
Okay, from your answers, I think you are interested the hue, and want to ignore lightness and saturation. Red, green and blue are equally-spaced on the colour circle, 120 degrees apart, or 0.3333 on a scale of 0.0 to 1.0. Red is at both 0.0 and 1.0. I add 50% to make it at 0.5. I assume a colour contains some red if that colour is closer to red than to green or blue. I assume the "percentage of red" declines from 100% to zero at halfway between red and green, or red and bue. This interval is one-sixth on either side of 50%. One-sixth of 100% is 16.6667%.

Windows BAT script:

Code: Select all

rem Red:
%IMG7%magick ^
  toes.png ^
  -colorspace HCL ^
  -channel 0 -separate +channel ^
  -evaluate AddModulus 50%% ^
  -range-threshold 33.3333,50,50,66.6667%% ^
  -format %%[fx:mean]\n ^
  info:

rem Green:
%IMG7%magick ^
  toes.png ^
  -colorspace HCL ^
  -channel 0 -separate +channel ^
  -evaluate AddModulus 33.3333%% ^
  -range-threshold 33.3333,50,50,66.6667%% ^
  -format %%[fx:mean]\n ^
  info:

rem Blue:
%IMG7%magick ^
  toes.png ^
  -colorspace HCL ^
  -channel 0 -separate +channel ^
  -evaluate AddModulus 83.3333%% ^
  -range-threshold 33.3333,50,50,66.6667%% ^
  -format %%[fx:mean]\n ^
  info:
The outputs are:

41.9103
32.4509
1.51347
Wow snibgo, thank you very much!!
Davide

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

Snibo,
I have IM 6.8.8-6, so I have to use convert, do you know the syntax of your commands for convert?
TIA

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

Re: Percentage of a color

Post by fmw42 »

Just replace magick with convert.

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

fmw42 wrote:
2020-05-06T09:41:17-07:00
Just replace magick with convert.
Nope, there isn't the option range-threshold in convert

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

Re: Percentage of a color

Post by snibgo »

Instead of

Code: Select all

-range-threshold 33.3333,50,50,66.6667%%
... you can use:

Code: Select all

-solarize 50%% -level 33.3333,50%%
snibgo's IM pages: im.snibgo.com

DavideDavide
Posts: 13
Joined: 2015-03-06T08:39:54-07:00
Authentication code: 6789

Re: Percentage of a color

Post by DavideDavide »

Thanks snibgo but probably there are some other options to tweak:
Actually the script is:
convert myImage.png -colorspace HCL -channel R -separate +channel -evaluate AddModulus 50%% -solarize 50%% -level 33.3333,50%% -format %%[fx:mean] info:

The option "-channel 0" didn't work with IM6, so I searched in forum and I found "-channel R", but unfortunately the script returns: %[fx:mean]
I don't know why

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

Re: Percentage of a color

Post by snibgo »

I showed you a command with BAT syntax. If you are running from CMD, the command line, don't double the % signs.
snibgo's IM pages: im.snibgo.com

Locked