Counting the number of pixels of a particular color
Counting the number of pixels of a particular color
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
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
-
- 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
To count the number of Yellow pixels, I would use this:
This is the same as 7 years ago, but explicitly rounded to the nearest integer.
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:
snibgo's IM pages: im.snibgo.com
Re: Counting the number of pixels of a particular color
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 wrote: ↑2017-05-31T15:07:41-07:00 To count the number of Yellow pixels, I would use this:This is the same as 7 years ago, but explicitly rounded to the nearest integer.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:
-
- 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
The syntax above is bash. For Windows BAT:
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:
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.
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:
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 ...
You can also change to a different directory before running an IM command, if you want.
snibgo's IM pages: im.snibgo.com
- 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
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
Thanks a lot, I`ll try that!snibgo wrote: ↑2017-05-31T16:24:35-07:00 The syntax above is bash. For Windows BAT:If using v7, use "magick" instead of "convert".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:
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:If you don't specify a directory, it will look in the current directory.Code: Select all
convert \mydir\mysubdir\tree.gif ...
You can also change to a different directory before running an IM command, if you want.
Hi there, for example this one
I'm trying to count the red and blue pixels inside the image (green border area). Thanks!
- 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
You would have to crop off the scale at the right first, since it contains your red and blue colors as well.
-
- 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
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:
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.
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"
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
- 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
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.
Either way, this is rather hard, since your file is jpg and your red and blue colors are not uniform.
- 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
Here is one possibility of finding the red blobs (regions). It uses my unix shell script, maxima.
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%
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=2 213,113 gray=63736,248,97.255%
max=3 229,164 gray=57054,222,87.059%