/// <summary> /// バイラテラルフィルタの算出 /// </summary> /// <param name="sourceImage">画像の2次元配列</param> /// <param name="filterSize">フィルタのサイズ</param> /// <param name="x">注目画素の横方向の座標</param> /// <param name="y">注目画素の縦方向の座標</param> /// <param name="sigma1">ガウシアンフィルタを制御するパラメータ</param> /// <param name="sigma2">注目画素との画素値の差を制御するパラメータ</param> /// <returns>指定された座標に対するフィルタの2次元配列</returns> public static double[,] CalculateBilateralFilter( byte[,] sourceImage, int filterSize, int x, int y, double sigma1, double sigma2) { Debug.Assert(filterSize % 2 == 1); Debug.Assert(x >= 0 && x < sourceImage.GetLength(0)); Debug.Assert(y >= 0 && y < sourceImage.GetLength(1)); double[,] filter = new double[filterSize, filterSize]; for (int m = -filterSize / 2; m <= filterSize / 2; ++m) { for (int n = -filterSize / 2; n <= filterSize / 2; ++n) { filter[m + filterSize / 2, n + filterSize / 2] = Math.Exp(-(Math.Pow(m, 2.0) + Math.Pow(n, 2.0)) / (2.0 * Math.Pow(sigma1, 2.0))) * Math.Exp(-Math.Pow(Utility.GetPixel(sourceImage, x, y) - Utility.GetPixel(sourceImage, x + m, y + n), 2.0) / (2.0 * Math.Pow(sigma2, 2.0))); } } ImageFiltering.NormalizeFilter(filter); return(filter); }
/// <summary> /// 画像にガウシアンフィルタを適用 /// </summary> /// <param name="sourceImage">画像の2次元配列</param> /// <param name="filterSize">フィルタのサイズ</param> /// <param name="standardDeviation">ガウス分布の標準偏差</param> /// <returns>フィルタ適用後の画像の2次元配列</returns> public static byte[,] ApplyGaussianFilter(byte[,] sourceImage, int filterSize, double standardDeviation) { Debug.Assert(filterSize % 2 == 1); double[,] filter = new double[filterSize, filterSize]; // ガウシアンフィルタを作成 for (int m = 0; m < filterSize; ++m) { for (int n = 0; n < filterSize; ++n) { double r = Math.Pow(m - (filterSize / 2), 2.0) + Math.Pow(n - (filterSize / 2), 2.0); filter[m, n] = Math.Exp(-r / (2.0 * Math.Pow(standardDeviation, 2.0))); } } // フィルタの各要素の合計値が1となるように正規化 ImageFiltering.NormalizeFilter(filter); return(ImageFiltering.ApplyFilter(sourceImage, filter)); }