예제 #1
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);
        }
예제 #2
0
        public Snake2(int[][] mat, int number)
        {
            this.m_Cols             = mat.Length;
            this.m_Rows             = mat[0].Length;
            this.no_of_snake_points = number;

            Snake_points = new SubpixelContour();
            avg_distance = 0.0;

            //mat = GrayScaleProcessor.Gaussian(mat, 1.0);

            Convolution conv = new Convolution();

            conv.CalculateEdge(mat, ConvKernel.Sobel_Gx, ConvKernel.Sobel_Gy, out grad_mag, ConvNorm.Norm_2);
            //grad_mag = mat;
        }
예제 #3
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);
        }
예제 #4
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);
        }
예제 #5
0
        public override double[] DoWork(System.Drawing.Image image, string archivePath)
        {
            Bitmap             background = null;
            List <double>      feature    = new List <double>();
            List <DoublePoint> contour    = null;
            Graphics           g          = null;
            Bitmap             archiveBmp = null;

            try
            {
                background = new Bitmap(NcvGlobals.Background_Size, NcvGlobals.Background_Size, PixelFormat.Format24bppRgb);
                g          = Graphics.FromImage(background);
                g.FillRectangle(new SolidBrush(Color.White), 0, 0, background.Width, background.Height);

                int imageDisplayWidth  = Convert.ToInt32(image.Width * (g.DpiX / image.HorizontalResolution));
                int imageDisplayHeight = Convert.ToInt32(image.Height * (g.DpiY / image.VerticalResolution));

                double ratio = 1.0d * imageDisplayWidth / imageDisplayHeight;
                double ww    = Math.Sqrt(ratio * ratio / (1 + ratio * ratio));
                double hh    = Math.Sqrt(1 / (1 + ratio * ratio));
                double xx    = (1 - ww) / 2;
                double yy    = (1 - hh) / 2;

                int x = Convert.ToInt32(xx * NcvGlobals.Background_Size);
                int y = Convert.ToInt32(yy * NcvGlobals.Background_Size);
                int w = Convert.ToInt32(ww * NcvGlobals.Background_Size);
                int h = Convert.ToInt32(hh * NcvGlobals.Background_Size);

                Rectangle rect = new Rectangle(x, y, w, h);

                g.DrawImage(image, rect);

                int[][] mat;

                mat = ImageConvert.Bitmap2GreenScale(background);
                ThresholdLib.ThresholdSimple(mat, NcvGlobals.ThresholdMin, NcvGlobals.ThresholdMax);

                mat = GrayScaleImageLib.Open(mat, StructuringElement.N4);
                mat = GrayScaleImageLib.Close(mat, StructuringElement.N8);

                Convolution conv = new Convolution();
                conv.CalculateEdge(mat, ConvKernel.Sobel_Gx, ConvKernel.Sobel_Gy, out mat, ConvNorm.Norm_2);

                GrayScaleImageLib.Normalize(mat);

                GrayValueFeatures features = new GrayValueFeatures(mat);
                features.CalcBasicFeatures();
                features.CalcMomentFeatures();

                int[][] rotated = GrayScaleAffineTransformation.Rotate(mat, -features.AngleOfEllipse, new DoublePoint(features.CenterX, features.CenterY), 0);

                // ======== Shape Descriptor ========
                if (NcvGlobals.FeatureExtractionType == FeatureExtractionType.Snake)
                {
                    Snake2 s = new Snake2(rotated, NcvGlobals.NumDimension);
                    s.InitCircle();
                    contour = s.DoIt();
                }
                else
                {
                    RegionContourStitching s    = new RegionContourStitching(rotated);
                    List <DoublePoint>     temp = s.DoIt(rotated.Length / 2, 0);
                    contour = NcvVector.interp1(temp, NcvGlobals.NumDimension);
                }

                double xc = 0;
                double yc = 0;

                for (int i = 0; i < contour.Count; i++)
                {
                    xc += contour[i].X / contour.Count;
                    yc += contour[i].Y / contour.Count;
                }

                Complex[] data = new Complex[NcvGlobals.NumDimension];
                for (int i = 0; i < NcvGlobals.NumDimension; i++)
                {
                    double r = Math.Sqrt((contour[i].X - xc) * (contour[i].X - xc) + (contour[i].Y - yc) * (contour[i].Y - yc));
                    //data[i] = new Complex(contour[i].X - xc, contour[i].Y - yc);
                    data[i] = new Complex(r, 0);
                }
                FourierTransform.FFT(data, FourierTransform.Direction.Forward);

                for (int i = 1; i < NcvGlobals.NumDimension; i++)
                {
                    double v = data[i].Magnitude / data[0].Magnitude;
                    feature.Add(v);
                }

                // ======== Archiving ========
                archiveBmp = ImageConvert.Mat2Bitmap(rotated);
                NComputerVision.Common.NcvDrawing.DrawContour(archiveBmp, contour);
                if (!Directory.Exists(@"D:\achive\"))
                {
                    Directory.CreateDirectory(@"D:\achive\");
                }
                //archiveBmp.Save(@"D:\achive\" + Path.GetFileName(path));
                archiveBmp.Save(archivePath);
            }
            finally
            {
                if (background != null)
                {
                    background.Dispose();
                }
                if (archiveBmp != null)
                {
                    archiveBmp.Dispose();
                }
                if (g != null)
                {
                    g.Dispose();
                }
                if (image != null)
                {
                    image.Dispose();
                }
            }

            return(feature.ToArray());
        }