过滤不需要的轮廓 - 使用 C++ 的 opencv

filtering unwanted contours - opencv with C++

我正在处理我想要检测一些对象的视频。

首先,我删除背景,然后将其设为灰色并使用 cv::threshold 制作二值图像,然后 cv::findcontours.

我想删除太长和太短的轮廓。 我在 "Opencv 2 cookbook" 中找到了这个原因的代码。但是它在我视频的第 21 帧有一个 运行 时间错误。

// Eliminate too short or too long contours
    size_t cmin{ 15 }; // minimum contour length
    size_t cmax{ 120 }; // maximum contour length


    std::vector<std::vector<cv::Point> >::
        const_iterator itc = contours.begin();
    while (itc != contours.end()) {
        if (itc->size() < cmin || itc->size() > cmax)
            itc = contours.erase(itc);
        else
            ++itc;
    }

我搜索了一下,发现了另一个代码,在同一个框架上也有同样的错误:

double min_area = 500; // area threshold
    double max_area = 1500; // area threshold
    for (int i = 0; i < contours.size(); i++) // iterate through each contour.
    {
        double area = contourArea(contours[i], false);  //  Find the area of contour
        if (area < min_area || area > max_area)
            contours.erase(contours.begin() + i);
    }

然后我写了一个代码来创建一个新的矢量,然后把接受的轮廓复制到里面。但它在同一帧上也有相同的错误:

std::vector<std::vector<cv::Point> > goodcontours;
    size_t cmin{ 15 };
    size_t cmax{ 120 };
    double contourlength = 1;
    size_t contoursize = contours.size();
    for (int i = 0; i != contoursize;i++) {
        contourlength = cv::arcLength(contours[i], true);
        if (contourlength > cmin && contourlength < cmax)
            goodcontours.push_back(contours.at(i));
    }

我尝试了其他视频,但所有视频在特定帧上都有相同的问题。

在错误发生之前,我仍然可以看到不需要的轮廓,这意味着它们并没有真正消除。

错误只是说:

Unhandled exception at 0x75017FB2 (ucrtbase.dll)

你对过滤轮廓有什么建议吗?

one of my video frames contours

这是我找到的答案:

cv::findContours(image,contours,CV_RETR_LIST,mode,cv::Point(offset_x,offset_y));
    
    for(int i=0;i<contours.size();i++)
    {
      AreaContour=cv::contourArea(contours[i]);
      if(AreaContour<MaxAreaContour && AreaContour>MinAreaContour)
        drawContours(Result,contours,i,cv::Scalar(DrawColor),Thickness,LineType,cv::noArray(),2147483647,cv::Point(DrawOffset_x,DrawOffset_y));
    
    }

这是我的 Find_Filter_Draw 等高线功能,分享它可能对某人有帮助

bool Find_Filter_Draw_Contours(){
   // Find Contours on our working image which is ... currentImage.
    cv::Mat hierarchy = cv::Mat();
    std::vector<std::vector<cv::Point> > contours;
    cv::Mat contourOutput = currentImage.clone();

    cv::findContours(contourOutput, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE);
    hierarchy.release();

    // filter them based on Area.  good to remove small dots
    double minArea = 50; 
    double maxArea = 900000;

   std::vector<std::vector<cv::Point> > GoodContours;

  for (size_t idx = 0; idx < contours.size(); idx++) {
        double area = cv::contourArea(contours[idx]);
       // http://www.cplusplus.com/reference/limits/numeric_limits/
        if (area >= (minArea == -1 ? std::numeric_limits<double>::min() : minArea) && area <= (maxArea == -1 ? std::numeric_limits<double>::max() : maxArea)) {
              GoodContours.push_back(contours.at(idx));
        }
    }

    //Draw the contours
    cv::Mat contourImage(currentImage.size(), CV_8UC3, cv::Scalar(0,0,0));
    cv::Scalar colors[3];
    colors[0] = cv::Scalar(255, 0, 0);
    colors[1] = cv::Scalar(0, 255, 0);
    colors[2] = cv::Scalar(0, 0, 255);


      for (size_t idx = 0; idx < GoodContours.size(); idx++) {
          cv::drawContours(contourImage, GoodContours, idx, colors[idx % 3]);
      }

      cv::imshow("Input Image", currentImage);
      cv::imshow("Contours", contourImage);

}