Example #1
0
        private void button7_Click(object sender, EventArgs e)
        {
            MatImage m1 = new MatImage(mimg);

            m1.Convert();

            mimgInGray = m1.Out();
            MatImage m2 = new MatImage(mimgInGray);

            m2.SmoothGaussian(3);
            m2.ThresholdBinaryInv(245, 255);
            // this causes an exception, and I don't know how to fix that yet.
            //The good news is that this really doesn't matter for small images
            //m2.MorphologyEx(2);
            mimgInGray = m2.Out();
            MatImage m3 = new MatImage(mimgInGray);
            VectorOfVectorOfPoint     contours = m3.FindContours();
            List <ColorfulContourMap> cmaps    = ColorfulContourMap.getAllContourMap(mimg, 0);



            //CvInvoke.DrawContours(mimg, contours,-1, new Bgr(255,0,0).MCvScalar,2);
            Mat mimg2 = new Mat(new Size(mimg.Width, mimg.Height), DepthType.Cv8U, 3);

            foreach (ColorfulContourMap cmap in cmaps)
            {
                cmap.DrawColorTo(mimg2);
            }
            pictureBox1.Image = mimg.Bitmap;

            pictureBox2.Image = mimg2.Bitmap;
        }
        // get all of the valid contour maps, valid means circumfence > 200 px
        // this was not in their code, I added this feature, but I used their logic
        public static List<ColorfulContourMap> getAllContourMap(Mat input, int index, int mode = 0)
        {
            // use for all members
            List<ColorfulContourMap> result = new List<ColorfulContourMap>();
            MatImage m1 = new MatImage(input);
            m1.Convert();
            Mat gray = m1.Out();
            // use for black background
            if (mode == 0)
            {
                MatImage m2 = new MatImage(gray);
                m2.SmoothGaussian(3);
                m2.ThresholdBinaryInv(245, 255);
                gray = m2.Out();
            }
            // use for white background
            else
            {
                MatImage m2 = new MatImage(gray);
                m2.SmoothGaussian(3);
                m2.ThresholdBinaryInv(100, 255);
                gray = m2.Out();
            }

            // one time use
            List<Point> pointList = new List<Point>();
            List<Point> polyPointList = new List<Point>();
            List<ColorfulPoint> cps = new List<ColorfulPoint>();
            List<ColorfulPoint> pcps = new List<ColorfulPoint>();

            // fetch all the contours using Emgu CV
            // fetch all the polys using Emgu CV
            // extract the points and colors

            Mat temp = gray.Clone();
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            CvInvoke.FindContours(gray, contours, new Mat(), RetrType.List, ChainApproxMethod.ChainApproxNone);

            double area = Math.Abs(CvInvoke.ContourArea(contours[0]));
            VectorOfPoint maxArea = contours[0]; // maxArea is used as the current contour
                                                 //contour = contour.HNext;
                                                 // use this to loop
            for (int i = 0; i < contours.Size; i++)
            {

                double nextArea = Math.Abs(CvInvoke.ContourArea(contours[i], false));  //  Find the area of contour
                area = nextArea;
                if (area >= Constants.MIN_AREA)
                {
                    maxArea = contours[i];
                    VectorOfPoint poly = new VectorOfPoint();
                    CvInvoke.ApproxPolyDP(maxArea, poly, 1.0, true);
                    pointList = maxArea.ToArray().ToList();
                    polyPointList = poly.ToArray().ToList();
                    foreach (Point p in pointList)
                    {
                        ColorfulPoint cp = new ColorfulPoint { X = p.X, Y = p.Y, color = extractPointColor(p, input) };
                        cps.Add(cp);
                    }
                    foreach (Point p in polyPointList)
                    {
                        ColorfulPoint cp = new ColorfulPoint { X = p.X, Y = p.Y, color = extractPointColor(p, input) };
                        pcps.Add(cp);
                    }
                    result.Add(new ColorfulContourMap(cps, pcps, index));
                    // clear temporal lists
                    pointList = new List<Point>();
                    polyPointList = new List<Point>();
                    cps = new List<ColorfulPoint>();
                    pcps = new List<ColorfulPoint>();

                }

            }

            return result;
        }
