4

I have a cv::Mat with N rows and 2 columns. What is the fastest way to turn that into a std::vector of cv:Point2d?

Currently I'm doing a for loop with manual assignment:

std::vector<cv::Point2d> vector;
cv::Mat centroids;
for (int i=0; i<N; i++) {
   vector.push_back(cv::Point2d(centroids.at<double>(i, 0), centroids.at<double>(i, 1)));
}

I'm working with opencv 3.2

Adam
  • 488
  • 4
  • 17
  • Why do you need a Point2d list exactly? How do you get that Mat? – Florian Castellane Jan 09 '17 at 15:06
  • 1
    Can you try this: http://stackoverflow.com/questions/26681713/convert-mat-to-array-vector-in-opencv if so then this is a dupe. Basically if the `Mat` `isContinous` then because the memory layout is contiguous then you can just assign directly I think – EdChum Jan 09 '17 at 15:08
  • I just corrected, it's a std::vector. I use it for further processing in the application (I'm using a QList, but that doesn't matter to the problem. sorry for the confusion). The Mat is comming from 'cv::connectedComponentsWithStats' – Adam Jan 09 '17 at 15:08
  • EdChum: that is'nt really shorter, is it? I'm coming from matlab, where a matrix and a vector of points is the same. – Adam Jan 09 '17 at 15:10
  • 1
    Unfortunately, there's not really a shorter way. The STL vector and the OpenCV Mat are not the same. – beaker Jan 09 '17 at 15:57

0 Answers0