简体   繁体   中英

Going back to the first Contour in Emgu after iterating through them

So basically i'm using Emgu.CV to detect contours, and after I get the contours of an image:

Contour<System.Drawing.Point> contours;
using (var stor = new MemStorage())
{
     contours = gray_image.FindContours(
        Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
        Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL,
        stor);
}

I basically iterate through them using:

for (i = 0; 
    (context.Contours != null) && (i < this.config.MaxNumberContours); 
    context.Contours = context.Contours.HNext)

Can I iterate through them again afterwards? The documentation says that it is similar to h_next pointer in OpenCV , what does this means?

First, where did you find that code to iterate through the contours? The normal way, taken from Emgu webpage , is something like this:

for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext)
{
 ...
}

The contours are not indexed, but each Contour contains a pointer HNext that points to the next contour. If HNext is null, there are no more contours.

If you want to go through them again, you can use the HPrev pointer, which points to the previous contour. If HPrev is null, you have reached the first contour.

Another option, if you are running through the contours many times, is to save the contours in a List like this:

var contourList = new List<Contour<Point>>()
for (Contour<Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext)
{
 contourList.Add(contour);
}

Now you can use the contourList to iterate through the contours and you can access them by an index.

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