is it possible to detect certain color like grey or red from 24bpp image so i can extract or highlight the specific color on image provided
Asked
Active
Viewed 313 times
0

kaki
- 25
- 1
- 1
- 1
-
4The answer is yes. If you gave more info, the answer could be more than yes. – leppie Jul 05 '12 at 10:20
-
actually, i want to develop simple program that can detect(verify) and highlight grey color from image provided. To be more advance, if successful i want to detect road from image. – kaki Jul 05 '12 at 13:39
2 Answers
0
Yes, you can compare pixel values.
Following link shows How to change color of Image at runtime
How to compare,
bmp.GetPixel(x, y) == Color.Red
0
Like @Tilak said, you can use bitmap.GetPixel
with a for
loops to search the image, like this
for(int y = 0; y < b.Height; y++)
{
for(int x = 0; x < b.Width; x++)
{
if(b.GetPixel(x, y) == Color.Red)
{
//do something
}
}
}
But if you want a "simpler" way, you can just use
foreach(Color c in b)
{
if (c == Color.Red)
{
//do something
}
}
But since the arbg
values might not be exactly equal to Red's, do something like this:
int pixelA = c.A;
int pixelR = c.R;
int pixelG = c.G;
int pixelB = c.B;
int searchA = Color.Red.A;
int searchR = Color.Red.R;
int searchG = Color.Red.G;
int searchB = Color.Red.B;
diffA = Math.Abs(pixelA - searchA);
diffR = Math.Abs(pixelR - searchR);
diffG = Math.Abs(pixelG - searchG);
diffB = Math.Abs(pixelB - searchB);
if (diffA < 10 && diffB < 10 && diffG < 10 && diffR < 10)
{
//do something }
if (diffA > 10 && diffA < 20 && diffR > 10 && diffR < 20 && diffG > 10 && diffG < 20 &&
diffB > 10 && diffB < 20)
{
//do something }
if (diffA > 20 && diffA < 130 && diffR > 20 && diffR < 130 && diffG > 20 && diffG < 130 &&
diffB > 20 && diffB < 130)
{
//do something
}
if (diffA > 130 && diffA < 240 && diffR > 130 && diffR < 240 && diffG > 130 && diffG < 240 &&
diffB > 130 && diffB < 240)
{
//do something
}
if (diffA > 240 && diffA < 350 && diffR > 240 && diffR < 350 && diffG > 240 && diffG < 350 &&
diffB > 240 && diffB < 350)
{
//do something
}
So you can see the different degrees of the color. Hope this helps!

Liam McInroy
- 4,339
- 5
- 32
- 53