??
[函数名称]
??图像统计滤波???WriteableBitmap?StatisticalFilter(WriteableBitmap?src,double?T)
/// <summary> /// Statistical filtering process. /// </summary> /// <param name="src">The source image.</param> /// <param name="T">The threshould to adjust filter effect.</param> /// <returns></returns> public static WriteableBitmap StatisticalFilter(WriteableBitmap src,double T)////图像统计滤波 { if (src != null) { int w = src.PixelWidth; int h = src.PixelHeight; WriteableBitmap srcImage = new WriteableBitmap(w, h); byte[] temp = src.PixelBuffer.ToArray(); byte[] tempMask = (byte[])temp.Clone(); double mean = 0; double std = 0; int[] windowValue = new int[9]; double mR = 0; double mG = 0; double mB = 0; for (int j = 1; j < h-1; j++) { for (int i = 1; i < w-1; i++) { windowValue[0] = (int)(tempMask[(i - 1) * 4 + (j - 1) * w * 4] * 0.114 + tempMask[(i - 1) * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + (j - 1) * w * 4] * 0.299); windowValue[1] = (int)(tempMask[i * 4 + (j - 1) * w * 4] * 0.114 + tempMask[i * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[i * 4 + 2 + (j - 1) * w * 4] * 0.299); windowValue[2] = (int)(tempMask[(i + 1) * 4 + (j - 1) * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + (j - 1) * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + (j - 1) * w * 4] * 0.299); windowValue[3] = (int)(tempMask[(i-1) * 4 + j * w * 4] * 0.114 + tempMask[(i-1) * 4 + 1 + j * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + j * w * 4] * 0.299); windowValue[4] = (int)(tempMask[i * 4 + j * w * 4] * 0.114 + tempMask[i * 4 + 1 + j * w * 4] * 0.587 + tempMask[i * 4 + 2 + j * w * 4] * 0.299); windowValue[5] = (int)(tempMask[(i + 1) * 4 + j * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + j * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + j * w * 4] * 0.299); windowValue[6] = (int)(tempMask[(i - 1) * 4 + (j + 1) * w * 4] * 0.114 + tempMask[(i - 1) * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[(i - 1) * 4 + 2 + (j + 1) * w * 4] * 0.299); windowValue[7] = (int)(tempMask[i * 4 + (j + 1) * w * 4] * 0.114 + tempMask[i * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[i * 4 + 2 + (j + 1) * w * 4] * 0.299); windowValue[8] = (int)(tempMask[(i + 1) * 4 + (j + 1) * w * 4] * 0.114 + tempMask[(i + 1) * 4 + 1 + (j + 1) * w * 4] * 0.587 + tempMask[(i + 1) * 4 + 2 + (j + 1) * w * 4] * 0.299); for (int n = 0; n < 9; n++) { mean += (double)((double)(windowValue[n]) / 9); } for (int n = 0; n < 9; n++) { std += Math.Pow((double)(windowValue[n]) - mean, 2)/9; } if (windowValue[4] >= T * Math.Sqrt(std)) { for (int k = -1; k < 2; k++) { for (int m = -1; m < 2; m++) { mB += (double)tempMask[(i + m) * 4 + (j + k) * w * 4] / 9; mG += (double)tempMask[(i + m) * 4 + 1 + (j + k) * w * 4] / 9; mR += (double)tempMask[(i + m) * 4 + 2 + (j + k) * w * 4] / 9; } } temp[i * 4 + j * w * 4] = (byte)mB; temp[i * 4 + 1 + j * w * 4] = (byte)mG; temp[i * 4 + 2 + j * w * 4] = (byte)mR; } mean = 0; std = 0; mR = mG = mB = 0; } } Stream sTemp = srcImage.PixelBuffer.AsStream(); sTemp.Seek(0, SeekOrigin.Begin); sTemp.Write(temp, 0, w * 4 * h); return srcImage; } else { return null; } }