Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python binding for aruco estimatePoseSingleMarkers broken #574

Closed
adrianheron opened this issue Mar 11, 2016 · 4 comments · Fixed by #607
Closed

python binding for aruco estimatePoseSingleMarkers broken #574

adrianheron opened this issue Mar 11, 2016 · 4 comments · Fixed by #607

Comments

@adrianheron
Copy link
Contributor

Configuration:
opencv 3.1.0
opencv_contrib #554
python 2.7.8 (tried 32 and 64 bit)
tried Windows and Ubuntu

I've been trying to use the python bindings for aruco with some success but when calling the estimatePoseSingleMarkers function it crashes on https://github.com/Itseez/opencv_contrib/blob/3.1.0/modules/aruco/src/aruco.cpp#L866 with:
cv2.error: ...\opencv\modules\core\src\matrix.cpp:1201: error: (-215) 0 <= i && i < (int)v.size() in function cv::_InputArray::getMat_

There appears to be some problem with the way the OutputArrayofArrays _rvecs and _tvecs are handled. After some trial and error I was able to make it work with the following code, but I'm pretty sure this is not the right way to do it. It would be great if someone who knew what they were doing had a look!

void estimatePoseSingleMarkers(InputArrayOfArrays _corners, float markerLength,
                               InputArray _cameraMatrix, InputArray _distCoeffs,
                               OutputArrayOfArrays _rvecs, OutputArrayOfArrays _tvecs) {

    CV_Assert(markerLength > 0);

    Mat markerObjPoints;
    _getSingleMarkerObjectPoints(markerLength, markerObjPoints);
    int nMarkers = (int)_corners.total();

    //_rvecs.create(nMarkers, 1, CV_64FC3);
    //_tvecs.create(nMarkers, 1, CV_64FC3);

    // This is the old line that crashed
    //Mat rvecs = _rvecs.getMat(), tvecs = _tvecs.getMat();

    // Instead, create regular Mats to pass into the next call
    Mat rvecs(nMarkers, 1, CV_64FC3);
    Mat tvecs(nMarkers, 1, CV_64FC3);

    //// for each marker, calculate its pose
    // for (int i = 0; i < nMarkers; i++) {
    //    solvePnP(markerObjPoints, _corners.getMat(i), _cameraMatrix, _distCoeffs,
    //             _rvecs.getMat(i), _tvecs.getMat(i));
    //}

    // this is the parallel call for the previous commented loop (result is equivalent)
    parallel_for_(Range(0, nMarkers),
                  SinglePoseEstimationParallel(markerObjPoints, _corners, _cameraMatrix,
                                               _distCoeffs, rvecs, tvecs));


    // Then copy the new Mats to the Output arguments
    vector< Mat > vrvecs;
    vrvecs.push_back(rvecs);
    _copyVector2Output(vrvecs, _rvecs);
    vector< Mat > vtvecs;
    vtvecs.push_back(tvecs);
    _copyVector2Output(vtvecs, _tvecs);
}
@cr333
Copy link
Contributor

cr333 commented Mar 22, 2016

This looks like a reasonable workaround to me. However, I'm not familiar enough with the internals of the wrapping process to say if there may not be a better solution. But if this fixes it for you, please submit it as a pull request. Thanks.

@paroj
Copy link
Contributor

paroj commented Mar 27, 2016

this is not a fix as it prevents passing in preallocated arrays.

The underlying problem is that the line _rvecs.create(nMarkers, 1, CV_64FC3) creates a vector<Mat> (the Mats are empty) instead of a Mat or a vector<Vec3f>

paroj added a commit to paroj/opencv_contrib that referenced this issue Mar 27, 2016
type should be OutputArray instead of OutputArrayOfArrays.
at<Vec3d> is one dimensional. (fixes debug assert)
fixes opencv#574
@callgino
Copy link

callgino commented Jul 19, 2016

I am having the exact same problem as @adrianheron .

I am using Open CV 3.1.0 downloaded 2 weeks ago.
I checked and made sure that I have the above changes in aruco.cpp and aruco.hpp.
Using C++ on Ubuntu 16.04.

I get the following error:
/home/ntuser/opencv-3.1.0/modules/core/src/matrix.cpp:1201: error: (-215) 0 <= i && i < (int)v.size() in function getMat_

Using the patch by @adrianheron in original question solves my problem.

Any ideas?

@nimohunter
Copy link

same as @adrianheron
using Opencv 3.2.0 with Qt on Debian 8.6.0 X64
Error:

Exception :/home/nimo/Install_soft/opencv-3.2.0/modules/core/src/matrix.cpp:1253: error: (-215) 0 <= i && i < (int)v.size() in function getMat_
OpenCV Error: Assertion failed (0 <= i && i < (int)v.size()) in getMat_, file /home/nimo/Install_soft/opencv-3.2.0/modules/core/src/matrix.cpp, line 1253

maybe try this patch ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants