public static Image RemoveNoise(Bitmap img, int maxAroundPoints=1)
{
int width = img.Width;
int height = img.Height;
BitmapData bdata = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite,
PixelFormat.Format32bppRgb);
#region 指针法
unsafe
{
byte* ptr = (byte*)bdata.Scan0.ToPointer();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) //边界点,直接当作噪点去除掉
{
ptr[0] = ptr[1] = ptr[2] = 255;
}
else
{
int aroundPoint = 0;
if (ptr[0] != 255) //看标记,不是背景点
{
//判断其周围8个方向与自己相连接的有几个点
if ((ptr - 4)[0] != 255) aroundPoint++; //左边
if ((ptr + 4)[0] != 255) aroundPoint++; //右边
if ((ptr - width * 4)[0] != 255) aroundPoint++; //正上方
if ((ptr - width * 4 + 4)[0] != 255) aroundPoint++; //右上角
if ((ptr - width * 4 - 4)[0] != 255) aroundPoint++; //左上角
if ((ptr + width * 4)[0] != 255) aroundPoint++; //正下方
if ((ptr + width * 4 + 4)[0] != 255) aroundPoint++; //右下方
if ((ptr + width * 4 - 4)[0] != 255) aroundPoint++; //左下方
}
if (aroundPoint < maxAroundPoints)//目标点是噪点
{
ptr[0] = ptr[1] = ptr[2] = 255; //去噪点
}
}
ptr += 4;
}
ptr += bdata.Stride - width * 4;
}
}
img.UnlockBits(bdata);
#endregion
return img;
}