예제 #1
0
        public List <Contour> Contours(RetrType retrievalMode = RetrType.Tree, ChainApproxMethod approximationMethod = ChainApproxMethod.ChainApproxSimple)
        {
            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            Mat hierarchy = new Mat();

            CvInvoke.FindContours(this.Data, contours, hierarchy, retrievalMode, approximationMethod);

            Point[][] contourArray = contours.ToArrayOfArray();
            int[,,] hierarchyArray = (int[, , ])hierarchy.GetData();

            List <Contour> output = new List <Contour>();

            for (int i = 0; i < contourArray.Length; i++)
            {
                Contour c = new Contour(contourArray[i])
                {
                    Hierarchy = new int[] {
                        hierarchyArray[0, i, 0],
                        hierarchyArray[0, i, 1],
                        hierarchyArray[0, i, 2],
                        hierarchyArray[0, i, 3],
                    }
                };

                output.Add(c);
            }

            return(output);
        }
        public static Tuple <VectorOfVectorOfPoint, Mat> FindContours
        (
            Image <Gray, byte> inImage
            , RetrType retrType = RetrType.External
            , ChainApproxMethod chainApproxMethod = ChainApproxMethod.ChainApproxSimple)
        {
            var contours  = new VectorOfVectorOfPoint();
            var hierarchy = new Mat();

            CvInvoke.FindContours(inImage, contours, hierarchy, retrType, chainApproxMethod);

            return(new Tuple <VectorOfVectorOfPoint, Mat>(contours, hierarchy));
        }
예제 #3
0
        public static List <VectorOfPoint> FindContours(Image <Gray, byte> image, RetrType retrType = RetrType.List, ChainApproxMethod chainApproxMethod = ChainApproxMethod.ChainApproxSimple)
        {
            // Contours
            VectorOfVectorOfPoint contoursDetected = new VectorOfVectorOfPoint();

            CvInvoke.FindContours(image, contoursDetected, null, retrType, chainApproxMethod);
            var contoursArray = new List <VectorOfPoint>();
            int count         = contoursDetected.Size;

            for (int i = 0; i < count; i++)
            {
                using (VectorOfPoint currContour = contoursDetected[i])
                {
                    contoursArray.Add(currContour);
                }
            }

            return(contoursArray);
        }
예제 #4
0
        public static List<VectorOfPoint> GetContours(Image<Gray, Byte> image, ChainApproxMethod apxMethod = ChainApproxMethod.ChainApproxSimple, RetrType retrievalType = RetrType.List, double accuracy = 0.001d, double minimumArea = 10)
        {
            List<VectorOfPoint> convertedContours = new List<VectorOfPoint>();

            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            {
                using (Image<Gray, Byte> tempImage = image.Copy())
                {
                    CvInvoke.FindContours(tempImage, contours, null, retrievalType, apxMethod);
                }

                int count = contours.Size;
                for (int i = 0; i < count; i++)
                {
                    using (VectorOfPoint contour = contours[i])
                    {
                        VectorOfPoint approxContour = new VectorOfPoint();
                        CvInvoke.ApproxPolyDP(contour, approxContour, accuracy, false);
                        if (CvInvoke.ContourArea(approxContour, false) > minimumArea)
                        {
                            convertedContours.Add(approxContour);
                        }
                    }
                }
            }

            return convertedContours;
        }
예제 #5
0
        public static List <VectorOfPoint> GetContours(Image <Gray, Byte> image, ChainApproxMethod apxMethod = ChainApproxMethod.ChainApproxSimple, RetrType retrievalType = RetrType.List, double accuracy = 0.001d, double minimumArea = 10)
        {
            List <VectorOfPoint> convertedContours = new List <VectorOfPoint>();

            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
            {
                using (Image <Gray, Byte> tempImage = image.Copy())
                {
                    CvInvoke.FindContours(tempImage, contours, null, retrievalType, apxMethod);
                }

                int count = contours.Size;
                for (int i = 0; i < count; i++)
                {
                    using (VectorOfPoint contour = contours[i])
                    {
                        VectorOfPoint approxContour = new VectorOfPoint();
                        CvInvoke.ApproxPolyDP(contour, approxContour, accuracy, false);
                        if (CvInvoke.ContourArea(approxContour, false) > minimumArea)
                        {
                            convertedContours.Add(approxContour);
                        }
                    }
                }
            }

            return(convertedContours);
        }
 private void ContouringMode_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
 {
     contouringMode = (RetrType)ContouringMode.SelectedValue;
 }
예제 #7
0
        //Function used to implement the CvInvoke.FindContours method
        public VectorOfVectorOfPoint FindContours(Image <Gray, byte> image, ChainApproxMethod method = ChainApproxMethod.ChainApproxSimple, RetrType type = RetrType.List)
        {
            // Check that all parameters are valid.
            VectorOfVectorOfPoint result = new VectorOfVectorOfPoint();

            if (method == Emgu.CV.CvEnum.ChainApproxMethod.ChainCode)
            {
                //throw new ColsaNotImplementedException("Chain Code not implemented, sorry try again later");
            }

            CvInvoke.FindContours(image, result, null, type, method);
            return(result);
        }
예제 #8
0
파일: NodeLibrary.cs 프로젝트: okeanz/IPS
        static object[] FindContours(Bitmap img, [InField(PVal = 25)] float MinContourSize, [InField(PVal = 500)] float MaxContourSize, [InField(PVal = RetrType.List)] RetrType retrType, [InField(PVal = ChainApproxMethod.ChainApproxNone)] ChainApproxMethod cam, [InField(PVal = true)] bool DrawContours, out string time, out BitmapImage drawedCs)
        {
            var inp      = new Image <Bgr, byte>(img);
            var onlyCont = new Image <Bgra, byte>(inp.Size);
            var inpGray  = new Image <Gray, byte>(img);
            var conts    = new VectorOfVectorOfPoint();
            var now      = DateTime.Now;

            CvInvoke.FindContours(inpGray, conts, null, retrType, cam);

            time = "Find Time: " + (DateTime.Now - now).TotalMilliseconds;

            List <VectorOfPoint> contsList = new List <VectorOfPoint>();
            var cout = new List <VectorOfPoint>();

            for (int i = 0; i < conts.Size; i++)
            {
                var con  = conts[i];
                var area = CvInvoke.ContourArea(con);
                if (con.Size > 2)
                {
                    if (area >= MinContourSize && area < MaxContourSize)
                    {
                        contsList.Add(con);
                        cout.Add(new VectorOfPoint(con.ToArray()));
                    }
                }
            }



            now = DateTime.Now;

            var parts = new List <Bitmap>();

            Bitmap[,] outpArr = null;
            var moments = new List <string>();

            double[,] oArr = null;

            if (DrawContours)
            {
                for (int i = 0; i < contsList.Count; i++)
                {
                    VectorOfPoint contour = contsList[i];
                    var           convex  = contour.ToArray();//CvInvoke.ConvexHull(contour.ToArray().Select(x => new PointF(x.X, x.Y)).ToArray());
                    var           pnt     = convex.Select(x => new System.Drawing.Point((int)x.X, (int)x.Y)).ToArray();
                    inp.Draw(pnt, new Bgr(0, 255, 0), 1);

                    onlyCont.Draw(contour.ToArray(), new Bgra(255, 255, 255, 255), 1);
                }
                for (int i = 0; i < contsList.Count; i++)
                {
                    VectorOfPoint        contour = contsList[i];
                    var                  cx      = contour.ToArray().Sum(x => x.X) / contour.Size;
                    var                  cy      = contour.ToArray().Sum(x => x.Y) / contour.Size;
                    System.Drawing.Point center  = new System.Drawing.Point(cx, cy);
                    inp.Draw(string.Format("[{0}|{1}|{2}]", i, CvInvoke.ContourArea(contour), contour.Size), center, FontFace.HersheyPlain, 1, new Bgr(0, 0, 255), 1);
                }

                for (int i = 0; i < contsList.Count; i++)
                {
                    VectorOfPoint contour = contsList[i];
                    var           cArray  = contour.ToArray();
                    var           left    = cArray.Min(x => x.X);
                    var           right   = cArray.Max(x => x.X);
                    var           top     = cArray.Min(x => x.Y);
                    var           bot     = cArray.Max(x => x.Y);
                    var           width   = right - left;
                    var           height  = bot - top;

                    int size = 500;

                    Image <Bgr, byte> part = new Image <Bgr, byte>(new System.Drawing.Size(width > size ? width : size, height > size ? height : size));

                    for (int k = 0; k < cArray.Length; k++)
                    {
                        cArray[k].X -= left;
                        cArray[k].Y -= top;
                    }
                    part.Draw(cArray, new Bgr(Color.White));


                    part.Draw(string.Format("[num: {0}; area: {1}; size: {2}]", i, CvInvoke.ContourArea(contour), contour.Size), new System.Drawing.Point(0, part.Height - 5), FontFace.HersheyPlain, 1, new Bgr(0, 0, 255), 1);



                    MCvMoments cvmoments = CvInvoke.Moments(contour);
                    var        ncmoment  = CvInvoke.cvGetNormalizedCentralMoment(ref cvmoments, 0, 0);
                    var        cmom      = CvInvoke.cvGetCentralMoment(ref cvmoments, 0, 0);
                    var        smom      = CvInvoke.cvGetSpatialMoment(ref cvmoments, 0, 0);



                    moments.Add(string.Format("CM: {0} || NCM: {1} || SM: {2}", cmom, ncmoment, smom));



                    parts.Add(new Bitmap(part.Bitmap));
                }
            }
            time = time + "|| Draw Time: " + (DateTime.Now - now).TotalMilliseconds + " Contours: " + contsList.Count;

            if (DrawContours)
            {
                drawedCs = B2BI(inp.Bitmap);
            }
            else
            {
                drawedCs = null;
            }

            return(new object[] { new Bitmap(inp.Bitmap), new Bitmap(onlyCont.Bitmap), parts.ToArray(), moments.ToArray(), cout.ToArray() });
        }