繁体   English   中英

如何在 EmguCV 中使用 C# 中的迭代器?

[英]How to use iterator in C# with EmguCV?

在 OpenCV 中,我使用std::vector<std::vector<cv::Point>>::const_iterator就像这里的代码:

std::vector<std::vector<cv::Point>> contours;
cv::findContours(contour,contours,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);    
std::vector<std::vector<cv::Point>>::const_iterator itContours = contours.begin();

while(itContours != contours.end())
{
    if(Condition1)
        itContours = contours.erase(itContours);
    else if(Condition2)
    itContours = contours.erase(itContours);
    else if(Condition3)
        itContours = contours.erase(itContours);
    else
        ++itContours;
}

但是现在我开始使用 EmguCV 但我找不到如何像上面的代码那样做。 我该怎么做?

查看 EMGU.Examples 文件夹中的形状检测示例。 它向您展示了如何处理轮廓。 我复制了下面的相关代码供您参考,但最好看一下示例。

        #region Find triangles and rectangles
        List<Triangle2DF> triangleList = new List<Triangle2DF>();
        List<MCvBox2D> boxList = new List<MCvBox2D>(); //a box is a rotated rectangle

        using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
           for (Contour<Point> contours = cannyEdges.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.05, storage);

              if (currentContour.Area > 250) //only consider contours with area greater than 250
              {
                 if (currentContour.Total == 3) //The contour has 3 vertices, it is a triangle
                 {
                    Point[] pts = currentContour.ToArray();
                    triangleList.Add(new Triangle2DF(
                       pts[0],
                       pts[1],
                       pts[2]
                       ));
                 }
                 else if (currentContour.Total == 4) //The contour has 4 vertices.
                 {
                    #region determine if all the angles in the contour are within [80, 100] degree
                    bool isRectangle = true;
                    Point[] pts = currentContour.ToArray();
                    LineSegment2D[] edges = PointCollection.PolyLine(pts, true);

                    for (int i = 0; i < edges.Length; i++)
                    {
                       double angle = Math.Abs(
                          edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
                       if (angle < 80 || angle > 100)
                       {
                          isRectangle = false;
                          break;
                       }
                    }
                    #endregion

                    if (isRectangle) boxList.Add(currentContour.GetMinAreaRect());
                 }
              }
           }
        #endregion

如果您需要任何额外的帮助,如果出现任何错误,请告诉我,

干杯,

克里斯

遍历EmguCV中Mat的每个像素

我认为这差不多了!

我希望你能解决问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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