我有位图。我想对我的位图应用中值过滤器。但我不能使用 GetPixel() 和 SetPixel(),因为速度对我来说非常重要。我需要非常快速的方法来做到这一点。也许可以通过Graphics.DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)
.
中值滤波器之后,我想应用二值化滤波器(对于每个像素计算亮度:B=0.299*R+0.5876*G+0.114B,如果亮度小于阈值(阈值是[0...255]中我的任务的参数)那么结果图像中我的像素值为 1,否则 - 0)二值化滤波器的速度对我来说也很重要
刚刚找到这个链接:在 .NET (C#) 中对图像进行灰度化的快速方法
/// <summary>
/// Grayscales a given image.
/// </summary>
/// <param name="image">
/// The image that is transformed to a grayscale image.
/// </param>
public static void GrayScaleImage(Bitmap image)
{
if (image == null)
throw new ArgumentNullException("image");
// lock the bitmap.
var data = image.LockBits(
new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, image.PixelFormat);
try
{
unsafe
{
// get a pointer to the data.
byte* ptr = (byte*)data.Scan0;
// loop over all the data.
for (int i = 0; i < data.Height; i++)
{
for (int j = 0; j < data.Width; j++)
{
// calculate the gray value.
byte y = (byte)(
(0.299 * ptr[2]) +
(0.587 * ptr[1]) +
(0.114 * ptr[0]));
// set the gray value.
ptr[0] = ptr[1] = ptr[2] = y;
// increment the pointer.
ptr += 3;
}
// move on to the next line.
ptr += data.Stride - data.Width * 3;
}
}
}
finally
{
// unlock the bits when done or when
// an exception has been thrown.
image.UnlockBits(data);
}
}
EDIT:查看更多信息:
- 使用LockBits方法访问图像数据
- 灰度和颜色矩阵
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)