topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday April 18, 2024, 6:17 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: color area  (Read 9397 times)

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,823
    • View Profile
    • Donate to Member
color area
« on: February 08, 2012, 08:38 AM »
hello!

in a photo, I want to calculate the area that a specific color occupies

how can I do that?

thanks!

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: color area
« Reply #1 on: February 08, 2012, 08:44 AM »
I don't know any software that can do that. The only way I know how to do it is in code. Basically, you just get the bitmap, iterate over each pixel and count if it is the colour you want. Probably not very useful for you... Hopefully someone knows some software that can do it easier.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,823
    • View Profile
    • Donate to Member
Re: color area
« Reply #2 on: February 08, 2012, 09:13 AM »
percentage of pixels would be nice too

flamerz

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 157
    • View Profile
    • Donate to Member
Re: color area
« Reply #3 on: February 08, 2012, 09:56 AM »
the pixels are in a shape or placed randomly?

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,961
    • View Profile
    • Donate to Member
Re: color area
« Reply #4 on: February 08, 2012, 11:57 AM »
With my ancient version of photoshop, I can select pixels of similar, or the same colour.
If I set it to same colour (Tolerance=0) it mostly only selects a handful of pixels.

I presume that's not what you want - but I'm not sure.
Maybe you could explain a bit more?
Tom

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,823
    • View Profile
    • Donate to Member
Re: color area
« Reply #5 on: February 08, 2012, 12:24 PM »
the pixels are in a shape or placed randomly?

concentrated in few big areas in the photo

With my ancient version of photoshop, I can select pixels of similar, or the same colour.
If I set it to same colour (Tolerance=0) it mostly only selects a handful of pixels.

I presume that's not what you want - but I'm not sure.
Maybe you could explain a bit more?

this is what I want exactly, but I need to quantify this selection as a percentage of the whole photo or as an absolute square inches etc

or maybe I can create another photo with only this selection of pixels and then count the pixels?

« Last Edit: February 08, 2012, 01:08 PM by kalos »

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,961
    • View Profile
    • Donate to Member
Re: color area
« Reply #6 on: February 08, 2012, 03:18 PM »
With my ancient version of photoshop, I can select pixels of similar, or the same colour.
If I set it to same colour (Tolerance=0) it mostly only selects a handful of pixels.

I presume that's not what you want - but I'm not sure.
Maybe you could explain a bit more?

this is what I want exactly, but I need to quantify this selection as a percentage of the whole photo or as an absolute square inches etc

or maybe I can create another photo with only this selection of pixels and then count the pixels?

I'm not really sure where to go from there... :-\ (my version of photoshop is no help in getting the percentage)

anyone else got any ideas?
Tom

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: color area
« Reply #7 on: February 09, 2012, 07:44 PM »
Well, looks like no answers so far...

Here's a quick hack that does the job. It is NOT release quality, but it does what is asked - i.e. Counts the number of pixels in a picture that are a specific colour and outputs that number along with a %.

* PixelCounter.zip (76.64 kB - downloaded 162 times.)

You can run the "PixelCounter.exe" in the debug or release folder.

Screenshot - 2012-02-10 , 12_41_51 PM.png

The complete project is there. (Consider it as WTFPL. ;D I just can't resist that!)



Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: color area
« Reply #8 on: February 09, 2012, 07:54 PM »
this is what I want exactly, but I need to quantify this selection as a percentage of the whole photo or as an absolute square inches etc

The program above that I posted uses % of total pixels, so, if you want to determine the area of the picture, then you can figure out the area that is the colour by using the %. Onscreen, area is largely irrelevant if it isn't expressed in pixels. i.e. Square cm or square inches are basically meaningless. (Yeah, yeah... I know... but it's close enough to true and is still more true than some sort of morphing universe.)

So, I think that covers it.



Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,823
    • View Profile
    • Donate to Member
Re: color area
« Reply #9 on: February 10, 2012, 07:16 AM »
thank you very much!

but, what about photos, where similar colors form an area or multiple areas, which must be counted?

maybe we can input many color codes, but again, how can we determine similar color codes in a photo?

it works exceptionally well with vector graphics, but with raster images, it seems bothersome (like many other stuff with rasters)

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: color area
« Reply #10 on: February 11, 2012, 03:17 AM »
thank you very much!

but, what about photos, where similar colors form an area or multiple areas, which must be counted?

maybe we can input many color codes, but again, how can we determine similar color codes in a photo?

it works exceptionally well with vector graphics, but with raster images, it seems bothersome (like many other stuff with rasters)


It counts all the pixels that are a specific colour, so it doesn't matter what kind of picture it is. I just picked that picture because it's fun. :D

It actually relies on a raster image. No matter what kind of image you use, it will be rasterized. (See below.)

Here's another example screenshot:

Screenshot - 2012-02-11 , 8_16_37 PM.pngcolor area

Here's the code:

Code: C# [Select]
  1. private string SomeOtherMethod(string imgPath, Color compareColor)
  2.         {
  3.             // http://stackoverflow.com/questions/6020406/travel-through-pixels-in-bmp
  4.             Bitmap bmp = new Bitmap(imgPath);
  5.             int count = 0;
  6.             int total = 0;
  7.  
  8.             for (int ii = 0; ii < bmp.Width; ii++)
  9.             {
  10.                 for (int jj = 0; jj < bmp.Height; jj++)
  11.                 {
  12.                     Color pixelColor = bmp.GetPixel(ii, jj);
  13.                    
  14.                     // do stuff with pixelColor
  15.                     if (pixelColor.R == compareColor.R && pixelColor.G == compareColor.G && pixelColor.B == compareColor.B)
  16.                     {
  17.                         count++;
  18.                     }
  19.                     total++;
  20.                 }
  21.             }
  22.  
  23.             decimal percent = (Convert.ToDecimal(count) / Convert.ToDecimal(total)) * 100;
  24.  
  25.             return "Counted " + count.ToString() + " pixels of " + total.ToString() + " (" + percent.ToString() + "%)";
  26.         }


