Beispiel #1
0
        /// <summary>
        /// foamliu, 2009/02/01, 动态或自适应阈值化, 与局部背景差别大于 gdiff 的点进入ROI.
        ///
        /// </summary>
        /// <param name="mat"></param>
        /// <param name="gdiff"></param>
        /// <param name="k">用于模糊的K-均值的k</param>
        public static void ThresholdDynamic(int[][] mat, int gdiff, int k)
        {
            int width  = mat.Length;
            int height = mat[0].Length;

            int[][] newImg;

            ConvKernel kernel = ConvKernel.GetKMeanKernal(k);

            Convolution conv = new Convolution();

            conv.Calculate(mat, kernel, out newImg);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int g    = mat[x][y];
                    int newg = newImg[x][y];
                    if (Math.Abs(g - newg) >= gdiff)
                    {
                        // foamliu, 2009/01/31, 感兴趣的部分1.
                        mat[x][y] = 1;
                    }
                    else
                    {
                        // foamliu, 2009/01/31, 不感兴趣的部分0.
                        mat[x][y] = 0;
                    }
                }
            }
        }
Beispiel #2
0
        public static Bitmap Gaussian(Bitmap bmp, double sigma)
        {
            int width, height;

            int[][][] img, newImg;
            Bitmap    newBmp;

            ImageConvert.Bitmap2MatColor(bmp, out img, out width, out height);

            newImg = new int[3][][];

            int        n      = 4;
            ConvKernel kernel = Util.GetGaussianKernal(sigma, n);

            Convolution conv = new Convolution();

            // RGB
            conv.Calculate(img[0], kernel, out newImg[0]);
            conv.Calculate(img[1], kernel, out newImg[1]);
            conv.Calculate(img[2], kernel, out newImg[2]);

            ImageConvert.Mat2BitmapColor(newImg, width, height, out newBmp);

            return(newBmp);
        }
Beispiel #3
0
        /// <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);
        }
Beispiel #4
0
        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);
        }
Beispiel #5
0
        /// <summary>
        /// foamliu, 2009/02/11, 计算梯度.
        ///
        /// </summary>
        /// <param name="mat"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="kernel"></param>
        /// <returns></returns>
        public static double Gradient(int[][] mat, int x, int y, ConvKernel kernel)
        {
            int m = kernel.M;
            int n = kernel.N;

            double sum = 0;

            for (int j = -n; j <= n; j++)
            {
                for (int i = -m; i <= m; i++)
                {
                    sum += Util.GetPixel(mat, x - i, y - j) * kernel.Filter(i, j);
                }
            }
            return(sum);
        }
Beispiel #6
0
        /// <summary>
        /// foamliu, 2009/01/29, 构建制定 sigma, (2n+1) × (2n+1) 的高斯核.
        ///
        /// </summary>
        /// <param name="sigma"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static ConvKernel GetGaussianKernal(double sigma, int n)
        {
            double[][] filter = BuildMat(2 * n + 1, 2 * n + 1);

            for (int j = 0; j <= 2 * n; j++)
            {
                for (int i = 0; i <= 2 * n; i++)
                {
                    filter[i][j] = GetGaussian(sigma, i - n) * GetGaussian(sigma, j - n);
                }
            }

            ConvKernel conv = new ConvKernel(n, n, filter);

            return(conv);
        }
Beispiel #7
0
        public static Bitmap K_Mean(Bitmap bmp, int k)
        {
            int width, height;

            int[][] img, filtered;
            Bitmap  newBmp;

            ImageConvert.Bitmap2Mat(bmp, out img, out width, out height);

            ConvKernel kernel = ConvKernel.GetKMeanKernal(k);

            Convolution conv = new Convolution();

            conv.Calculate(img, kernel, out filtered);

            ImageConvert.Mat2Bitmap(filtered, width, height, out newBmp);

            return(newBmp);
        }
Beispiel #8
0
        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);
        }
Beispiel #9
0
        public static Bitmap K_Mean(Bitmap bmp, int k)
        {
            int width, height;

            int[][][] img, newImg;
            Bitmap    newBmp;

            ImageConvert.Bitmap2MatColor(bmp, out img, out width, out height);

            newImg = new int[3][][];

            ConvKernel kernel = ConvKernel.GetKMeanKernal(k);

            Convolution conv = new Convolution();

            // RGB
            conv.Calculate(img[0], kernel, out newImg[0]);
            conv.Calculate(img[1], kernel, out newImg[1]);
            conv.Calculate(img[2], kernel, out newImg[2]);

            ImageConvert.Mat2BitmapColor(newImg, width, height, out newBmp);

            return(newBmp);
        }
 public ConvolutionOperatorVisitable(ConvKernel filter)
 {
     _filter = filter;
 }
 public IConvolutionVisitable Get(ConvKernel filter)
 => filter
 switch
 {
 public virtual IConvolutionKernel Get(ConvKernel model)
 => _factory.Get(model);
Beispiel #13
0
 public Bitmap Operator(Bitmap bmp, ConvKernel filter)
 => _cache.GetOrCreate(filter,
                       () => _convolution.Convolution(bmp, _factory.Get(filter))
                       );
Beispiel #14
0
 /// <summary>
 /// A factory method
 /// where the <see cref="ConvKernel"/> represents an
 /// enumeration for the types implementing the <see cref="IConvolutionKernel"/>.
 /// </summary>
 public IConvolutionKernel Get(ConvKernel filter)
 => filter
 switch
 {
 /// <inheritdoc/>
 public Bitmap ApplyFilter(Bitmap bmp, ConvKernel filter)
 => _factory.Get(filter).Accept(_visitor).Filter(bmp);