简体   繁体   中英

How to sort contours in EMGUCV/C# from left to right?

How do we sort contours in EMGUCV 4.5 from left to right? There is no sorting function defined in the library.

Has anyone tried sorting contours in the new version of EMGUCV?

Have extracted the bounding rect and sorted using linq. See below

    static void Sort(Mat img, VectorOfVectorOfPoint contours)
    {
        List<Rectangle> rects= new List<Rectangle>();

        for (int i = 0; i < contours.Size; i++)
        {
            var rect = CvInvoke.BoundingRectangle(contours[i]);
            rects.Add(rect);
            CvInvoke.Rectangle(img, rect, new MCvScalar(0, 0, 0), 3);
            CvInvoke.Imshow("img", img);
            CvInvoke.WaitKey();
        }

        var rectList = rects.Distinct().OrderBy(r => r.Left).ThenBy(r 
             => r.Top).ToList();
    }

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