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)
-kalos
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.
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:
color areaHere's the code:
private string SomeOtherMethod(string imgPath, Color compareColor)
{
// http://stackoverflow.com/questions/6020406/travel-through-pixels-in-bmp
Bitmap bmp
= new Bitmap
(imgPath
); int count = 0;
int total = 0;
for (int ii = 0; ii < bmp.Width; ii++)
{
for (int jj = 0; jj < bmp.Height; jj++)
{
Color pixelColor = bmp.GetPixel(ii, jj);
// do stuff with pixelColor
if (pixelColor.R == compareColor.R && pixelColor.G == compareColor.G && pixelColor.B == compareColor.B)
{
count++;
}
total++;
}
}
decimal percent = (Convert.ToDecimal(count) / Convert.ToDecimal(total)) * 100;
return "Counted " + count.ToString() + " pixels of " + total.ToString() + " (" + percent.ToString() + "%)";
}
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.
(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.