我有一张图片,如果像素(x,y).R < 165,我想将像素设置为白色。
之后,我想将不是白色的所有像素都设置为黑色。
我可以使用ColorMatrix来完成这个操作吗?
我有一张图片,如果像素(x,y).R < 165,我想将像素设置为白色。
之后,我想将不是白色的所有像素都设置为黑色。
我可以使用ColorMatrix来完成这个操作吗?
您无法使用颜色矩阵完成此操作。颜色矩阵适用于将一种颜色线性转换为另一种颜色。而您所需的变换不是线性的。
[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();
}
}
}
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;
}