在C++/CLI中如何利用等高线逼近OpenCV寻找随机形状物体的长度和宽度

在C++/CLI中如何利用等高线逼近OpenCV寻找随机形状物体的长度和宽度,opencv,c++-cli,contour,Opencv,C++ Cli,Contour,我使用C++/CLI应用程序中的find contours OpenCV函数选择不同的随机形状对象 我想找到图像中每个轮廓的最大长度。以及轮廓的宽度,每次的差异为40到50像素。你能帮帮我吗 findContours(K, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); for(idx=0; idx<contours.size(); idx++) { double cArea = contourArea(con

我使用C++/CLI应用程序中的find contours OpenCV函数选择不同的随机形状对象

我想找到图像中每个轮廓的最大长度。以及轮廓的宽度,每次的差异为40到50像素。你能帮帮我吗

findContours(K, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); for(idx=0; idx<contours.size(); idx++)
{
    double cArea = contourArea(contours[idx]);

    // get only contours with area above specific threshold
    if (cArea > min_area && cArea < 40000)// && !k )
    {
        if (cArea > largest_area) {
            largest_area = cArea; 

            largest_contour_index = idx;

           // Find the bounding rectangle for biggest contour
            bounding_rect = boundingRect(contours[idx]);
        }

        for (unsigned int j = 0; j<contours[idx].size(); j++)      
        {
            circle(dst, Point(contours[idx][j].x, contours[idx][j].y), 3, Scalar(255, 255,0), FILLED, LINE_AA);

        }
   }

}

假设找到了轮廓,。。。。。是您正在使用的,其结果是

   std::vector<std::vector<cv::Point>> contours;
将旋转的矩形拟合到每个轮廓

 //Assuming right version of C++
 for(const auto& it: contours){
 Rect boundingRect = boundingRect(it);
 // boundRect.height, boundingRect.width will give you what you 
  //need
  }
 //Assuming C++ 11
 for(const auto& it: contours){
 RotatedRect rotRect = minAreaRect(it);
 // rotRect.size.height, rotRect.size.width will give you what you 
  //need
  }
 //Assuming C++ 11
 for(const auto& it: contours){
 Point2f center;
 float radius;
 minEnclosingCircle(it, center, radius);
 }
 //Assuming C++ 11
 for(const auto& it: contours){
 // note need atleast 5 points to fit an ellipse
 if(it.size()>5){
 RotatedRect boundingEllipse = minBoundingEllipse(it);
 }
 // boundingEllipse.size.height, boundingEllipse.size.width will 
 //give you what you need - in this case the major and minor axis
 }
为每个轮廓拟合一个封闭圆

 //Assuming right version of C++
 for(const auto& it: contours){
 Rect boundingRect = boundingRect(it);
 // boundRect.height, boundingRect.width will give you what you 
  //need
  }
 //Assuming C++ 11
 for(const auto& it: contours){
 RotatedRect rotRect = minAreaRect(it);
 // rotRect.size.height, rotRect.size.width will give you what you 
  //need
  }
 //Assuming C++ 11
 for(const auto& it: contours){
 Point2f center;
 float radius;
 minEnclosingCircle(it, center, radius);
 }
 //Assuming C++ 11
 for(const auto& it: contours){
 // note need atleast 5 points to fit an ellipse
 if(it.size()>5){
 RotatedRect boundingEllipse = minBoundingEllipse(it);
 }
 // boundingEllipse.size.height, boundingEllipse.size.width will 
 //give you what you need - in this case the major and minor axis
 }
将椭圆拟合到每个轮廓

 //Assuming right version of C++
 for(const auto& it: contours){
 Rect boundingRect = boundingRect(it);
 // boundRect.height, boundingRect.width will give you what you 
  //need
  }
 //Assuming C++ 11
 for(const auto& it: contours){
 RotatedRect rotRect = minAreaRect(it);
 // rotRect.size.height, rotRect.size.width will give you what you 
  //need
  }
 //Assuming C++ 11
 for(const auto& it: contours){
 Point2f center;
 float radius;
 minEnclosingCircle(it, center, radius);
 }
 //Assuming C++ 11
 for(const auto& it: contours){
 // note need atleast 5 points to fit an ellipse
 if(it.size()>5){
 RotatedRect boundingEllipse = minBoundingEllipse(it);
 }
 // boundingEllipse.size.height, boundingEllipse.size.width will 
 //give you what you need - in this case the major and minor axis
 }
4通常是最准确的。2和4还可以根据需要提供更多信息,如中心、角度


我们用这种方法发现的宽度和长度只根据轮廓面积计算,或者也包含不属于轮廓部分的日食面积。轮廓形状是完全随机的,在不同角度上宽度不同。那么它将为宽度和长度选择哪个区域。最大的还是最大的?我想得到长度和宽度的单位,比如毫米或英寸。我使用了你和我分享的以下方法:对于const auto&it:contours{//注意,如果椭圆的大小大于5,则至少需要5个点来拟合椭圆{//RotatedRect BoundingEllipseit=minBoundingEllipseit;RotatedRect BoundingEllipseit=fitEllipseit;浮动裂纹高度=boundingEllipse.size.height;浮动裂纹宽度=boundingEllipse.size.width;//浮动裂纹面积=boundingEllipse.size.area;ellipsetempimage,boundingEllipse,0,0,255,2}它给了我一个错误:float crack_area=boundingEllipse.size.area;@DavidHorn我很确定属性大小没有属性区域。如果你需要椭圆的面积,你可以自己计算pi*boundingEllipse.size.width*boundingEllipse.size.height,或者你可以直接用轮廓区域听起来不错。非常感谢。你能告诉我如何在一个轮廓内找到子轮廓或区域吗。我想将每个轮廓划分为不同的分包商或区域,然后计算每个子轮廓或区域的宽度。然后我想根据每个区域的宽度进行分类。再次感谢。