If you wanted to check for multiple colors, that's easily done by adding in more color pickers, etc. etc.

If you want to check for "similar" colours, this is the comparison:

pixelColor.R == compareColor.R && pixelColor.G == compareColor.G && pixelColor.B == compareColor.B

That needs to be adjusted to whatever you consider "similar".

e.g.

Code: C# [Select]
  1. (pixelColor.R <= compareColor.R + 5) && (pixelColor.R >= compareColor.R - 5) && (pixelColor.G <= compareColor.G + 5) && (pixelColor.G >= compareColor.G - 5) && (pixelColor.B <= compareColor.B + 5) && (pixelColor.B >= compareColor.B - 5)

Or whatever.

However, you really need to know some colour theory and the math behind it to come up with something that will work the way you want it to. The above example is just a very simple example.



Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: color area
« Reply #11 on: February 11, 2012, 03:51 AM »
Here's a new version:

Screenshot - 2012-02-11 , 8_49_05 PM.pngcolor area

* PixelCounter2.zip (81.75 kB - downloaded 165 times.)

It lets you look for 3 colours and define a variance as described above. Full source is there as well so anyone can tweak it if they need.

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

kalos

  • Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 1,823
    • View Profile
    • Donate to Member
Re: color area
« Reply #12 on: February 11, 2012, 05:38 AM »
thanks it works wonders!

is there a simple way to quantify the pixels of these skin moles?

http://www.gulfmd.co...images/skinchart.gif

also, how will I know that all the pixels are selected? a line that will mark the area would be helpful

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: color area
« Reply #13 on: February 11, 2012, 09:10 AM »
@Renegade: I like that new SOPA flag...  

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,961
    • View Profile
    • Donate to Member
Re: color area
« Reply #14 on: February 11, 2012, 11:06 AM »
is there a simple way to quantify the pixels of these skin moles?

http://www.gulfmd.co...images/skinchart.gif

also, how will I know that all the pixels are selected? a line that will mark the area would be helpful

I suspect you need photoshop (or equivalent, Gimp, etc.) for what you want. Each of those moles has probably dozens of colours - so trying to select by colour, you will need to vary the "tolerance" of the selection - a bit like making a search "fuzzy". (As I described above.)

In fact I wonder if selecting automatically (using colour) is even fully possible, as all moles have highlit areas which will not be selected when you select by the main colour...
Tom

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: color area
« Reply #15 on: February 11, 2012, 12:02 PM »
In case of the skinchart picture, a selection like using the lasso tool of lots of graphics editors would be the easiest way of handling that process. Then counting the pixels inside would give a percentage covered by it.
But what would that need to be compared to? Total no. of square mm, but how to relate that to the pixelcount? (like Renegade already said) You would need a confirmed measuring standard on the same photo to be able to compare, like the crime-scene photo's you see in TV series like CSI.

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,749
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: color area
« Reply #16 on: February 12, 2012, 11:06 PM »
Isn't this what the Magic Wand does in just about every paint/photo program out there?

Renegade

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,288
  • Tell me something you don't know...
    • View Profile
    • Renegade Minds
    • Donate to Member
Re: color area
« Reply #17 on: February 12, 2012, 11:20 PM »
If someone knows how to do it in Photoshop, chime in. I don't know of how the Magic Wand helps...

Here's an ASCII representation that is impractical for the Magic Wand (to count the "O"s):

---O---O---O---O---O---O---
-O---O---O---O---O---O---O-
---O---O---O---O---O---O---
-O---O---O---O---O---O---O-
---O---O---O---O---O---O---

Or with X's just to make it more painful to look at:

XXXOXXXOXXXOXXXOXXXOXXXOXXX
XOXXXOXXXOXXXOXXXOXXXOXXXOX
XXXOXXXOXXXOXXXOXXXOXXXOXXX
XOXXXOXXXOXXXOXXXOXXXOXXXOX
XXXOXXXOXXXOXXXOXXXOXXXOXXX

That's the first problem for the Magic Wand - You must manually select all similar colour patches yourself, which defeats the purpose of doing it manually.

Next, I don't know how to get a pixel count from the Magic Wand. Anyone know how to do that?


Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,749
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: color area
« Reply #18 on: February 13, 2012, 03:20 AM »
Hmm... That's right. It doesn't select all similar colors. Only connected ones.

I don't use Photoshop, but I remember in PSP several years ago you there was either an option to make the Magic Wand select all similar colors in the entire image, or you could select a color and then use a menu option to select all similar colors. Something like that. . .

Once you've got all the pixels selected there is still a bit of a problem counting them. One way to do it could be to make the selection one color and make everything not selected a different color so you've got just a two-tone image and then use coding magic to figure it out from there. That's based on the assumption that it's easier to count specific pixels in a two-tone image than a 32-bit color image.

If it sounds like I don't know what I'm talking about, that's because I don't. I'll shut up now. :-[

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,961
    • View Profile
    • Donate to Member
Re: color area
« Reply #19 on: February 13, 2012, 09:32 AM »
That's the first problem for the Magic Wand - You must manually select all similar colour patches yourself, which defeats the purpose of doing it manually.

again, in my ancient version of photoshop:
Contigious selected = select adjacent pixels of same colour (or similar colour - depending on the "tolerance")
Contigious deselected= select pixels of same colour (etc.) throughout the image

No idea how to count them though...
and,
as per Ath's post above, I dont really understand the request in the context of the example given...
Tom