Exemplo n.º 1
0
        /// <summary>
        /// foamliu, 2009/02/02, 彩色图片缩放.
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="sr"></param>
        /// <param name="sc"></param>
        /// <returns></returns>
        public static Bitmap Scale(Bitmap bmp, double sr, double sc)
        {
            int width, height;
            int[][][] img, newImg;
            Bitmap newBmp;
            ImageConvert.Bitmap2MatColor(bmp, out img, out width, out height);

            int newW = (int)(width * sc);
            int newH = (int)(height * sr);

            newImg = Util.BuildMatIntColor(newW, newH);
            for (int y = 0; y < newH; y++)
            {
                for (int x = 0; x < newW; x++)
                {
                    double[] input = new double[] { y, x, 1 };
                    double[] output = NcvMatrix.ScaleInvert(input, sr, sc);
                    int oldY = (int)Math.Round(output[0]);
                    int oldX = (int)Math.Round(output[1]);
                    if (Util.InRect(oldX, oldY, width, height))
                    {
                        // RGB
                        newImg[0][x][y] = img[0][oldX][oldY];
                        newImg[1][x][y] = img[1][oldX][oldY];
                        newImg[2][x][y] = img[2][oldX][oldY];
                    }
                }
            }

            ImageConvert.Mat2BitmapColor(newImg, newW, newH, out newBmp);

            return newBmp;
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// foamliu, 2009/02/02, 彩色图片平移.
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="tr"></param>
        /// <param name="tc"></param>
        /// <returns></returns>
        public static Bitmap Translate(Bitmap bmp, double tr, double tc)
        {
            int width, height;
            int[][][] img, newImg;
            Bitmap newBmp;
            ImageConvert.Bitmap2MatColor(bmp, out img, out width, out height);
            newImg = Util.BuildMatIntColor(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    double[] input = new double[] { y, x, 1 };
                    double[] output = NcvMatrix.TranslateInvert(input, tr, tc);
                    int oldY = (int)Math.Round(output[0]);
                    int oldX = (int)Math.Round(output[1]);
                    if (Util.InRect(oldX, oldY, width, height))
                    {
                        // RGB
                        newImg[0][x][y] = img[0][oldX][oldY];
                        newImg[1][x][y] = img[1][oldX][oldY];
                        newImg[2][x][y] = img[2][oldX][oldY];
                    }
                }
            }

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

            return newBmp;
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        public static Bitmap Median(Bitmap bmp, int k)
        {
            int width, height;

            int[][][] mat, filtered = new int[3][][];
            Bitmap    newBmp;

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

            filtered[0] = Util.Median(mat[0], k);
            filtered[1] = Util.Median(mat[1], k);
            filtered[2] = Util.Median(mat[2], k);

            //GrayValueImageLib.Normalize(filtered[0]);
            //GrayValueImageLib.Normalize(filtered[1]);
            //GrayValueImageLib.Normalize(filtered[2]);

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

            return(newBmp);
        }
Exemplo n.º 7
0
        public static Bitmap Sobel(Bitmap bmp)
        {
            int width, height;

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

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

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

            Convolution conv = new Convolution();

            // RGB
            conv.CalculateEdge(img[0], ConvKernel.Sobel_Gx, ConvKernel.Sobel_Gy, out newImg[0], ConvNorm.Norm_1);
            conv.CalculateEdge(img[1], ConvKernel.Sobel_Gx, ConvKernel.Sobel_Gy, out newImg[1], ConvNorm.Norm_1);
            conv.CalculateEdge(img[2], ConvKernel.Sobel_Gx, ConvKernel.Sobel_Gy, out newImg[2], ConvNorm.Norm_1);

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

            return(newBmp);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        /// <summary>
        /// foamliu, 2009/02/02, 彩色图片旋转.
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="alpha">旋转角度(弧度)</param>
        /// <param name="bi">bilinear interpolation (双线性插值)</param>
        /// <returns></returns>
        public static Bitmap Rotate(Bitmap bmp, double alpha, bool bi)
        {
            int width, height;
            int[][][] img, newImg;
            Bitmap newBmp;
            ImageConvert.Bitmap2MatColor(bmp, out img, out width, out height);
            newImg = Util.BuildMatIntColor(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (!bi)
                    {
                        double[] input = new double[] { y, x, 1 };
                        double[] output = NcvMatrix.RotateInvert(input, alpha);
                        int oldY = (int)Math.Round(output[0]);
                        int oldX = (int)Math.Round(output[1]);
                        if (Util.InRect(oldX, oldY, width, height))
                        {
                            // RGB
                            newImg[0][x][y] = img[0][oldX][oldY];
                            newImg[1][x][y] = img[1][oldX][oldY];
                            newImg[2][x][y] = img[2][oldX][oldY];
                        }
                    }
                    else
                    {
                        // p00 ------------- p01
                        //  |                 |
                        // p10 ------------- p11
                        //
                        // 双线性插值需要更长的计算时间 (原来的5倍以上)
                        //
                        double[] input = new double[] { y, x, 1 };
                        double[] output = NcvMatrix.RotateInvert(input, alpha);
                        double newY = output[0];
                        double newX = output[1];
                        int p00Y = (int)Math.Floor(newY);
                        int p00X = (int)Math.Floor(newX);
                        int p01Y = (int)Math.Floor(newY);
                        int p01X = (int)Math.Ceiling(newX);
                        int p10Y = (int)Math.Ceiling(newY);
                        int p10X = (int)Math.Floor(newX);
                        int p11Y = (int)Math.Ceiling(newY);
                        int p11X = (int)Math.Ceiling(newX);

                        if (Util.InRect(p00X, p00Y, width, height)
                            && Util.InRect(p01X, p01Y, width, height)
                            && Util.InRect(p10X, p10Y, width, height)
                            && Util.InRect(p11X, p11Y, width, height))
                        {
                            double a = newY - p00Y;
                            double b = newX - p00X;
                            for (int c = 0; c < 3; c++)
                            {
                                int g00 = img[c][p00X][p00Y];
                                int g01 = img[c][p01X][p01Y];
                                int g10 = img[c][p10X][p10Y];
                                int g11 = img[c][p11X][p11Y];
                                double g = b * (a * g11 + (1 - a) * g01) + (1 - b) * (a * g01 + (1 - a) * g00);
                                newImg[c][x][y] = (byte)Math.Round(g);
                            }
                        }
                    }
                }
            }

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

            return newBmp;
        }