Beispiel #1
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);
        }
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/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 #4
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 #5
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 #6
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);
        }
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 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);
        }
Beispiel #9
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);
        }