Page 1 of 1
Counting the number of pixels of a particular color
Posted: 2017-05-31T14:56:20-07:00
by soulkyo
Hi, I'm completely new to IM, in fact I downloaded it today. I found an answer for this exact question from 7 years ago.
Here are the answers:
https://www.imagemagick.org/discourse-s ... hp?t=16177
My question would be, with the current version of IM, are those answers still the rule of thumb for today's version? or is there an updated answer for my question that could facilitate my work?
Thanks a lot
Re: Counting the number of pixels of a particular color
Posted: 2017-05-31T15:07:41-07:00
by snibgo
To count the number of Yellow pixels, I would use this:
Code: Select all
convert tree.gif -fill black +opaque yellow \
-fill white -opaque yellow \
-format "yellow sun pixels = %[fx:int(w*h*mean+0.5)]" info:
This is the same as 7 years ago, but explicitly rounded to the nearest integer.
Re: Counting the number of pixels of a particular color
Posted: 2017-05-31T15:22:40-07:00
by soulkyo
snibgo wrote: ↑2017-05-31T15:07:41-07:00
To count the number of Yellow pixels, I would use this:
Code: Select all
convert tree.gif -fill black +opaque yellow \
-fill white -opaque yellow \
-format "yellow sun pixels = %[fx:int(w*h*mean+0.5)]" info:
This is the same as 7 years ago, but explicitly rounded to the nearest integer.
Thanks a lot for the help, sorry for the following questions but I'm really new to all of this, if I wanted for example to count red or green pixels, should i put opaque green or opaque red, instead? and an even sillier question, where should I put a file for imagemagick to detect it? I'm using Windows and it seems to only find files at the root of users and not for example in another folder where I open the command prompt window.
Re: Counting the number of pixels of a particular color
Posted: 2017-05-31T16:24:35-07:00
by snibgo
The syntax above is bash. For Windows BAT:
Code: Select all
convert tree.gif ^
-fill black +opaque yellow ^
-fill white -opaque yellow ^
-format "yellow sun pixels = %%[fx:int(w*h*mean+0.5)]" info:
If using v7, use "magick" instead of "convert".
Instead of "yellow", put "red" or "#f00" or "rgb(255,0,0)" or whatever you want. See
http://www.imagemagick.org/script/color.php
When you open a command window, you are in a directory, the "current" directory. When you name a file in an IM command, you can include a directory, if you want:
Code: Select all
convert \mydir\mysubdir\tree.gif ...
If you don't specify a directory, it will look in the current directory.
You can also change to a different directory before running an IM command, if you want.
Re: Counting the number of pixels of a particular color
Posted: 2017-05-31T17:57:41-07:00
by fmw42
You can post an image to any free hosting service and put the URL here.
Re: Counting the number of pixels of a particular color
Posted: 2017-06-01T02:46:17-07:00
by soulkyo
snibgo wrote: ↑2017-05-31T16:24:35-07:00
The syntax above is bash. For Windows BAT:
Code: Select all
convert tree.gif ^
-fill black +opaque yellow ^
-fill white -opaque yellow ^
-format "yellow sun pixels = %%[fx:int(w*h*mean+0.5)]" info:
If using v7, use "magick" instead of "convert".
Instead of "yellow", put "red" or "#f00" or "rgb(255,0,0)" or whatever you want. See
http://www.imagemagick.org/script/color.php
When you open a command window, you are in a directory, the "current" directory. When you name a file in an IM command, you can include a directory, if you want:
Code: Select all
convert \mydir\mysubdir\tree.gif ...
If you don't specify a directory, it will look in the current directory.
You can also change to a different directory before running an IM command, if you want.
Thanks a lot, I`ll try that!
fmw42 wrote: ↑2017-05-31T17:57:41-07:00
You can post an image to any free hosting service and put the URL here.
Hi there, for example this one
I'm trying to count the red and blue pixels inside the image (green border area). Thanks!
Re: Counting the number of pixels of a particular color
Posted: 2017-06-01T09:29:48-07:00
by fmw42
You would have to crop off the scale at the right first, since it contains your red and blue colors as well.
Re: Counting the number of pixels of a particular color
Posted: 2017-06-01T17:05:46-07:00
by snibgo
The problem is quite complex, and depends on what assumptions you can make.
1. You can crop to a rectangle that surrounds the green line.
2. If you want, you can turn pixels outside the green line to black, using:
Code: Select all
fill Black -draw "color 0,0 filltoborder"
For this, it is easiest if you first turn the greenish pixels to exactly #0f0.
3. What do you mean by "red"? No pixels are exactly #f00. Once you have defined what pixels you want, you can turn those white and all others black. Then count, as above.
Re: Counting the number of pixels of a particular color
Posted: 2017-06-04T19:09:29-07:00
by soulkyo
fmw42 wrote: ↑2017-06-01T09:29:48-07:00
You would have to crop off the scale at the right first, since it contains your red and blue colors as well.
Hi, like this?
Re: Counting the number of pixels of a particular color
Posted: 2017-06-04T21:43:46-07:00
by fmw42
Do you want the total number of red pixels or the total number of isolated red regions (small clusters of red)?
Either way, this is rather hard, since your file is jpg and your red and blue colors are not uniform.
Re: Counting the number of pixels of a particular color
Posted: 2017-06-04T22:04:28-07:00
by fmw42
Here is one possibility of finding the red blobs (regions). It uses my unix shell script, maxima.
Code: Select all
compare -metric rmse -subimage-search 0qDN5Mq.jpg xc:"#A63429" comp.png
Code: Select all
maxima -t 82 -n 10 -r 10 comp-1.png max.png
max=1 134,160 gray=65021,253,99.2157%
max=2 213,113 gray=63736,248,97.255%
max=3 229,164 gray=57054,222,87.059%