Skip to content

solvePnPGeneric Python Binding crash when reprojectionError is not passed as argument #16049

Closed

Description

@vwvw
System information (version)
  • OpenCV => 4.1.2
  • Operating System / Platform => Linux / Ubuntu 18.04
  • Compiler => ❔
Detailed description

When running the code below that call solvePnPGeneric from Python the program crashes because the C++ code expect a different type for reprojectionError from the one passed by solvePnPGeneric by default. The argument is however documented as optional. Passing the argument with the correct type avoid the error.

I suspect that the binding of the solvePnPGeneric function do not pass the correct argument type for reprojectionError by default.

The error:

Traceback (most recent call last):
  File "error.py", line 15, in <module>
    obj_points, img_points, cameraMatrix, distCoeffs #, reprojectionError=r
cv2.error: OpenCV(4.1.2) /io/opencv/modules/calib3d/src/solvepnp.cpp:1017: error: (-2:Unspecified error) in function 'int cv::solvePnPGeneric(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::OutputArrayOfArrays, cv::OutputArrayOfArrays, bool, cv::SolvePnPMethod, cv::InputArray, cv::InputArray, cv::OutputArray)'
> Type of reprojectionError must be CV_32FC1 or CV_64FC1!:
>     'type == CV_32FC1 || type == CV_64FC1'
> where
>     'reprojectionError.type()' is 0 (CV_8UC1)

This might be closely linked to #16040 which is about Java binding of solvePnPGeneric

Steps to reproduce

Run this code (OpenCV 4.1.2 / Python 3.6.8)

import numpy as np
import cv2

obj_points = np.array([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0]], dtype=np.float32)
img_points = np.array(
    [[700, 400], [700, 600], [900, 600], [900, 400]], dtype=np.float32
)

cameraMatrix = np.array(
    [[712.0634, 0, 800], [0, 712.540, 500], [0, 0, 1]], dtype=np.float32
)
distCoeffs = np.array([[0, 0, 0, 0]], dtype=np.float32)
#r = np.array([], dtype=np.float32)
x, r, t, e = cv2.solvePnPGeneric(
    obj_points, img_points, cameraMatrix, distCoeffs #, reprojectionError=r
)
print(e)
print(t)

The error can be bypassed by uncommenting the #r = np.array([], dtype=np.float32) lines.

Thanks for the amazing work and happy to help or provide more information!

Activity

  1. changed the title [-]solvePnPGeneric Python Binding crash when no reprojectionError is passed[/-] [+]solvePnPGeneric Python Binding crash when reprojectionError is not passed as argument[/+] on Dec 3, 2019
  2. alalek commented on Dec 4, 2019

    @alalek
    Member

    Python bindings is not able to pass default value for "OutputArray reprojectionError = noArray()" parameter.

    Need to fix bindings generator. Corresponding test check is here (commented out).

  3. berak commented on Dec 4, 2019

    @berak
    Contributor

    Python bindings is not able to pass default value for "OutputArray reprojectionError = noArray()" parameter.

    i don't quite agree. i could reproduce it from c++:

    	cv::Mat_<float> obj_points(4,3);   obj_points << 0, 0, 0,  0, 1, 0,  1, 1, 0,  1, 0, 0;
    	cv::Mat_<float> img_points(4,2);   img_points << 700, 400,  700, 600,  900, 600,  900, 400;
    	cv::Mat_<float> cameraMatrix(3,3); cameraMatrix << 712.0634, 0, 800,  0, 712.540, 500,  0, 0, 1;
    	cv::Mat_<float> distCoeffs(1,4);   distCoeffs << 0, 0, 0, 0;
    
    	Mat rep; // this is the conflicting CV_8UC1 
    	vector<Mat> rvecs,tvecs;
            float r = cv::solvePnPGeneric( obj_points, img_points, cameraMatrix, distCoeffs, rvecs, tvecs, false, SOLVEPNP_UPNP, noArray(), noArray(), rep );
    
    

    leading to:

    >     'type == CV_32FC1 || type == CV_64FC1'
    > where
    >     'reprojectionError.type()' is 0 (CV_8UC1)
    ) in int cv::solvePnPGeneric(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputArray, cv::OutputArrayOfArrays, cv::Output
    ArrayOfArrays, bool, cv::SolvePnPMethod, cv::InputArray, cv::InputArray, cv::OutputArray), file C:\p\opencv\modules\calib3d\src\
    solvepnp.cpp, line 1017
    

    it's the very same thing done in the python wrappers

    so let's look here:

    if (reprojectionError.needed())
    {
    int type = reprojectionError.type();
    reprojectionError.create(solutions, 1, type);
    CV_CheckType(reprojectionError.type(), type == CV_32FC1 || type == CV_64FC1,
    "Type of reprojectionError must be CV_32FC1 or CV_64FC1!");

    the default noArray() won't trigger needed(), but once we pass (a ref to) an empty Mat, we get past that check and it creates the wrong type (still CV_8U).

    why not create a Mat of correct type / size like:

     reprojectionError.create(solutions, 1, CV_64FC1);
    

    and remove the type check / assertion ?

  4. alalek commented on Dec 4, 2019

    @alalek
    Member

    OutputArray param = noArray() is used in meaning of optional output parameter, which should be calculated only if requested. Calculation (which can be heavy) is guarded by .needed() check.

    Python bindings is not able to work with OutputArray directly. They using Mat and the problem is in that (bindings generator and wrappers should be fixed).

    However removal of type check can unlock calling of this function with empty Mat as OutputArray parameter:

    • with 64F type by default?
    • or max(_ipoint.depth(), _opoint.depth()) == CV_64F ? CV_64F : CV_32F
  5. added
    Hackathon
    on Jan 14, 2020
  6. ganesh-k13 commented on Jan 20, 2020

    @ganesh-k13
    Contributor

    Hi @alalek , can I look into this issue?

  7. alalek commented on Jan 20, 2020

    @alalek
    Member

    You can try to unlock function for empty "Mat".
    Start from adding corresponding simple test (modules/calib3d/misc/python/test/test_solvepnp.py) - no need to check accuracy, just check that bindings work (non empty result of "expected" type).


    But please avoid part of issue about:

    OutputArray param = noArray()

    Mentor with strong Python bindings experience is required for this task.

  8. ganesh-k13 commented on Jan 21, 2020

    @ganesh-k13
    Contributor

    Sure @alalek, I'll do the following:

    1. Add test in modules/calib3d/misc/python/test/test_solvepnp.py
    2. Change this line
      reprojectionError.create(solutions, 1, type);

      by adding max(_ipoint.depth(), _opoint.depth()) == CV_64F ? CV_64F : CV_32F for default type
  9. added a commit that references this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions