繁体   English   中英

从矢量创建垫子<point2f></point2f>

[英]Create Mat from vector<point2f>

我对计算机视觉和 opencv 库非常陌生。

我已经做了一些谷歌搜索,试图找到如何从 Point2fs 的矢量制作新图像,但没有找到任何有效的例子。 我已经看到vector<Point>Mat但当我使用这些示例时,我总是会出错。

我正在使用这个示例,如有任何帮助,我们将不胜感激。

代码:我传入occludedSquare。

   resize(occludedSquare, occludedSquare, Size(0, 0), 0.5, 0.5);

   Mat occludedSquare8u;
   cvtColor(occludedSquare, occludedSquare8u, CV_BGR2GRAY);

   //convert to a binary image. pixel values greater than 200 turn to white. otherwize black
   Mat thresh;
   threshold(occludedSquare8u, thresh, 170.0, 255.0, THRESH_BINARY);



   GaussianBlur(thresh, thresh, Size(7, 7), 2.0, 2.0);

   //Do edge detection
   Mat edges;
   Canny(thresh, edges, 45.0, 160.0, 3);

   //Do straight line detection
   vector<Vec2f> lines;
   HoughLines( edges, lines, 1.5, CV_PI/180, 50, 0, 0 );

   //imshow("thresholded", edges);


   cout << "Detected " << lines.size() << " lines." << endl;

   // compute the intersection from the lines detected...
   vector<Point2f> intersections;
   for( size_t i = 0; i < lines.size(); i++ )
   {
       for(size_t j = 0; j < lines.size(); j++)
       {
           Vec2f line1 = lines[i];
           Vec2f line2 = lines[j];
           if(acceptLinePair(line1, line2, CV_PI / 32))
           {
               Point2f intersection = computeIntersect(line1, line2);
               intersections.push_back(intersection);
           }
       }

   }

   if(intersections.size() > 0)
   {
       vector<Point2f>::iterator i;
       for(i = intersections.begin(); i != intersections.end(); ++i)
       {
           cout << "Intersection is " << i->x << ", " << i->y << endl;
           circle(occludedSquare8u, *i, 1, Scalar(0, 255, 0), 3);
       }
   }

//Make new matrix bounded by the intersections
...
imshow("localized", localized);

应该如此简单

std::vector<cv::Point2f> points;
cv::Mat image(points);
//or
cv::Mat image = cv::Mat(points) 

可能的混淆是cv :: Mat是图像width*height*number通道width*height*number ,但它也是数学矩阵, rows*columns*other dimension

如果你从'n'个2D点的矢量制作一个Mat,它将按'n'行矩阵创建一个2列。 您将此传递给期望图像的函数。

如果您只有一组散在的2D点,并希望将它们显示为图像,则需要制作一个足够大的空cv :: Mat(无论您的最大x,y点是什么),然后使用绘图绘制点函数http://docs.opencv.org/doc/tutorials/core/basic_geometric_drawing/basic_geometric_drawing.html

如果您只想在这些点坐标处设置像素值,请搜索SO以获取opencv设置像素值,这里有很多答案

马丁的回答是正确的,但在我看来,这取决于如何进一步使用image cv::Mat 我遇到了一些问题,浩峰的评论帮助我解决了这些问题。 这是我尝试详细解释的尝试:

假设代码如下所示:

  std::vector<cv::Point2f> points = {cv::Point2f(1.0, 2.0), cv::Point2f(3.0, 4.0), cv::Point2f(5.0, 6.0), cv::Point2f(7.0, 8.0), cv::Point2f(9.0, 10.0)};
  cv::Mat image(points);  // or cv::Mat image = cv::Mat(points) 
  std::cout << image << std::endl;

这将打印:

[1, 2;
 3, 4;
 5, 6;
 7, 8;
 9, 10]

因此,乍一看,这看起来完全正确并且符合预期:对于给定vector中的五个 2D 点,我们得到了一个具有 5 行和 2 列的cv::Mat ,对吗? 但是,这里不是这种情况!

如果检查更多属性:

  std::cout << image.rows << std::endl;  // 5
  std::cout << image.cols << std::endl;  // 1
  std::cout << image.channels() << std::endl;  // 2

可以看出上面的cv::Mat有5行1列2通道 根据管道,我们可能不希望那样。 大多数时候,我们想要一个有 5 行、2 列且只有 1 个通道的矩阵。

要解决这个问题,我们需要做的就是reshape矩阵:

  cv::Mat image(points).reshape(1);

在上面的代码中, 1代表1 channel 查看 OpenCV reshape()文档以获取更多信息。

如果这个矩阵被打印出来,它看起来和上一个一样。 然而,这还不是全部(比喻)新矩阵有 5 行、 2 列1 个通道

我希望 OpenCV 有不同的方法来打印出这两个相似但不同的矩阵(从 OpenCV 数据结构的角度来看)!

暂无
暂无

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

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