Iterate through Contours - OpenCV ( C# )

本文关键字:OpenCV through Contours Iterate | 更新日期: 2023-09-27 18:02:16

我试图在OpenCV (c#)中使用ContourArea()方法获得轮廓面积。它总是在每次迭代中为Area返回相同的值。
以下是我的代码的相关部分。

public void getcontour()
    {
        IplImage binary_image= Cv.LoadImage("binary.png", LoadMode.GrayScale);
        CvMemStorage memory = new CvMemStorage();
        OpenCvSharp.CvSeq<CvPoint> contours;
        Cv.FindContours(binary_image, memory, out contours);
        double area=0;
        for (int k=0; k<=contours.Total;k++)
        {
            area= contours.ContourArea();
            /*need to access properties such as width & height of current contour in here*/
        }
        //draw contours
        IplImage save = Cv.CreateImage(binary_image.Size, BitDepth.U8, 1);
        Cv.DrawContours(save, contours, col1, col2, 1, 1);
        Cv.SaveImage("cont.png", save);
    }

Iterate through Contours - OpenCV ( C# )

找到了用OpenCv (C#)迭代contours的解决方案
代码如下:

public void getcontour()
{
    IplImage binary_image= Cv.LoadImage("binary.png", LoadMode.GrayScale);
    CvMemStorage memory = new CvMemStorage();
    OpenCvSharp.CvSeq<CvPoint> contours;
    Cv.FindContours(binary_image, memory, out contours);
    double area=0;
    while (contours != null) 
    {
        area = contours.ContourArea();
        /* logic */
        contours = contours.HNext;
    }
 }