繁体   English   中英

使用opencv检测盒子

[英]detecting a box using opencv

我正在尝试使用OpenCV检测以下包装盒尺寸。 如果您觉得有用,我也有深度图片。

我尝试过的是使用霍夫变换并获取直线的相交点...

我的问题是,是否可以获取轮廓,然后对所选轮廓使用霍夫变换? 代码看起来如何?

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

cv::Point2f center( 0, 0 );

cv::Point2f computeIntersect( cv::Vec4i a,
  cv::Vec4i b )
{
  int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
  float denom;

  if ( float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)) )
  {
    cv::Point2f pt;
    pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
    pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
    return pt;
  }
  else
    return cv::Point2f( -1, -1 );
}

void sortCorners( std::vector<cv::Point2f>& corners,
  cv::Point2f center )
{
  std::vector<cv::Point2f> top, bot;

  for ( int i = 0; i < corners.size(); i++ )
  {
    if ( corners[i].y < center.y )
      top.push_back( corners[i] );
    else
      bot.push_back( corners[i] );
  }
  corners.clear();

  if ( top.size() == 2 && bot.size() == 2 ) {
    cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
    cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
    cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
    cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];


    corners.push_back( tl );
    corners.push_back( tr );
    corners.push_back( br );
    corners.push_back( bl );
  }
}

int main( int argc, char** argv )
{


  Mat src, src_copy, edges, dst;
  src = imread( "freezeFrame__1508152029892.png", 0 );

  src_copy = src.clone();

  GaussianBlur( src, edges, Size( 5, 5 ), 1.5, 1.5 );

  erode( edges, edges, Mat() );// these lines may need to be optimized 
  dilate( edges, edges, Mat() );
  dilate( edges, edges, Mat() );
  erode( edges, edges, Mat() );

  Canny( edges, dst, 50, 300, 3 ); // canny parameters may need to be optimized 
  imshow( "canny", dst );

  vector<Point> selected_points;
  vector<vector<Point> > contours;
  findContours( dst, contours, RETR_LIST, CHAIN_APPROX_SIMPLE );


  for ( size_t i = 0; i < contours.size(); i++ )
  {
    Rect minRect = boundingRect( contours[i] );

    if ( (minRect.width > src.cols / 2) | (minRect.height > src.rows / 2) ) // this line also need to be optimized 
    {
      selected_points.insert( selected_points.end(), contours[i].begin(), contours[i].end() );

        drawContours( src, contours, i, Scalar( 0, 0, 255 ), 3 );

    }
  }

 imshow( "Selected contours", src );

 cv::waitKey( 0 );

  vector<Vec4i> lines;
  HoughLinesP( selected_points, lines, 1, CV_PI / 180, 50, 50, 10 );
  for ( size_t i = 0; i < lines.size(); i++ )
  {

    cv::Vec4i v = lines[i];
    lines[i][0] = 0;
    lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];
    lines[i][2] = src.cols;
    lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols - v[2]) + v[3];

    v = lines[i];
    cv::line( src, cv::Point( v[0], v[1] ), cv::Point( v[2], v[3] ), CV_RGB( 0, 255, 0 ) );

  }



  std::vector<cv::Point2f> corners;
  for ( int i = 0; i < lines.size(); i++ )
  {
    for ( int j = i + 1; j < lines.size(); j++ )
    {
      cv::Point2f pt = computeIntersect( lines[i], lines[j] );
      if ( pt.x >= 0 && pt.y >= 0 )
        corners.push_back( pt );
    }
  }

  for ( int i = 0; i < corners.size(); i++ )
  {
    // Draw corner points
    cv::circle( src, corners[i], 3, CV_RGB( 255, 0, 0 ), 2 );
  }

  imshow( "line src", src );
  imshow("line dest", edges );
  cv::waitKey( 0 );


  return 0;
}

在此处输入图片说明

我实际上想计算那个盒子的体积...

这是一个非常广泛的问题...我只给您一些有关如何进行的建议,而不是代码。 我想您想确定一张休闲桌子上任何盒子的体积,而不仅仅是这个非常具体的图像中的盒子。

使用(应该是)密集的深度图像,您可以重建场景的3D点云。 您可以使用pcl库和RANSAC等拟合方法来拟合点云中的平面。

然后,您应该了解桌子是哪个表面,然后找到盒子表面并计算封闭的体积。

另一种方法是使用cv :: goodFeaturesToTrack()在图像中找到拐角,再次使用深度图像计算相应的3D点,以仅计算检测到的拐角的3D位置,然后找到连接拐角的线并尝试找出哪些线与其他线垂直。

暂无
暂无

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

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