来自std :: vector <std :: vector <cv :: point>&gt;到cv :: Mat </std :: vector <cv :: point>

时间:2013-07-23 09:04:02

标签: c++ opencv

我希望将每个点矢量都放到一个矩阵中,如:

std::vector<std::vector<cv::Point>> vec;
......................
for (int i ; i < vec.size();i++){
   imshow("stuff", cv::Mat(vec[i]); /// this crashes !!!
}

任何想法怎么做? 提前谢谢

2 个答案:

答案 0 :(得分:3)

imshow寻找完整的图片。

通过将轮廓投射到Mat上,您将无法获得图片。 你能做的是:( src是你的照片)

  src = imread( argv[1], 1 );

  /// Convert image to gray and blur it
  cvtColor( src, src_gray, CV_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );

  Mat canny_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  /// Detect edges using canny
  Canny( src_gray, canny_output, thresh, thresh*2, 3 );
  /// Find contours
  findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );



  /// Draw contours

    for( int i = 0; i< contours.size(); i++ )
     {
       Mat test_image = Mat::zeros( canny_output.size(), CV_8UC3 );
       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
       drawContours( test_image, contours, i, color, 2, 8, hierarchy, 0, Point() );
       imshow("test",test_image);
       waitKey();
     }

答案 1 :(得分:0)

你可以这样做:

void draw_contour(cv::Mat &dst_img, const std::vector<cv::Point> &contour, const cv::Scalar &color)
{
  for (auto &point: contour)
  {
    dst_img.at<unsigned char>(point) = color;
  }
}

或者如果您使用等高线:

void draw_contour(cv::Mat &dst_img, const std::vector<cv::Point> &contour, const cv::Scalar &color)
{
  for (unsigned i = 0; i < contour.size(); ++i)
  {
    cv::line(dst_img, contour[i], contour[(i + 1) % contour.size(), color);
  }
}