How to convert mxArray to Point2f ? OpenCV interface

3 views (last 30 days)
In order to use the calcOpticalFlowPyrLK function provided by OpenCV libraries through the MATLAB/OpenCV interface, the third and fourth arguments are required to be 2D arrays - the third populated with 2D keypoint locations (in single floats) to tracked forward by the function and keypoints that were tracked forward's location to be stored in the fourth argument.
From the main enviroment, the array of 2D keypoints locations are singles - How do I convert them from mxArray of singles to a format that the cpp environment will accept - a 2D array of single-precision floating-points?
A function that is the opposite of ocvMxArrayFromPoints2f would be ideal - ocvPoints2fFromMxArray. I have attempted to use ocvStructToKeyPoints, but an error
error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function cv::calcOpticalFlowPyrLK
..
Error in testopticalFlowLKOCV (line 38) opticalFlowLKOCV(original, distorted, validPtsOriginal)
was thrown. The cpp used was
//convert structures to keypoints
ocvStructToKeyPoints(prhs[2], keypoints);
//Calculate forward optical flow for prev_location
if (keypoints.size() > 0)
{
calcOpticalFlowPyrLK(*img_prev, *img_current, keypoints, keypoints1, status, err);
}
else
{
mexErrMsgTxt("Keypoints are empty");
}
I assumed the cause of the error was due to https://github.com/uscauv-legacy/old-uscauv-ros-pkg/issues/47,
i.e. the third argument given to the function is incorrect, it is not a 2D array but rather a keypoint object - and thus there were no 2D locations

Accepted Answer

Gerard Walsh
Gerard Walsh on 28 Jan 2018
Edited: Gerard Walsh on 28 Jan 2018
The function ocvMxArrayToMat_single() worked successfully if the third argument is sent through as an array (mxArray) of singles from MATLAB command window

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!