3

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
4
  • Can you provide a specific example? The short answer will be: either use Mat or std::vector. For any other container you need to convert. Commented Dec 23, 2015 at 13:34
  • In your previous question the problem was nothing to do with std::array (not lowercase), it was that some functions want cv::Point<float>* and some want cv::Point<double>* - and the obvious question is why? Commented Dec 23, 2015 at 13:41
  • In your previous question the problem was nothing to do with std::array (note lowercase), it was that some functions wanted point<float>* and some want point<double>* - and the obvious question is why? Commented Dec 23, 2015 at 13:43
  • if it was accept begin and end, the two problems will be solved in the same time,.. because cv::point and cv::point2f have built-in conversion functions Commented Dec 23, 2015 at 13:50

1 Answer 1

3

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()); 
Sign up to request clarification or add additional context in comments.

10 Comments

if OpenCV accept iterators instead of direct vector container, both problem should be solved. shouldn't it?
In general, you can't expect that a library fulfills any your requirements, but it's the other way around. You should commit to the library style. OpenCV is highly optimized and usually expects all it's data to be in a given format exactly for this. If any OpenCV function would iterate over the container and convert/copy the data to another format, it will be far too slow. Bottom line, give OpenCV data as it expects, you can always wrap OpenCV functions to accept iterators
I see, However, I still think that any C++ library should accept iterator by default. At least, to provide a wrapper that accept it.
No, it doesn't make sense in this case. You want iterators to perform implicit data conversion, and that's not what iterators should be used for. If a function accepts foo, you should give it foo, not bar. It's up to you to convert bar to foo. And your last comment is true the other way around: any C++ library should provide iterator-like access to its data structures. OpenCV has support for iterators in order to use Mat with standard algorithms using MatIterator.
What about the part related to its functions? should not it accept iterators too? it provides iterator for its container(Mat) but does not accept iterators of std containers?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.