Page 1 of 2

Percentage of a color

Posted: 2020-05-04T02:53:02-07:00
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

Re: Percentage of a color

Posted: 2020-05-04T05:49:58-07:00
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?

Re: Percentage of a color

Posted: 2020-05-04T13:59:12-07:00
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.

Re: Percentage of a color

Posted: 2020-05-04T14:27:35-07:00
by snibgo
What about my first question?

Re: Percentage of a color

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

Re: Percentage of a color

Posted: 2020-05-05T05:45:19-07:00
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?

Re: Percentage of a color

Posted: 2020-05-05T07:15:17-07:00
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.

Re: Percentage of a color

Posted: 2020-05-05T08:23:15-07:00
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

Re: Percentage of a color

Posted: 2020-05-06T01:21:09-07:00
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

Re: Percentage of a color

Posted: 2020-05-06T06:48:40-07:00
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

Re: Percentage of a color

Posted: 2020-05-06T09:41:17-07:00
by fmw42
Just replace magick with convert.

Re: Percentage of a color

Posted: 2020-05-07T02:25:03-07:00
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

Re: Percentage of a color

Posted: 2020-05-07T04:44:50-07:00
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%%

Re: Percentage of a color

Posted: 2020-05-08T01:28:42-07:00
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

Re: Percentage of a color

Posted: 2020-05-08T04:52:19-07:00
by snibgo
I showed you a command with BAT syntax. If you are running from CMD, the command line, don't double the % signs.