可以从24bpp图像中检测某些颜色,如灰色或红色,这样我就可以提取或突出显示图像上的特定颜色
答案 0 :(得分:0)
答案 1 :(得分:0)
就像@Tilak所说,你可以使用bitmap.GetPixel
和for
循环搜索图片,就像这样
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
}
}
}
但如果你想要一种“更简单”的方式,你可以使用
foreach(Color c in b)
{
if (c == Color.Red)
{
//do something
}
}
但由于arbg
值可能 等于Red,所以请执行以下操作:
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
}
所以你可以看到不同程度的颜色。希望这有帮助!