【问题标题】:Find straight lines in contour openCV在轮廓openCV中查找直线
【发布时间】:2012-12-28 15:08:01
【问题描述】:

我正在使用 android openCV,我想检测图像中的三角形、矩形和圆形。所以我这样做如下: Canny => findContours => approxPolyDP 并得到这个图像:


但是,approxPolyDP 的结果包含太多顶点,所以我无法确定它是哪个形状。为了消除顶点,我想检测每个轮廓中的线并找到它们的交点。我怎样才能为单个轮廓做到这一点?

【问题讨论】:

  • 红圈也算圆圈吗?我问这个,因为他们有开放的轮廓,这就是为什么,因此理想情况下不符合圆圈......
  • “红圈”其实是C字母:),我用Core.putText画的,我们不用担心。

标签: android opencv contour


【解决方案1】:

对于圆圈检测,请使用HoughCircles

那么在这里您只是在寻找简化的多边形(三角形和正方形)。您是否尝试过在 approxPolyDP 中调整 epsilon?

这是一个来自 openCV squares.cpp sample code 的示例 sn-p - 了解如何相对于轮廓大小设置近似精度(epsilon,approxPolyDP 的第三个参数)。

C++代码,但是openCV的接口应该是一样的,所以我相信适应你的环境很简单。

 // test each contour
  for( size_t i = 0; i < contours.size(); i++ )
      {
          // approximate contour with accuracy proportional
          // to the contour perimeter
      approxPolyDP(Mat(contours[i]), approx, 
                        arcLength(Mat(contours[i]), true)*0.02, true);

          // square contours should have 4 vertices after approximation
          // relatively large area (to filter out noisy contours)
          // and be convex.
          // Note: absolute value of an area is used because
          // area may be positive or negative - in accordance with the
          // contour orientation
      if( approx.size() == 4 &&
         fabs(contourArea(Mat(approx))) > 1000 &&
         isContourConvex(Mat(approx)) )
          {
          double maxCosine = 0;

          for( int j = 2; j < 5; j++ )
              {
                  // find the maximum cosine of the angle between joint edges
              double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
              maxCosine = MAX(maxCosine, cosine);
              }

              // if cosines of all angles are small
              // (all angles are ~90 degree) then write quandrange
              // vertices to resultant sequence

          if( maxCosine < 0.3 )
              squares.push_back(approx);
          }
      }

【讨论】:

猜你喜欢
  • 2021-01-31
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多