Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using OpenCV with std::Array

Tags:

c++

c++11

opencv

This question is related to this one std::array<T,N> need to be fetched in another types

I have noticed that OpenCV methods that deal with sequence of points accept either cv::InputArray or const* of the point type. e.g. (cv::Point* const) When you have your points in std::array, you may depend on the const* input of the OpenCV method and call my_array.data() in order to get a pointer of the type. However, you will have to deal with the cv::Point to cv::Point2f and vice-versa problem. Furthermore, it is not really the right way.

I thought that the solution could be found in cv::InputArray where I will find iterator-based solution or at least overloads for C++ standard containers. I read its documentation and I shocked that it takes either std::vector or cv::Mat and some other Gpu data types.

The question is : How to overcome this problem? Did I miss something? in other word: How to use OpenCV with the standard container in the best way?

Example:

std::array<cv::Point,4> my_points,my_points2;
//fill my_points,my_points2
cv::fillConvexPoly(img, my_points.data(), 4, cv::Scalar::all(255));//this works
auto homography_matrix = cv::getPerspectiveTransform(my_points.data(), my_points2.data()); //This will not work
like image 808
Humam Helfawi Avatar asked Apr 13 '26 11:04

Humam Helfawi


1 Answers

There's nothing wrong with OpenCV functions. Simply fillConvexPoly is expecting Point, while getPerspectiveTransform is expecting Point2f. So you should pass the correct data type to those functions.

It has also nothing to do with array instead of vector. A vector<Point> would not be good either, but you need a vector<Point2f>.

You can convert your std::array<cv::Point, 4> to a vector<Point2f> and then pass it to getPerspectiveTransform:

vector<Point2f> pts(my_points.begin(), my_points.end());
auto homography_matrix = cv::getPerspectiveTransform(pts.data(), pts.data()); 
like image 58
Miki Avatar answered Apr 16 '26 01:04

Miki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!