Page 1 of 1
How to count number of pixels of a particular color
Posted: 2010-05-07T07:40:46-07:00
by holla
I am new to IM.
I did search the forum, tried about a day figuring this out. No luck.
I want to count number of pixels having a particular rgb value (say rgb(133,82,3)).
You may skip the rest or read on to know why I m doing this:
I am creating a prototype to check whether an image of an eye(retina) has significant yellow spots.
My Approach:
I reduce the number of colors to a fixed set of colors using:
convert "input.jpg" -dither None -map colortable.gif output.gif
(colortable.gif is a typical image reduced to an optimal size)
Once I do this, I want to count the number of pixels having a particular color, like red, yellow, etc.
If I get this info, to some extent, I think I may be able to figure out artifacts automatically.
--
Holla.
Re: How to count number of pixels of a particular color
Posted: 2010-05-07T09:00:33-07:00
by snibgo
convert in.png out.txt
will dump all the pixels, so a text utility could count the pixels thatg are (133,82,3). Or you might explore the "histogram" facilities.
Re: How to count number of pixels of a particular color
Posted: 2010-05-07T09:05:24-07:00
by fmw42
one way is as follows:
convert image -fill black +opaque "rgb(133,82,3)" -format %c histogram:info:
The resulting histogram will have two values, black and your color. The counts are next to them.
Another better method is:
convert image -fill black +opaque "rgb(133,82,3)" -fill white -opaque "rgb(133,82,3)" -format "%[fx:w*h*mean]" info:
this will just report the number of pixels with your color
Re: How to count number of pixels of a particular color
Posted: 2010-05-08T00:39:49-07:00
by anthony
After color reduction, you can get counts of ALL the pixel colors, by getting the histogram comment.
For example
convert image -format %c histogram:info:-
For more info see
Special output file format histogram:
http://www.imagemagick.org/Usage/files/#histogram
and Quantization, Extracting Image Colors
http://www.imagemagick.org/Usage/quantize/#extract
That last also looks at using 'txt: file format which is also looks at in
http://www.imagemagick.org/Usage/files/#txt
Re: How to count number of pixels of a particular color
Posted: 2010-05-08T00:54:42-07:00
by anthony
fmw42 wrote:Another better method is:
convert image -fill black +opaque "rgb(133,82,3)" -fill white -opaque "rgb(133,82,3)" -format "%[fx:w*h*mean]" info:
this will just report the number of pixels with your color
Note however that this is probably just about as fast as outputing all the colors. I have however added it to IM Examples in the Quantization, Color Extraction section. Thanks for the tip.
This can be used for example to get the pixel count of a specific color. What you do is make anything not that color black, and then make that color white. For example lets get the number of colors in the "yellow" sun in the 'tree' image.
Code: Select all
convert tree.gif -fill black +opaque yellow \
-fill white -opaque yellow \
-print "yellow sun pixels = %[fx:w*h*mean]" null:
yellow sun pixels = 20
Remember the "-print" option is equivelent to using "-format ... -write info:" and can be used anywhere within your image processing. This the above I then junked the resuling image using the special "null:" file format.
You could also have added a Fuzz Factor option "-fuzz" before the "-opaque" operator to select 'near' colors as well.
Tree image
Re: How to count number of pixels of a particular color
Posted: 2010-05-08T09:36:00-07:00
by fmw42
Thanks Anthony. But on my system you need to add a \n to your command to keep the result from appending to the next command line prompt.
convert tree.gif -fill black +opaque yellow \
-fill white -opaque yellow \
-print "yellow sun pixels = %[fx:w*h*mean]\n" null:
But that is really no different from the following and I don't know why one needs to use -print and null:
convert tree.gif -fill black +opaque yellow \
-fill white -opaque yellow \
-format "yellow sun pixels = %[fx:w*h*mean]" info:
Both output:
yellow sun pixels = 20
Re: How to count number of pixels of a particular color
Posted: 2010-05-08T18:34:25-07:00
by anthony
They are the same, but I was wanting to make more use of -print as you can use it ANYWHERE. For example in the middle of a larger more complex command.
-print is actually defined as equivalent to
-format '...' -write info:
except that -format is not actually modified.
And yes you are right about the '\n'. Though it did not make much difference to example. Thanks.
Re: How to count number of pixels of a particular color
Posted: 2010-05-09T14:00:02-07:00
by holla
Thanks a lot for all the replies.
It is working..
I am now checking for yellow spots, red spots, white spots, etc one by one.
That is like 4 times invoking convert.exe. The performance is ok for my requirement.
Wondering whether that is the way it should be done or there is an optimized way to get the number of pixels of particular colors that I am interested in, all in one single invocation of convert.exe.
I know for sure that there are some colors like cyan, blue will neven appear in these images i am working with.
Is there a way to replace one color by a nonexistant color like cyan, the next color by blue etc and and find out the number of pixels of a specific color ?
Hope I am clear.
thanks again,
-- Holla.
Re: How to count number of pixels of a particular color
Posted: 2010-05-09T14:07:53-07:00
by snibgo
You could list all of the colours in the image, with the count of each:
convert input.jpg -format %c histogram:info:out.txt
Re: How to count number of pixels of a particular color
Posted: 2010-09-09T15:43:01-07:00
by andienz
Can someone help me how to sucessfully use the solution described above into a cygwin bash script?
As soon as I'm using +opaque it thinks the next color is a file name.
Error message:
convert: unable to open image `rgb(255,0,0)': No such file or directory.
Also the %[fx:w*h*mean]" makes some problems under cygwin bash.
Error message:
convert: unable to parse expression `mean'.
Any suggestions? Thanks!
Re: How to count number of pixels of a particular color
Posted: 2010-09-09T19:00:07-07:00
by anthony
What version of IM?
Sounds old.
What was your EXACT command line? The whole line!
You may have a quoting problem as your fx option only showed one quote.
Re: How to count number of pixels of a particular color
Posted: 2010-09-10T05:55:01-07:00
by andienz
anthony,
Version: ImageMagick 6.4.0 01/19/10 Q16
http://www.imagemagick.org
Full command line:
Code: Select all
convert Cylinder.png -fill black +opaque "rgb(255,0,0)" -fill white -opaque "rgb(255,0,0)" -format "%[fx:w*h*mean]" info:
Results in:
Code: Select all
convert: unable to open image `rgb(255,0,0)': No such file or directory.
If i cange the + to - (just for trouble shooting) I get this:
Full command line:
Code: Select all
convert Cylinder.png -fill black -opaque "rgb(255,0,0)" -fill white -opaque "rgb(255,0,0)" -format "%[fx:w*h*mean]" info:
Results in:
Code: Select all
convert: unable to parse expression `mean'.
When I invoke the command inside a standard windows XP
cmd.exe window it works fine, but i'm using Cygwin for my scripts and I'm working in a bash shell.
Thanks!
Andi
I think I installed Imagemagick for Windows. Is there a special version for Cygwin users? Or is there a way to invoke a regular windows dos shell from cygwin that will run my convert command and then exits?
Re: How to count number of pixels of a particular color
Posted: 2010-09-10T06:53:17-07:00
by anthony
I ran the command
Code: Select all
convert rose: -fill black +opaque "rgb(255,0,0)" -fill white -opaque "rgb(255,0,0)" -format "%[fx:w*h*mean]" info:
in my linux bash command line and got a value of zero. That means everything works fine, as their are no pure reds in that image. Adding a fuzz factor gives me a non-zero pixel count.
Could two versions of IM be installed? On in windows and one in cgywin? A log time ago (before IM v6.3.7-10) the +opaque would not have taken an argument as the 'not this color' form of the operator was not defined. It sounds like you have a version that is older than that!
The same thing about fx 'mean' it was a more recent addition IM v6.4.2-6 according to the 'ChangeLog' so I am not surprised that is not working for you.
WARNING: This fx method will fail due to precision limits of the floating point mean value for vary large images. Caution is recommended.
Re: How to count number of pixels of a particular color
Posted: 2010-09-15T07:05:39-07:00
by andienz
Thanks for your help! My installation of IM is 6.4.0 01/19/10. I'll get a new workstation to work with soon and I'll wait with a new installation until then. For now I found a good workaround by just dumping the complete histogram with the pixel counts into a temporary file and then and using cat, grep and awk to extract the results I'm after. This also enables me to retrieve counts of other colors while only having to invoke the convert command only once.
Thanks again.
Andi