Friday, June 23, 2017

OpenCV std::vector for cv::Mat arguments

Leave a Comment

Executing this:

std::vector<cv::Point2f> pts; // contains 4 elements cv::Mat ptsMat = ((cv::InputArray)pts).getMat(); 

On one machine I get a 4-by-1 cv::Mat with 2 channels. Each element represents a 2D point. On another machine, I get a 2090-by-1 cv::Mat with 2 channels with weird data. This is wrong and this is a problem, since the vector contained only 4 items.

On both machines using OpenCV 3.1 built from source using CMake on Windows 10.

EDIT

I started experiencing similar problem on a different machine. In Visual Studio in Debug mode the following snippet works fine:

std::vector<cv::Point2f> points = { cv::Point2f(2, 1), cv::Point2f(2, 1), cv::Point2f(1, 3) }; cv::InputArray arr = points; cv::Mat ma = arr.getMat(); std::cout << ma.ptr<float>(0)[1]; 

But when I compile it in Release, it crashes on the last line with Access violation reading location...

Further investigation revealed that the key flag that makes a difference is:

Runtime library: Multithreaded DLL (/MD) --------> crash

Runtime library: Multithreaded DLL Debug (/MDd) --------> works

EDIT 2

Based on the comment of Mateen Ulhaq, I checked the

std::vector<cv::Point2f> points; cv::Mat image(points); 

and this works fine. The problem is that some of OpenCV larger functions (e.g. undistortPoints) internally use the getMat() conversion. And if I run that on release, the app crashes.

EDIT 3

Just checked, same problem occurs on OpenCV 3.2

1 Answers

Answers 1

This is not exactly an answer I was looking for but here goes.

It seems every time this has occurred, it was immediately after a Windows Update.

I completely deleted and re-built the following libraries: VTK, OpenCV, Eigen and finally, my project.

This fixed it for me.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment