我有一个图像,如果像素(x,y).R < 165,我想将像素设置为白色。
之后,我想将所有不是白色的像素设置为黑色。
我可以使用ColorMatrix来做这件事吗?
发布于 2010-11-28 17:10:12
你不能用colormatrix做到这一点。colormatrix适用于从一种颜色到另一种颜色的线性变换。你需要的不是线性的。
发布于 2010-11-28 21:18:40
完成这些相对简单的图像操作的一个好方法是自己直接获取位图数据。鲍勃·鲍威尔在https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx上写了一篇关于这方面的文章。它解释了如何锁定位图并通过Marshal类访问它的数据。
有这样一个结构是很好的:
[StructLayout(LayoutKind.Explicit)]
public struct Pixel
{
// These fields provide access to the individual
// components (A, R, G, and B), or the data as
// a whole in the form of a 32-bit integer
// (signed or unsigned). Raw fields are used
// instead of properties for performance considerations.
[FieldOffset(0)]
public int Int32;
[FieldOffset(0)]
public uint UInt32;
[FieldOffset(0)]
public byte Blue;
[FieldOffset(1)]
public byte Green;
[FieldOffset(2)]
public byte Red;
[FieldOffset(3)]
public byte Alpha;
// Converts this object to/from a System.Drawing.Color object.
public Color Color {
get {
return Color.FromArgb(Int32);
}
set {
Int32 = Color.ToArgb();
}
}
}只需创建一个新的像素对象,您就可以通过Int32字段设置它的数据,并读取/修改各个颜色分量。
Pixel p = new Pixel();
p.Int32 = pixelData[pixelIndex]; // index = x + y * stride
if(p.Red < 165) {
p.Int32 = 0; // Reset pixel
p.Alpha = 255; // Make opaque
pixelData[pixelIndex] = p.Int32;
}https://stackoverflow.com/questions/4296107
复制相似问题