简体   繁体   English

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

[英]Create Mat from vector<point2f>

I am extremely new to computer vision and the opencv library.我对计算机视觉和 opencv 库非常陌生。

I've done some googling around to try to find how to make a new image from a vector of Point2fs and haven't found any examples that work.我已经做了一些谷歌搜索,试图找到如何从 Point2fs 的矢量制作新图像,但没有找到任何有效的例子。 I've seen vector<Point> to Mat but when I use those examples I always get errors.我已经看到vector<Point>Mat但当我使用这些示例时,我总是会出错。

I'm working from this example and any help would be appreciated.我正在使用这个示例,如有任何帮助,我们将不胜感激。

Code: I pass in occludedSquare.代码:我传入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);

Should be as simple as 应该如此简单

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

The probably confusion is that a cv::Mat is an image width*height*number of channels but it also a mathematical matrix , rows*columns*other dimension . 可能的混淆是cv :: Mat是图像width*height*number通道width*height*number ,但它也是数学矩阵, rows*columns*other dimension

If you make a Mat from a vector of 'n' 2D points it will create a 2 column by 'n' rows matrix. 如果你从'n'个2D点的矢量制作一个Mat,它将按'n'行矩阵创建一个2列。 You are passing this to a function which expects an image. 您将此传递给期望图像的函数。

If you just have a scattered set of 2D points and want to display them as an image you need to make an empty cv::Mat of large enough size (whatever your maximum x,y point is) and then draw the dots using the drawing functions http://docs.opencv.org/doc/tutorials/core/basic_geometric_drawing/basic_geometric_drawing.html 如果您只有一组散在的2D点,并希望将它们显示为图像,则需要制作一个足够大的空cv :: Mat(无论您的最大x,y点是什么),然后使用绘图绘制点函数http://docs.opencv.org/doc/tutorials/core/basic_geometric_drawing/basic_geometric_drawing.html

If you just want to set the pixel values at those point coordinates search SO for opencv setting pixel values, there are lots of answers 如果您只想在这些点坐标处设置像素值,请搜索SO以获取opencv设置像素值,这里有很多答案

Martin's answer is right but IMO it depends on how image cv::Mat is used further along the line.马丁的回答是正确的,但在我看来,这取决于如何进一步使用image cv::Mat I had some issues and Haofeng's comment helped me fix them.我遇到了一些问题,浩峰的评论帮助我解决了这些问题。 Here is my attempt to explain it in detail:这是我尝试详细解释的尝试:

Let's say the code looks like this:假设代码如下所示:

  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;

This will print:这将打印:

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

So, at first glance, this looks perfectly correct and as expected: for the five 2D points in the given vector , we got a cv::Mat with 5 rows and 2 columns, right?因此,乍一看,这看起来完全正确并且符合预期:对于给定vector中的五个 2D 点,我们得到了一个具有 5 行和 2 列的cv::Mat ,对吗? However, that's not the case here!但是,这里不是这种情况!

If further properties are inspected:如果检查更多属性:

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

it can be seen that the above cv::Mat has 5 rows, 1 column , and 2 channels .可以看出上面的cv::Mat有5行1列2通道 Depending on the pipeline, we may not want that.根据管道,我们可能不希望那样。 Most of the time, we want a matrix with 5 rows, 2 columns, and just 1 channel.大多数时候,我们想要一个有 5 行、2 列且只有 1 个通道的矩阵。

To fix this problem, all we need to do is reshape the matrix:要解决这个问题,我们需要做的就是reshape矩阵:

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

In the above code, 1 is for 1 channel .在上面的代码中, 1代表1 channel Check out OpenCV reshape() documentation for further information.查看 OpenCV reshape()文档以获取更多信息。

If this matrix is printed out, it will look the same as the previous one.如果这个矩阵被打印出来,它看起来和上一个一样。 However, that's not the whole picture (metaphorically,) The new matrix has 5 rows, 2 columns , and 1 channel .然而,这还不是全部(比喻)新矩阵有 5 行、 2 列1 个通道

I wish OpenCV had different ways of printing out these two similar yet different matrices (from the OpenCV data structure point of view)!我希望 OpenCV 有不同的方法来打印出这两个相似但不同的矩阵(从 OpenCV 数据结构的角度来看)!

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

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