从vector <point2f> </point2f>创建Mat

时间:2014-05-10 19:08:19

标签: c++ opencv

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

我已经做了一些谷歌搜索,试图找到如何从Point2fs的矢量创建一个新的图像,并找不到任何有效的例子。我已经看过vector<Point> to Mat但是当我使用这些例子时,我总是会遇到错误。

我在this示例工作,我们将不胜感激。

代码:我传入了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);

1 个答案:

答案 0 :(得分:10)

应该像

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

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

如果你用'&n;&#39; n&#39; 2D点它将通过&#39; n&#39;创建2列。行矩阵。您将此传递给需要图像的函数。

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

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