Пример #1
0
        public ConnectedComponents(Bitmap bmp)
        {
            m_bmp = bmp;

            ImageConvert.Bitmap2Mat(bmp, out m_img, out m_width, out m_height);
            m_newImg = Util.BuildMatInt(m_width, m_height);
        }
Пример #2
0
        /// <summary>
        /// foamliu, 2009/01/31, 缩放.
        /// </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.Bitmap2Mat(bmp, out img, out width, out height);

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

            newImg = Util.BuildMatInt(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))
                    {
                        newImg[x][y] = img[oldX][oldY];
                    }
                }
            }

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

            return(newBmp);
        }
Пример #3
0
        /// <summary>
        /// foamliu, 2009/01/31, 平移.
        /// </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.Bitmap2Mat(bmp, out img, out width, out height);
            newImg = Util.BuildMatInt(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))
                    {
                        newImg[x][y] = img[oldX][oldY];
                    }
                }
            }

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

            return(newBmp);
        }
Пример #4
0
        public static void RetrieveFeatureSet(Bitmap bmp, out GrayValueFeatures features)
        {
            int width, height;

            int[][] mat;
            ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height);
            features = new GrayValueFeatures(mat);
            features.CalcBasicFeatures();
        }
Пример #5
0
        public IntegralImage(Bitmap bmp)
        {
            int[][] originalImage;
            ImageConvert.Bitmap2Mat(bmp, out originalImage, out m_width, out m_height);

            m_mat = Util.BuildMat(m_width, m_height);

            Calculate(originalImage);
        }
Пример #6
0
        /// <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);
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        // foamliu, 2009/02/01, 处理后所有点的灰度值有:
        //     gmin <= g <= gmax
        //
        //  Notes: 或者为0.
        public static Bitmap ThresholdSimple(Bitmap bmp, int gmin, int gmax)
        {
            int width, height;

            int[][] mat;
            Bitmap  newBmp;

            ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height);
            ThresholdLib.ThresholdSimple(mat, gmin, gmax);
            ImageConvert.Mat2Bitmap(mat, width, height, out newBmp);

            return(newBmp);
        }
Пример #9
0
        /// <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);
        }
Пример #10
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);
        }
Пример #11
0
        /// <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);
        }
Пример #12
0
        /// <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);
        }
Пример #13
0
        /// <summary>
        /// foamliu, 2009/02/09, 傅立叶变换
        ///
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static Bitmap Fourier(Bitmap bmp, NComputerVision.GraphicsLib.FourierTransform.Direction dire)
        {
            int width, height;

            int[][] mat, filtered1, filtered2;
            Bitmap  newBmp;

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

            filtered1 = FourierTransform.Transform(mat, dire);
            filtered2 = Util.LogarithmicTransform(filtered1);

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

            return(newBmp);
        }
Пример #14
0
        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);
        }
Пример #15
0
        //public Bitmap Scharr(Bitmap bmp)
        //{
        //    int width, height;
        //    int[][] img, newImg;
        //    Bitmap newBmp;
        //    Util.Bitmap2Mat(bmp, out img, out width, out height);

        //    Convolution conv = new Convolution();
        //    conv.Calculate2Knl(img, ConvKernel.Scharr_Gx, ConvKernel.Scharr_Gy, out newImg);

        //    Util.Mat2Bitmap(newImg, width, height, out newBmp);

        //    return newBmp;
        //}



        public static Bitmap Prewitt(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.Prewitt_Gx, ConvKernel.Prewitt_Gy, out filtered, ConvNorm.Norm_1);

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

            return(newBmp);
        }
Пример #16
0
        /// <summary>
        /// foamliu, 2009/02/09, 二值图像开操作
        ///
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static Bitmap BinaryOpen(Bitmap bmp)
        {
            int width, height;

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

            ImageConvert.Bitmap2Mat(bmp, out mat, out width, out height);
            ImageConvert.GrayValue2Binary(mat);

            BinaryImageLib.Open(mat, StructuringElement.N4, out filtered);

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

            return(newBmp);
        }
Пример #17
0
        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);
        }
Пример #18
0
        public static Bitmap ThresholdDynamic(Bitmap bmp, int gdiff, int k)
        {
            int width, height;

            int[][] mat;
            Bitmap  newBmp;

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

            ThresholdLib.ThresholdDynamic(mat, gdiff, k);

            // foamliu, 2009/02/04, 阈值化的结果是二值图像, 需要转化为可显示的灰度图.
            ImageConvert.Binary2GrayValue(mat);

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

            return(newBmp);
        }
Пример #19
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);
        }
Пример #20
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);
        }
Пример #21
0
        /// <summary>
        /// foamliu, 2009/03/05, 镜像.
        ///
        /// </summary>
        /// <returns></returns>
        public static Bitmap Mirror(Bitmap bmp)
        {
            int width, height;

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

            ImageConvert.Bitmap2Mat(bmp, out img, out width, out height);
            newImg = Util.BuildMatInt(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    newImg[x][y] = img[width - 1 - x][y];
                }
            }

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

            return(newBmp);
        }
Пример #22
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);
        }
Пример #23
0
        /// <summary>
        /// foamliu, 2009/01/31, 旋转.
        /// </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.Bitmap2Mat(bmp, out img, out width, out height);
            newImg = Util.BuildMatInt(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))
                        {
                            newImg[x][y] = img[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;
                            int    g00 = img[p00X][p00Y];
                            int    g01 = img[p01X][p01Y];
                            int    g10 = img[p10X][p10Y];
                            int    g11 = img[p11X][p11Y];
                            double g   = b * (a * g11 + (1 - a) * g01) + (1 - b) * (a * g01 + (1 - a) * g00);
                            newImg[x][y] = (byte)Math.Round(g);
                        }
                    }
                }
            }

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

            return(newBmp);
        }