Example #3
0
        private void button7_Click(object sender, EventArgs e)
        {
            MatImage m1 = new MatImage(mimg);
            m1.Convert();

            mimgInGray = m1.Out();
            MatImage m2 = new MatImage(mimgInGray);
            m2.SmoothGaussian(3);
            m2.ThresholdBinaryInv(245, 255);
            // this causes an exception, and I don't know how to fix that yet.
            //The good news is that this really doesn't matter for small images
            //m2.MorphologyEx(2);
            mimgInGray = m2.Out();
            MatImage m3 = new MatImage(mimgInGray);
            VectorOfVectorOfPoint contours = m3.FindContours();
            List<ColorfulContourMap> cmaps = ColorfulContourMap.getAllContourMap(mimg, 0);

            //CvInvoke.DrawContours(mimg, contours,-1, new Bgr(255,0,0).MCvScalar,2);
            Mat mimg2 =new Mat(new Size(mimg.Width,mimg.Height),DepthType.Cv8U,3);
            foreach(ColorfulContourMap cmap in cmaps)
            {
                cmap.DrawColorTo(mimg2);
            }
            pictureBox1.Image = mimg.Bitmap;

            pictureBox2.Image = mimg2.Bitmap;
        }
Example #4
0
        // get all of the valid contour maps, valid means circumfence > 200 px
        // this was not in their code, I added this feature, but I used their logic
        public static List <ColorfulContourMap> getAllContourMap(Mat input, int index, int mode = 0)
        {
            // use for all members
            List <ColorfulContourMap> result = new List <ColorfulContourMap>();
            MatImage m1 = new MatImage(input);

            m1.Convert();
            Mat gray = m1.Out();

            // use for black background
            if (mode == 0)
            {
                MatImage m2 = new MatImage(gray);
                m2.SmoothGaussian(3);
                m2.ThresholdBinaryInv(245, 255);
                gray = m2.Out();
            }
            // use for white background
            else
            {
                MatImage m2 = new MatImage(gray);
                m2.SmoothGaussian(3);
                m2.ThresholdBinaryInv(100, 255);
                gray = m2.Out();
            }


            // one time use
            List <Point>         pointList     = new List <Point>();
            List <Point>         polyPointList = new List <Point>();
            List <ColorfulPoint> cps           = new List <ColorfulPoint>();
            List <ColorfulPoint> pcps          = new List <ColorfulPoint>();

            // fetch all the contours using Emgu CV
            // fetch all the polys using Emgu CV
            // extract the points and colors

            Mat temp = gray.Clone();
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            CvInvoke.FindContours(gray, contours, new Mat(), RetrType.List, ChainApproxMethod.ChainApproxNone);

            double        area    = Math.Abs(CvInvoke.ContourArea(contours[0]));
            VectorOfPoint maxArea = contours[0]; // maxArea is used as the current contour

            //contour = contour.HNext;
            // use this to loop
            for (int i = 0; i < contours.Size; i++)
            {
                double nextArea = Math.Abs(CvInvoke.ContourArea(contours[i], false));  //  Find the area of contour
                area = nextArea;
                if (area >= Constants.MIN_AREA)
                {
                    maxArea = contours[i];
                    VectorOfPoint poly = new VectorOfPoint();
                    CvInvoke.ApproxPolyDP(maxArea, poly, 1.0, true);
                    pointList     = maxArea.ToArray().ToList();
                    polyPointList = poly.ToArray().ToList();
                    foreach (Point p in pointList)
                    {
                        ColorfulPoint cp = new ColorfulPoint {
                            X = p.X, Y = p.Y, color = extractPointColor(p, input)
                        };
                        cps.Add(cp);
                    }
                    foreach (Point p in polyPointList)
                    {
                        ColorfulPoint cp = new ColorfulPoint {
                            X = p.X, Y = p.Y, color = extractPointColor(p, input)
                        };
                        pcps.Add(cp);
                    }
                    result.Add(new ColorfulContourMap(cps, pcps, index));
                    // clear temporal lists
                    pointList     = new List <Point>();
                    polyPointList = new List <Point>();
                    cps           = new List <ColorfulPoint>();
                    pcps          = new List <ColorfulPoint>();
                }
            }



            return(result);
        }