[函数代码]
////// Mid-point filter. /// /// The source image. ///public static WriteableBitmap MidPointFilterProcess(WriteableBitmap src)中点滤波器 { if (src != null) { int w = src.PixelWidth; int h = src.PixelHeight; WriteableBitmap filterImage = new WriteableBitmap(w, h); byte[] temp = src.PixelBuffer.ToArray(); byte[] tempMask = (byte[])temp.Clone(); double[] Gray = new double[9]; double gray = 255, graymax = 0; int tempr = 0, tempb = 0, tempg = 0, temprMax = 0, tempgMax = 0, tempbMax = 0; for (int j = 1; j < h - 1; j++) { for (int i = 1; i < w - 1; i++) { tempb = 0; tempg = 0; tempr = 0; gray = 255; graymax = 0; int[] B = new int[9] { tempMask[i * 4 + j * w * 4], tempMask[(i - 1) * 4 + (j - 1) * w * 4], tempMask[i * 4 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + j * w * 4], tempMask[(i + 1) * 4 + j * w * 4], tempMask[(i - 1) * 4 + (j + 1) * w * 4], tempMask[i * 4 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + (j + 1) * w * 4] }; int[] G = new int[9] { tempMask[i * 4 + 1 + j * w * 4], tempMask[(i - 1) * 4 + 1 + (j - 1) * w * 4], tempMask[i * 4 + 1 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + 1 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + 1 + j * w * 4], tempMask[(i + 1) * 4 + 1 + j * w * 4], tempMask[(i - 1) * 4 + 1 + (j + 1) * w * 4], tempMask[i * 4 + 1 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + 1 + (j + 1) * w * 4] }; int[] R = new int[9] { tempMask[i * 4 + 2 + j * w * 4], tempMask[(i - 1) * 4 + 2 + (j - 1) * w * 4], tempMask[i * 4 + 2 + (j - 1) * w * 4], tempMask[(i + 1) * 4 + 2 + (j - 1) * w * 4], tempMask[(i - 1) * 4 + 2 + j * w * 4], tempMask[(i + 1) * 4 + 2 + j * w * 4], tempMask[(i - 1) * 4 + 2 + (j + 1) * w * 4], tempMask[i * 4 + 2 + (j + 1) * w * 4], tempMask[(i + 1) * 4 + 2 + (j + 1) * w * 4] }; for (int n = 0; n < 9; n++) { Gray[n] = (double)B[n] * 0.114 + (double)G[n] * 0.587 + (double)R[n] * 0.299; if (gray > Gray[n]) { gray = Gray[n]; tempb = B[n]; tempr = R[n]; tempg = G[n]; } if (graymax < Gray[n]) { graymax = Gray[n]; tempbMax = B[n]; tempgMax = G[n]; temprMax = R[n]; } } temp[i * 4 + j * w * 4] = (byte)((tempb + tempbMax) / 2); temp[i * 4 + 1 + j * w * 4] = (byte)((tempg + tempgMax) / 2); temp[i * 4 + 2 + j * w * 4] = (byte)((tempr + temprMax) / 2); } } Stream sTemp = filterImage.PixelBuffer.AsStream(); sTemp.Seek(0, SeekOrigin.Begin); sTemp.Write(temp, 0, w * 4 * h); return filterImage; } else { return null; } }
[图像效果]
最后,分享一个专业的图像处理网站(微像素),里面有很多源代码下载: