简体   繁体   中英

EMGUCV findContours how to get the points themselves?

Using the findContours method I'm ultimately able to outline the human figure.

My sample pictures were used from the creator AForge.Net's website. Using an absdiff along with findContours I'm able to use CvInvoke.cvDrawContours to actually draw the contours themselves to the screen.

What I would like, however, is access to the points that are being used to draw those contours.

In reference to the image below I want to get those points making up the blue contours. There has to be some way to get to those no?

This is the relevant code:

    Image<Gray, byte> grayImage = new Image<Gray, byte>(colorImage);
    Image<Bgr, byte> color = new Image<Bgr, byte>(colorImage);

    Image<Bgr, byte> whiteconverter = new Image<Bgr, byte>(blankImage);

    grayImage = grayImage.ThresholdBinary(new Gray(60), new Gray(255));

    grayImage._Not();

    using (MemStorage storage = new MemStorage())
    {
        //add points to listbox
        using (var p2 = new Pen(Color.Yellow, 2))
        {
            var grp = Graphics.FromImage(pictureBox3.Image);

            for (Contour<Point> contours = grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
            {

                Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.015, storage);
                CvInvoke.cvDrawContours(whiteconverter, contours, new MCvScalar(255), new MCvScalar(255), -1, 2, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));

                pictureBox3.Image = whiteconverter.Bitmap;
            }
        }
    }

在此处输入图片说明

The contours are a Vector of Vectors

In EMGU you just needed one more loop:

The following code will find the contours and then grab the individual points.

NOTE: Do not use 'CV_CHAIN_APPROX_SIMPLE'...you will not get all of the points. This is useful if you're using the CvInvoke.cvDrawContours() call.

Make sure to use 'CV_CHAIN_APPROX_NONE'...at least that's what my experience tells me.

    Image<Gray, byte> grayImage = new Image<Gray, byte>(colorImage);
    Image<Bgr, byte> color = new Image<Bgr, byte>(colorImage);

    Image<Bgr, byte> whiteconverter = new Image<Bgr, byte>(blankImage);

    grayImage = grayImage.ThresholdBinary(new Gray(0), new Gray(255));

    grayImage._Not();

    using (MemStorage storage = new MemStorage())
    {
        using (var p2 = new Pen(Color.Yellow, 2))
        {
            var grp = Graphics.FromImage(blankImage);

            for (Contour<Point> contours = grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, 
                Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
            {

                foreach (var ctr in contours)
                {
                    grp.DrawEllipse(p2, ctr.X, ctr.Y, 4, 4);
                }

                pictureBox3.Image = blankImage;
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM