c++ - 从 vector<point2f> 创建 Mat

标签 c++ opencv

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

我已经进行了一些谷歌搜索,试图找到如何从 Point2fs vector 制作新图像,但没有找到任何有效的示例。我看过 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);

最佳答案

应该像

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

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

如果您从包含“n”个二维点的 vector 创建一个 Mat,它将创建一个 2 列乘“n”行的矩阵。您正在将其传递给需要图像的函数。

如果你只有一组分散的 2D 点并想将它们显示为图像,你需要制作一个足够大的空 cv::Mat(无论你的最大 x,y 点是多少)然后绘制这些点使用绘图功能 http://docs.opencv.org/doc/tutorials/core/basic_geometric_drawing/basic_geometric_drawing.html

如果你只是想在那些点坐标处设置像素值搜索 SO for opencv setting pixel values,有很多答案

关于c++ - 从 vector<point2f> 创建 Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585158/

相关文章:

c++ - STL 的 list::sort() 使用哪种排序算法?

c++ - 结构初始化语法

opencv - kanade Lucas 图像对齐与跟踪有任何关系吗?

c++ - 静态摄像机视频的 OpenCV 噪声去除

c++ - 我如何确保枚举类中定义的每个案例都得到处理,例如使用 static_assert?

c++ - 运行外部 .exe 并从中接收返回值

c++ - 将 ZMQ 上下文传递给线程

opencv - 使用 OpenCV 和 C++ 检测 RGB 颜色区间

python-3.x - cv2 python没有imread成员

c++ - 在 OpenCV 中选择强度最高的像素