[函數名稱]html
最大值濾波器WriteableBitmap MaxFilterProcess(WriteableBitmap src)算法
[算法說明]函數
最大值濾波屬於非線性濾波方法,它是一種基於排序統計理論的可有效抑制噪聲的非線性平滑濾波spa
器,基本原理是把數字圖像或數字序列中一點的值用該點的一個臨域中各點值的最大值替換。.net
[函數代碼]code
/// <summary> /// Max value filter. /// </summary> /// <param name="src">The source image.</param> /// <returns></returns> public static WriteableBitmap MaxFilterProcess(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 = 0; int tempr = 0, tempb = 0, tempg = 0; for (int j = 1; j < h - 1; j++) { for (int i = 1; i < w - 1; i++) { tempb = 0; tempg = 0; tempr = 0; gray = 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]; } } temp[i * 4 + j * w * 4] = (byte)tempb; temp[i * 4 + 1 + j * w * 4] = (byte)tempg; temp[i * 4 + 2 + j * w * 4] = (byte)tempr; } } Stream sTemp = filterImage.PixelBuffer.AsStream(); sTemp.Seek(0, SeekOrigin.Begin); sTemp.Write(temp, 0, w * 4 * h); return filterImage; } else { return null; } }
[圖像效果]htm