/// <summary> /// 获得非极大值抑制后的图像 /// </summary> /// <param name="Input"></param> /// <param name="Edges"></param> /// <param name="KernelSize"></param> /// <param name="Sigma"></param> public static void Suppression(Bitmap Input, out Bitmap Edges, int KernelSize, float Sigma) { ReadImage(Input); GenerateGaussianKernel(KernelSize, Sigma, out float[,] GaussianKernel); //高斯模糊处理 _grayImage = ConvolutionFilter2D(_grayImage, _width, _height, GaussianKernel, KernelSize); //利用算子获得梯度与梯度方向数组 Soble(_grayImage, _width, _height, out _gradient, out _angle); //非极大值抑制 Suppression(_image.Width, _height, _gradient, _angle); Edges = new Bitmap(_width, _height); LockBitmap lockBitmap = new LockBitmap(Edges); lockBitmap.LockBits(); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { if (_gradient[i, j] > 255) { _gradient[i, j] = 255; } else if (_gradient[i, j] < 0) { _gradient[i, j] = 0; } lockBitmap.SetPixel(i, j, Color.FromArgb(_gradient[i, j], _gradient[i, j], _gradient[i, j])); } } lockBitmap.UnlockBits(); }
/// <summary> /// 将灰度化的图像储存于grayImage中 /// </summary> static void ReadImage(Bitmap image) { _image = image; _width = image.Width; _height = image.Height; _grayImage = new int[image.Width, image.Height]; LockBitmap lockBitmap = new LockBitmap(image); lockBitmap.LockBits(); for (int i = 0; i < image.Width; i++) { for (int j = 0; j < image.Height; j++) { _grayImage[i, j] = (int)(0.3 * lockBitmap.GetPixel(i, j).R + 0.59 * lockBitmap.GetPixel(i, j).G + 0.11 * lockBitmap.GetPixel(i, j).B); } } lockBitmap.UnlockBits(); }
/// <summary> /// Canny检测 /// </summary> /// <param name="Input"></param> /// <param name="Edges"></param> /// <param name="KernelSize"></param> /// <param name="Sigma"></param> /// <param name="ThrHigh"></param> /// <param name="ThrLow"></param> public static void DetectCannyEdges(Bitmap Input, out Bitmap Edges, int KernelSize, float Sigma, int ThrHigh, int ThrLow) { ReadImage(Input); GenerateGaussianKernel(KernelSize, Sigma, out float[,] GaussianKernel); //高斯模糊处理 _grayImage = ConvolutionFilter2D(_grayImage, _width, _height, GaussianKernel, KernelSize); //利用算子获得梯度与梯度方向数组 Soble(_grayImage, _width, _height, out _gradient, out _angle); //非极大值抑制 Suppression(_image.Width, _height, _gradient, _angle); int[,] EdgesMap = new int[_width, _height]; //双阈值检测与滞后处理 HysterisisThresholding(_gradient, _width, _height, ThrHigh, ThrLow, ref EdgesMap); //获得处理后的图像 Edges = new Bitmap(_width, _height); LockBitmap lockBitmap = new LockBitmap(Edges); lockBitmap.LockBits(); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { if (EdgesMap[i, j] == 1) { lockBitmap.SetPixel(i, j, System.Drawing.Color.White); } else { lockBitmap.SetPixel(i, j, System.Drawing.Color.Black); } } } lockBitmap.UnlockBits(); }
/// <summary> /// 获得高斯过滤后的图像 /// </summary> /// <param name="Input"></param> /// <param name="Edges"></param> /// <param name="KernelSize"></param> /// <param name="Sigma"></param> public static void GaussianFilter(Bitmap Input, out Bitmap Edges, int KernelSize, float Sigma) { ReadImage(Input); GenerateGaussianKernel(KernelSize, Sigma, out float[,] GaussianKernel); //高斯模糊处理 _grayImage = ConvolutionFilter2D(_grayImage, _width, _height, GaussianKernel, KernelSize); Edges = new Bitmap(_width, _height); LockBitmap lockBitmap = new LockBitmap(Edges); lockBitmap.LockBits(); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { lockBitmap.SetPixel(i, j, Color.FromArgb(_grayImage[i, j], _grayImage[i, j], _grayImage[i, j])); } } lockBitmap.UnlockBits(); }