验证C#中24 bpp图像的特定颜色 [英] verify specific color from 24 bpp image in C#

查看:91
本文介绍了验证C#中24 bpp图像的特定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可以从24bpp图像中检测某些颜色,如灰色或红色,这样我就可以提取或突出显示图像上的特定颜色

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


推荐答案

像@Tilak说的那样,你可以使用 bitmap.GetPixel for 循环搜索图像,如下所示

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
     }
}

但由于 arbg 值可能不是完全等于红色,做这样的事情:

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!

这篇关于验证C#中24 bpp图像的特定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