Home > OS >  Get vector<Point2f> from cv::Mat
Get vector<Point2f> from cv::Mat

Time:11-10

I am getting an input of vector of 2d points, make transformation on them and then output transformed vector of 2d points. My code needs to run fast so I want to optimize memory access time, is there a way to cast rotated_points to vector without copying the data?

    std::vector<cv::Point2f> points =
        std::vector<cv::Point2f>({{1, 0}, {0, 1}, {-1, 0}});
    auto points_mat= cv::Mat(points);
    auto rotation_matrix = cv::Matx22f({1, 2, 3, 4});
    cv::Mat rotated_points;
    cv::transform(points_mat, rotated_points, rotation_matrix);
    std::vector<cv::Point2f> res_vec;
    res_vec.assign(rotated_points.begin<cv::Point2f>(), rotated_points.end<cv::Point2f>());

I tried to execute the code without copying the data

CodePudding user response:

std::vector<cv::Point2f> res_vec(points.size());
cv::Mat rotated_points(res_vec);
cv::transform(points_mat, rotated_points, rotation_matrix);

Create vector and pass it as argument to cv::Mat instance with copyData flag set to false (by default it is false cv::Mat ctor). cv::Mat will just refer to vector data as underlying data - cv::Mat will share passed vector's data. All results generated by cv::transform will be stored inside vector by cv::Mat interface.

  • Related