Skip to content

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

@vwvw

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!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions