// foamliu, 2009/02/04, 开操作. // public static int[][] Open(int[][] mat, StructuringElement strel) { int[][] temp, output; Erosion(mat, strel, out temp); GrayScaleImageLib.MinkowskiAddtion(temp, strel, out output); return(output); }
/// <summary> /// 自动阈值化 /// </summary> /// <param name="mat"></param> public static void ThresholdAutomatic(int[,] mat, out int[,] newMat) { int cols = mat.GetLength(0); int rows = mat.GetLength(1); newMat = new int[cols, rows]; double threshold = GrayScaleImageLib.mean(mat); int d = Math.Sign(mat[cols / 2, rows / 2] - threshold); for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { if ((mat[x, y] - threshold) * d > 0) { newMat[x, y] = 255; } else { newMat[x, y] = 0; } } } }
/// <summary> /// foamliu, 2009/03/03, 边缘锐化. /// 从原图像中减去拉普拉斯算子处理后的结果. /// 我做的效果是图像锐化的同时产生了噪音. 与这个结果类似: /// /// http://www.dfanning.com/ip_tips/sharpen.html /// /// </summary> /// <param name="bmp"></param> /// <param name="type"></param> /// <returns></returns> public static Bitmap SharpenEdges(Bitmap bmp) { int width, height; int[][][] mat, filtered = new int[3][][]; Bitmap newBmp; ImageConvert.Bitmap2MatColor(bmp, out mat, out width, out height); Convolution conv = new Convolution(); conv.Calculate(mat[0], ConvKernel.Laplacian_4, out filtered[0]); conv.Calculate(mat[1], ConvKernel.Laplacian_4, out filtered[1]); conv.Calculate(mat[2], ConvKernel.Laplacian_4, out filtered[2]); NcvMatrix.MatSubtract(mat[0], filtered[0]); NcvMatrix.MatSubtract(mat[1], filtered[1]); NcvMatrix.MatSubtract(mat[2], filtered[2]); GrayScaleImageLib.Normalize(mat[0]); GrayScaleImageLib.Normalize(mat[1]); GrayScaleImageLib.Normalize(mat[2]); ImageConvert.Mat2BitmapColor(mat, width, height, out newBmp); return(newBmp); }
/// <summary> /// 灰度直方图 /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static double[] Histogram(Bitmap bmp) { int width, height; int[][] mat; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); double[] hist = GrayScaleImageLib.Histogram(mat); return(hist); }
/// <summary> /// 绘制灰度图的距 /// </summary> /// <param name="bmp"></param> /// <returns></returns> //public static Bitmap GrayValueMoment(Bitmap bmp) //{ // int width, height; // int[][] mat; // Bitmap newBmp = (Bitmap)bmp.Clone(); // ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); // GrayValueFeatures features = new GrayValueFeatures(mat); // features.CalcMomentFeatures(); // Drawing.DrawMoments(newBmp, features); // return newBmp; //} public static void IntensityDist(Bitmap bmp, out double[] intensityDistX, out double[] intensityDistY) { int width, height; int[][] mat; Bitmap newBmp = (Bitmap)bmp.Clone(); ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); intensityDistX = GrayScaleImageLib.IntensityDistributionX(mat); intensityDistY = GrayScaleImageLib.IntensityDistributionY(mat); }
/// <summary> /// 鲁棒的灰度值归一化处理 /// </summary> /// <param name="bmp"></param> /// <param name="pl"></param> /// <param name="pu"></param> /// <returns></returns> public static Bitmap RobustNormalize(Bitmap bmp, double pl, double pu) { int width, height; int[][] mat; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); GrayScaleImageLib.RobustNormalize(mat, pl, pu); ImageConvert.Mat2Bitmap(mat, width, height, out newBmp); return(newBmp); }
/// <summary> /// foamliu, 2009/02/03, 累积灰度直方图. /// /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static double[] CumulativeHistogram(int[][] mat) { double[] hist = GrayScaleImageLib.Histogram(mat); double[] cumu = new double[256]; double sum = 0.0; for (int i = 0; i < 256; i++) { sum += hist[i]; cumu[i] = sum; } return(cumu); }
/// <summary> /// foamliu, 2009/02/03, 鲁棒的(这个词好怪 -_-!!)正则化. /// 比如 pl=0.1, pu=0.99. /// /// </summary> /// <param name="mat"></param> public static void RobustNormalize(int[][] mat, double pl, double pu) { int width = mat.Length; int height = mat[0].Length; // 第一遍, 取得最大最小值. int min = 0, max = 255; double[] cumuHist = GrayScaleImageLib.CumulativeHistogram(mat); for (int i = 0; i < 256; i++) { if (cumuHist[i] > pl) { min = i - 1; break; } } for (int i = 255; i >= 0; i--) { if (cumuHist[i] < pu) { max = i + 1; break; } } // 第二遍,归一化. for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int grayScale = mat[x][y]; if (grayScale > max) { grayScale = max; } if (grayScale < min) { grayScale = min; } mat[x][y] = 255 * (grayScale - min) / (max - min); } } }
/// <summary> /// foamliu, 2009/03/03, 试图整合这些滤波器函数. /// /// </summary> /// <param name="bmp"></param> /// <param name="type"></param> /// <returns></returns> public static Bitmap Filter(Bitmap bmp, FilterType type) { int width, height; int[][] mat, filtered; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); Convolution conv = new Convolution(); ConvKernel kernel = null; switch (type) { case FilterType.Sobel_Gx: kernel = ConvKernel.Sobel_Gx; break; case FilterType.Sobel_Gy: kernel = ConvKernel.Sobel_Gy; break; case FilterType.Prewitt_Gx: kernel = ConvKernel.Prewitt_Gx; break; case FilterType.Prewitt_Gy: kernel = ConvKernel.Prewitt_Gy; break; case FilterType.Laplacian_4: kernel = ConvKernel.Laplacian_4; break; case FilterType.Laplacian_8: kernel = ConvKernel.Laplacian_8; break; default: break; } conv.Calculate(mat, kernel, out filtered); GrayScaleImageLib.Normalize(filtered); ImageConvert.Mat2Bitmap(filtered, width, height, out newBmp); return(newBmp); }
public static int[][] Gaussian(int[][] mat, double sigma) { int[][] filtered; int n = 4; ConvKernel kernel = Util.GetGaussianKernal(sigma, n); Convolution conv = new Convolution(); conv.Calculate(mat, kernel, out filtered); GrayScaleImageLib.Normalize(filtered); return(filtered); }
/// <summary> /// foamliu, 2009/02/09, 灰度图像取反 /// /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static Bitmap GrayValueInverse(Bitmap bmp) { int width, height; int[][] mat; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); GrayScaleImageLib.Inverse(mat); ImageConvert.Mat2Bitmap(mat, width, height, out newBmp); return(newBmp); }
/// <summary> /// foamliu, 2009/02/09, 灰度图像闭操作 /// /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static Bitmap GrayValueClose(Bitmap bmp) { int width, height; int[][] mat, filtered; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); GrayScaleImageLib.Close(mat, StructuringElement.N4, out filtered); ImageConvert.Mat2Bitmap(filtered, width, height, out newBmp); return(newBmp); }
public static Bitmap Median(Bitmap bmp, int k) { int width, height; int[][] mat, filtered; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); filtered = Util.Median(mat, k); GrayScaleImageLib.Normalize(filtered); ImageConvert.Mat2Bitmap(filtered, width, height, out newBmp); return(newBmp); }
public static Bitmap Erosion(Bitmap bmp) { int width, height; int[][][] mat, filtered = new int[3][][]; Bitmap newBmp; ImageConvert.Bitmap2MatColor(bmp, out mat, out width, out height); GrayScaleImageLib.Erosion(mat[0], StructuringElement.N4, out filtered[0]); GrayScaleImageLib.Erosion(mat[1], StructuringElement.N4, out filtered[1]); GrayScaleImageLib.Erosion(mat[2], StructuringElement.N4, out filtered[2]); ImageConvert.Mat2BitmapColor(filtered, width, height, out newBmp); return(newBmp); }
/// <summary> /// foamliu, 2009/02/04, 灰度值腐蚀. /// /// </summary> /// <param name="mat">灰度图像</param> /// <param name="b">结构元素</param> public static void Erosion(int[][] mat, StructuringElement strel, out int[][] output) { //int width = mat.Length; //int height = mat[0].Length; //output = new int[width][]; //for (int i = 0; i < width; i++) //{ // output[i] = new int[height]; //} //int m = b.Length; //int n = b[0].Length; //for (int y = 0; y < height; y++) //{ // for (int x = 0; x < width; x++) // { // int min = int.MaxValue; // for (int j = 0; j < n; j++) // { // for (int i = 0; i < m; i++) // { // int matxy = Util.GetPixel(mat, x - i, y - j); // if (matxy + b[i][j] < min) // { // min = matxy + b[i][j]; // } // } // } // output[x][y] = min; // } //} // foamliu, 2009/02/06, 改用二值图像库中的算法. // // 先把 strel 求转置 // StructuringElement trans = StructuringElement.Transposition(strel); GrayScaleImageLib.MinkowskiSubtraction(mat, trans, out output); }
public static Bitmap Sobel(Bitmap bmp) { int width, height; int[][] img, filtered; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out img, out width, out height); Convolution conv = new Convolution(); conv.CalculateEdge(img, ConvKernel.Sobel_Gx, ConvKernel.Sobel_Gy, out filtered, ConvNorm.Norm_2); GrayScaleImageLib.Normalize(filtered); ImageConvert.Mat2Bitmap(filtered, width, height, out newBmp); return(newBmp); }
public static Bitmap SharpenMore(Bitmap bmp) { int width, height; int[][] mat, filtered; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height); Convolution conv = new Convolution(); conv.Calculate(mat, ConvKernel.Laplacian_8, out filtered); NcvMatrix.MatSubtract(mat, filtered); GrayScaleImageLib.Normalize(mat); ImageConvert.Mat2Bitmap(mat, width, height, out newBmp); return(newBmp); }
public static Bitmap Gaussian(Bitmap bmp, double sigma) { int width, height; int[][] img, filtered; Bitmap newBmp; ImageConvert.Bitmap2Mat(bmp, out img, out width, out height); //double sigma = 1.0; int n = 4; ConvKernel kernel = Util.GetGaussianKernal(sigma, n); Convolution conv = new Convolution(); conv.Calculate(img, kernel, out filtered); GrayScaleImageLib.Normalize(filtered); ImageConvert.Mat2Bitmap(filtered, width, height, out newBmp); return(newBmp); }
/// <summary> /// foamliu, 2009/02/04, 开操作. /// 先进行腐蚀操作再紧接着进行一个使用同样结构元的闵可夫斯基加法. /// /// </summary> /// <param name="mat"></param> /// <param name="b"></param> /// <param name="output"></param> public static void Open(int[][] mat, StructuringElement strel, out int[][] output) { int[][] temp; Erosion(mat, strel, out temp); GrayScaleImageLib.MinkowskiAddtion(temp, strel, out output); }
/// <summary> /// foamliu, 2009/02/04, 闭操作. /// 先执行一个膨胀操作后紧接着再用同一个结构元进行闵可夫斯基减法. /// /// </summary> /// <param name="mat"></param> /// <param name="b"></param> /// <param name="output"></param> public static void Close(int[][] mat, StructuringElement strel, out int[][] output) { int[][] temp; Dilation(mat, strel, out temp); GrayScaleImageLib.MinkowskiSubtraction(temp, strel, out output); }