Counting the number of pixels of a particular 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?".
Post Reply
soulkyo
Posts: 4
Joined: 2017-05-31T14:48:52-07:00
Authentication code: 1151

Counting the number of pixels of a particular color

Post 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 :D
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Counting the number of pixels of a particular color

Post 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.
snibgo's IM pages: im.snibgo.com
soulkyo
Posts: 4
Joined: 2017-05-31T14:48:52-07:00
Authentication code: 1151

Re: Counting the number of pixels of a particular color

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Counting the number of pixels of a particular color

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting the number of pixels of a particular color

Post by fmw42 »

You can post an image to any free hosting service and put the URL here.
soulkyo
Posts: 4
Joined: 2017-05-31T14:48:52-07:00
Authentication code: 1151

Re: Counting the number of pixels of a particular color

Post 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 Image
I'm trying to count the red and blue pixels inside the image (green border area). Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting the number of pixels of a particular color

Post by fmw42 »

You would have to crop off the scale at the right first, since it contains your red and blue colors as well.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Counting the number of pixels of a particular color

Post 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.
snibgo's IM pages: im.snibgo.com
soulkyo
Posts: 4
Joined: 2017-05-31T14:48:52-07:00
Authentication code: 1151

Re: Counting the number of pixels of a particular color

Post 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?
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting the number of pixels of a particular color

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Counting the number of pixels of a particular color

Post 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
Image

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%


Image
Post Reply