在我的灰度图片中,我需要找到特定的值并将其替换为某种颜色,例如黄色。这是我现在拥有的代码,但它没有我需要的那么快。有没有更有效的方法来实现这一目标?我对ColorMatrix类感到不满,但找不到在不影响整个图像的情况下替换单个值的方法。
unsafe
{
byte stlThres = 115;
int bytePerPixel = Image.GetPixelFormatSize(kpImageViewer1.Image.PixelFormat) / 8;
var data = kpImageViewer1.Image.LockBits(new Rectangle(0, 0, kpImageViewer1.Image.Width, kpImageViewer1.Image.Height), ImageLockMode.WriteOnly, kpImageViewer1.Image.PixelFormat);
for (int y = 0; y < data.Height; y++)
{
byte* row = (byte*)data.Scan0 + (y * data.Stride);
for (int x = 0; x < data.Width; x++, row += bytePerPixel)
{
if (row[0] == stlThres)
{
row[0] = 0; //b
row[1] = 255; //g
row[2] = 255; //r
if (bytePerPixel == 4)
{
row[3] = 255; //a
}
}
}
}
kpImageViewer1.Image.UnlockBits(data);
}