c++ - 如何使用 cv::findcontours 和层次结构查找内孔数

标签 c++ opencv image-processing

我需要在下图中找到内孔的数量。即我的最终要求是使用 opencv 中的轮廓层次单独检测和找到圆形黑洞的区域。无需使用任何其他算法。

基于此链接 Using hierarchy in findContours () in OpenCV?我试过了,但没用。

有没有其他方法可以找到图像中的孔数?

这里我附上了示例图像和代码。任何人都可以提出使用层次结构单独找到内部黑洞的想法。我在轮廓层次结构方面没有太多经验。提前致谢。 我使用了 opencv c++ 库。

<code>enter image description here</code>

cv::Mat InputImage = imread("New Image.jpg");
int Err;

if(InputImage.empty() == 1)
{
    InputImage.release();
    cout<<"Error:Input Image Not Loaded"<<endl;
    return 1;
}
cv::Mat greenTargetImage;

std::vector<cv::Mat> Planes;
cv::split(InputImage,Planes);

greenTargetImage = Planes[1];
cv::Mat thresholdImage = cv::Mat (greenTargetImage.size(),greenTargetImage.type());
cv::threshold(greenTargetImage,thresholdImage,128,255,THRESH_OTSU);
imwrite("thresholdImage.jpg",thresholdImage);

std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(thresholdImage,contours,hierarchy,cv::RETR_CCOMP,cv::CHAIN_APPROX_SIMPLE,cv::Point(-1,-1));
cout<<contours.size()<<endl;
cout<<hierarchy.size()<<endl;
int count = 0;
if (!contours.empty() && !hierarchy.empty())
{
    for (int i = 0;i<contours.size();i++ )
    {
        if ( hierarchy[i][3] != -1)
        {
            cv::drawContours(InputImage,contours,i,CV_RGB(0,255,0),3);
            count = count+1;
        }
    }   
}
cout<<count<<endl; //No of inner holes in same level  
imwrite("ContourImage.jpg",InputImage);

应用此代码后,我得到的输出计数值为 11。但我的要求是计数值应为 10,而且我需要仅绘制内部黑洞,而不是外部轮廓的所有边界。抱歉我的英语。

最佳答案

尝试使用此代码对使用层次结构的我来说效果很好。

思路很简单,只考虑没有子节点的轮廓。

也就是

hierarchy[i][2]= -1

代码:-

  Mat tmp,thr;
  Mat src=imread("img.jpg",1);
  cvtColor(src,tmp,CV_BGR2GRAY);
  threshold(tmp,thr,200,255,THRESH_BINARY_INV);
  namedWindow("thr",0);
  imshow("thr",thr);

   vector< vector <Point> > contours; // Vector for storing contour
   vector< Vec4i > hierarchy;
   Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
   int count=0;

   findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
   for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through each contour.
      {   
        Rect r= boundingRect(contours[i]);
        if(hierarchy[i][2]<0){
            rectangle(src,Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,0,255),3,8,0);
            count++;
        }
      }
  cout<<"Numeber of contour = "<<count<<endl;
  imshow("src",src);
  imshow("contour",dst);
  waitKey();

结果:- enter image description here

关于c++ - 如何使用 cv::findcontours 和层次结构查找内孔数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492152/

相关文章:

c# - 并行应用滤波器组

c++ - 将 Lua 绑定(bind)胶水代​​码与我的游戏引擎的其余部分隔离开来

python - 平滑二值图像的边缘

c++ - QGLWidget + QGraphicsScene + QGraphicsView问题

python - 使用欧氏距离显示图像

python - 如何使用 Python 创建可操纵的边缘检测过滤器或丢弃不符合所需角度的边缘

python - 使用 h264 编码流式传输 openCV 帧

python - 识别航拍图像上的人行横道

C++ 可以使用 move 语义学将数据从一个 vector move 到另一个 vector

c++ - 在 C 中,将函数指针赋值给适当类型的变量给出 "cannot convert ... in assignment"