C# 如何在位图中保留 1 个颜色范围?
C# How to keep 1 range of colors in a bitmap?
所以在 c# 中,我在位图中有一个图像,其中有不同的颜色。现在我试图只在图像中保留一个单一的颜色范围,而所有其他颜色都可以删除(将它们变成白色像素)。现在我想提取的颜色是黄色,但仅仅将像素的颜色与 Color.Yellow 进行比较是不够的,因为像素可以有不同的黄色阴影,所以我猜我需要过滤掉所有其他颜色颜色,但我似乎无法弄清楚该怎么做。
我读过一些关于卷积的内容,但我没有找到直接在程序中实现它的方法。
有没有一种方法可以让我只保留黄色并且图像中有不同的阴影?
提前致谢。
非常模糊的定义,
如果我明白你想做什么,我会这样做:
- 遍历位图的每个像素并将其与黄色范围进行比较(如果在黄色范围之外 - 分配白色值)
- 将每个像素的RGB值转换为CMYK(在线搜索转换公式)[Y = (1-Blue-Black) / (1-Black)]
- 如果YellowMin,则分配白色值
卷积不会帮助你,它作用于空间域而不是颜色
这是一个快速简单的解决方案。
它使用了一个函数,它将 plug-in 与 post 你可以找到 .
这是函数:
public Color ToWhiteExceptYellow(Color c, int range)
{
float hueC = c.GetHue();
float e = 1.5f * range; // you can adapt this nuumber
float hueY = Color.Yellow.GetHue();
float delta = hueC - hueY;
bool ok = (Math.Abs(delta) < e);
//if (!ok) { ok = (Math.Abs(360 + delta) < e); } // include these lines ..
//if (!ok) { ok = (Math.Abs(360 - delta) < e); } // for reddish colors!
return ok ? c : Color.White;
}
它适用于黄色,但由于色调是一个环绕数字它需要更多代码才能与环绕点颜色一起工作 (红色的)。我已经包含了两行来帮助解决问题。
要使其正常工作,请更改链接中的这些行 post:
// pick one of our filter methods
ModifyHue hueChanger = new ModifyHue(ToWhiteExceptYellow);
..和..
// we pull the bitmap from the image
Bitmap bmp = new Bitmap( (Bitmap)pictureBox1.Image); // create a copy
..和..
c = hueChanger(c, trackBar1.Value); // insert a number you like, mine go from 1-10
..和..:[=20=]
// we need to re-assign the changed bitmap
pictureBox2.Image = (Bitmap)bmp; // show in a 2nd picturebox
不要忘记包括代表:
public delegate Color ModifyHue(Color c, int ch);
和 using 子句:
using System.Drawing.Imaging;
注意旧内容要处理掉,以免图片泄露,可能是这样:
Bitmap dummy = (Bitmap )pictureBox2.Image;
pictureBox2.Image = null;
if (dummy != null) dummy.Dispose;
// now assign the new image!
让我们在工作中看看它:
随时展开。您可以更改函数的签名以包含目标颜色并添加亮度范围 and/or 饱和度..
所以在 c# 中,我在位图中有一个图像,其中有不同的颜色。现在我试图只在图像中保留一个单一的颜色范围,而所有其他颜色都可以删除(将它们变成白色像素)。现在我想提取的颜色是黄色,但仅仅将像素的颜色与 Color.Yellow 进行比较是不够的,因为像素可以有不同的黄色阴影,所以我猜我需要过滤掉所有其他颜色颜色,但我似乎无法弄清楚该怎么做。
我读过一些关于卷积的内容,但我没有找到直接在程序中实现它的方法。
有没有一种方法可以让我只保留黄色并且图像中有不同的阴影?
提前致谢。
非常模糊的定义, 如果我明白你想做什么,我会这样做:
- 遍历位图的每个像素并将其与黄色范围进行比较(如果在黄色范围之外 - 分配白色值)
- 将每个像素的RGB值转换为CMYK(在线搜索转换公式)[Y = (1-Blue-Black) / (1-Black)]
- 如果YellowMin,则分配白色值
卷积不会帮助你,它作用于空间域而不是颜色
这是一个快速简单的解决方案。
它使用了一个函数,它将 plug-in 与 post 你可以找到
这是函数:
public Color ToWhiteExceptYellow(Color c, int range)
{
float hueC = c.GetHue();
float e = 1.5f * range; // you can adapt this nuumber
float hueY = Color.Yellow.GetHue();
float delta = hueC - hueY;
bool ok = (Math.Abs(delta) < e);
//if (!ok) { ok = (Math.Abs(360 + delta) < e); } // include these lines ..
//if (!ok) { ok = (Math.Abs(360 - delta) < e); } // for reddish colors!
return ok ? c : Color.White;
}
它适用于黄色,但由于色调是一个环绕数字它需要更多代码才能与环绕点颜色一起工作 (红色的)。我已经包含了两行来帮助解决问题。
要使其正常工作,请更改链接中的这些行 post:
// pick one of our filter methods
ModifyHue hueChanger = new ModifyHue(ToWhiteExceptYellow);
..和..
// we pull the bitmap from the image
Bitmap bmp = new Bitmap( (Bitmap)pictureBox1.Image); // create a copy
..和..
c = hueChanger(c, trackBar1.Value); // insert a number you like, mine go from 1-10
..和..:[=20=]
// we need to re-assign the changed bitmap
pictureBox2.Image = (Bitmap)bmp; // show in a 2nd picturebox
不要忘记包括代表:
public delegate Color ModifyHue(Color c, int ch);
和 using 子句:
using System.Drawing.Imaging;
注意旧内容要处理掉,以免图片泄露,可能是这样:
Bitmap dummy = (Bitmap )pictureBox2.Image;
pictureBox2.Image = null;
if (dummy != null) dummy.Dispose;
// now assign the new image!
让我们在工作中看看它:
随时展开。您可以更改函数的签名以包含目标颜色并添加亮度范围 and/or 饱和度..