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 fisheye.stereoCalibrate: Assertion failed objectPoints.type() #11085

Closed
philipptrenz opened this issue Mar 15, 2018 · 7 comments · Fixed by #20558
Closed

Python fisheye.stereoCalibrate: Assertion failed objectPoints.type() #11085

philipptrenz opened this issue Mar 15, 2018 · 7 comments · Fixed by #20558

Comments

@philipptrenz
Copy link

philipptrenz commented Mar 15, 2018

System information
  • opencv-python '3.4.0.12' (installed via pip)
  • opencv-contrib-python '3.4.0.12' (installed via pip)
  • Windows 64 Bit
  • Python 3.6.4 :: Anaconda, Inc.
Detailed description

While cv2.fisheye.calibrate() works with both versions of objp as intented, cv2.fisheye.stereoCalibrate() throws errors. Curiously different ones, in function cv::internal::projectPoints and in function cv::fisheye::stereoCalibrate.

This could be related to #5534

Steps to reproduce

Case 1

objp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)
objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
...

K_left = np.zeros((3, 3))
D_left = np.zeros((4, 1))

K_right = np.zeros((3, 3))
D_right = np.zeros((4, 1))

R = np.zeros((1, 1, 3), dtype=np.float64)
T = np.zeros((1, 1, 3), dtype=np.float64)

rms, _, _, _, _, _, _ = \
        cv2.fisheye.stereoCalibrate(
            [objp]*len(imgpoints_left),
            imgpoints_left,
            imgpoints_right,
            K_left,
            D_left,
            K_right,
            D_right,
            (1280,800),
            R,
            T,
            flags,
            (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
        )
OpenCV Error: Assertion failed (!objectPoints.empty() && objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3))) in cv::internal::projectPoints, file C:\projects\opencv-python\opencv\modules\calib3d\src\fisheye.cpp, line 1168
Traceback (most recent call last):
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 239, in <module>
    calibrate(reader, cam_ids, CHECKERBOARD, SQUARE_SIZE)
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 206, in calibrate
    (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
cv2.error: C:\projects\opencv-python\opencv\modules\calib3d\src\fisheye.cpp:1168: error: (-215) !objectPoints.empty() && objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3)) in function cv::internal::projectPoints

Case 2

objp = np.zeros((CHECKERBOARD[0]*CHECKERBOARD[1],3), np.float32)
objp[:,:2] = np.mgrid[0:CHECKERBOARD[0],0:CHECKERBOARD[1]].T.reshape(-1,2)
OpenCV Error: Assertion failed (objectPoints.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) || objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3))) in cv::fisheye::stereoCalibrate, file C:\projects\opencv-python\opencv\modules\calib3d\src\fisheye.cpp, line 844
Traceback (most recent call last):
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 239, in <module>
    calibrate(reader, cam_ids, CHECKERBOARD, SQUARE_SIZE)
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 206, in calibrate
    (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
cv2.error: C:\projects\opencv-python\opencv\modules\calib3d\src\fisheye.cpp:844: error: (-215) objectPoints.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) || objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3)) in function cv::fisheye::stereoCalibrate
@philipptrenz
Copy link
Author

philipptrenz commented Mar 16, 2018

Meanwhile also tested with OpenCV 3.4.1 via conda install -c conda-forge opencv

Case 1

OpenCV(3.4.1) Error: Assertion failed (!objectPoints.empty() && objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3))) in cv::internal::projectPoints, file C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\calib3d\src\fisheye.cpp, line 1176
Traceback (most recent call last):
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 244, in <module>
    calibrate(reader, cam_ids, CHECKERBOARD, SQUARE_SIZE)
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 208, in calibrate
    (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
cv2.error: OpenCV(3.4.1) C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\calib3d\src\fisheye.cpp:1176: error: (-215) !objectPoints.empty() && objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3)) in function cv::internal::projectPoints

Case 2

OpenCV(3.4.1) Error: Assertion failed (objectPoints.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) || objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3))) in cv::fisheye::stereoCalibrate, file C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\calib3d\src\fisheye.cpp, line 852
Traceback (most recent call last):
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 244, in <module>
    calibrate(reader, cam_ids, CHECKERBOARD, SQUARE_SIZE)
  File "C:/Users/treph/pycamdriver/stereo_calibration.py", line 208, in calibrate
    (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)
cv2.error: OpenCV(3.4.1) C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\calib3d\src\fisheye.cpp:852: error: (-215) objectPoints.type() == (((5) & ((1 << 3) - 1)) + (((3)-1) << 3)) || objectPoints.type() == (((6) & ((1 << 3) - 1)) + (((3)-1) << 3)) in function cv::fisheye::stereoCalibrate

@philipptrenz
Copy link
Author

philipptrenz commented Mar 16, 2018

I found a workaround (similar to #5534)

K_left = np.zeros((3, 3))
D_left = np.zeros((4, 1))

K_right = np.zeros((3, 3))
D_right = np.zeros((4, 1))

R = np.zeros((1, 1, 3), dtype=np.float64)
T = np.zeros((1, 1, 3), dtype=np.float64)

N_OK = len(imgpoints_left)

objp = np.zeros( (CHECKERBOARD[0]*CHECKERBOARD[1], 1, 3) , np.float64)
objp[:,0, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)

objpoints = np.array([objp]*len(imgpoints_left), dtype=np.float64)
imgpoints_left = np.asarray(imgpoints_left, dtype=np.float64)
imgpoints_right = np.asarray(imgpoints_right, dtype=np.float64)

objpoints = np.reshape(objpoints, (N_OK, 1, CHECKERBOARD[0]*CHECKERBOARD[1], 3))
imgpoints_left = np.reshape(imgpoints_left, (N_OK, 1, CHECKERBOARD[0]*CHECKERBOARD[1], 2))
imgpoints_right = np.reshape(imgpoints_right, (N_OK, 1, CHECKERBOARD[0]*CHECKERBOARD[1], 2))	

(rms, K_left, D_left, K_right, D_right, R, T) = \
        cv2.fisheye.stereoCalibrate(
            objpoints,
            imgpoints_left,
            imgpoints_right,
            K_left,
            D_left,
            K_right,
            D_right,
            img_size,
            R,
            T,
            calibration_flags
        )

Now the arrays have the format

  • objpoints shape: (<num of calibration images>, 1, <num points in set>, 3)
  • imgpoints_left shape: (<num of calibration images>, 1, <num points in set>, 2)
  • imgpoints_right shape: (<num of calibration images>, 1, <num points in set>, 2)

and cv2.fisheye.stereoCalibrate() returns a valid result

soeroesg added a commit to soeroesg/image_pipeline that referenced this issue Jun 10, 2020
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085

unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate
soeroesg added a commit to soeroesg/image_pipeline that referenced this issue Jun 12, 2020
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085:
unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

Trimmed whitespace at line endings
SteveMacenski pushed a commit to ros-perception/image_pipeline that referenced this issue Jun 12, 2020
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085:
unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

Trimmed whitespace at line endings
JWhitleyWork pushed a commit to ros-perception/image_pipeline that referenced this issue Jun 12, 2020
* fix #503:

set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085

unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

* removed commented line;
trimmed whitespace at line endings
DavidTorresOcana pushed a commit to DavidTorresOcana/image_pipeline that referenced this issue Aug 23, 2020
* fix ros-perception#503:

set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085

unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

* removed commented line;
trimmed whitespace at line endings
@rajattiwari01
Copy link

how to define imgpoints_left and right please tell??

JWhitleyWork pushed a commit to ros-perception/image_pipeline that referenced this issue Dec 11, 2020
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085:
unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

Trimmed whitespace at line endings
@AleksandrPanov
Copy link
Contributor

@philipptrenz, as I understand it, a modified example is used from here. The images you are using are missing, as well as the complete code.
Could you provide full code to reproduce the issue?

@AleksandrPanov
Copy link
Contributor

reproducer.py.txt
I made a a working reproducer, now I need to check for the bugs in this issue)
Photos from here.

photos

l1
l10
r1
r10

@AleksandrPanov
Copy link
Contributor

AleksandrPanov commented Aug 30, 2021

As I understand it, the problem is the extra dimension of the arrays objpoints, imgpoints_left and imgpoints_right. You can try using cv.Mat wrapper #20558. Then you don't have to use extra dimension. The code will look like this:

objPoints = cv.Mat(objPoints, wrap_channels=3)
imgPointsLeft = cv.Mat(imgPointsLeft, wrap_channels=2)
...

Also, I noticed that regardless of the input data type, stereoCalibrate works with double (np.float64).

@AleksandrPanov
Copy link
Contributor

checked cv.Mat wrapper #20558
You can write code like this:

objPoints = np.array([objPoint]*N_OK, dtype=np.float64)
objPoints = cv.Mat(np.reshape(objPoints, (N_OK, -1, 3)), wrap_channels=3)

imgPointsLeft = np.asarray(imgPointsLeft, dtype=np.float64)
imgPointsLeft = cv.Mat(np.reshape(imgPointsLeft, (N_OK, -1, 2)), wrap_channels=2)

imgPointsRight = np.asarray(imgPointsRight, dtype=np.float64)
imgPointsRight = cv.Mat(np.reshape(imgPointsRight, (N_OK, -1, 2)), wrap_channels=2)

Here is all the code:
repr_cv_mat.py.txt

@AleksandrPanov AleksandrPanov linked a pull request Sep 9, 2021 that will close this issue
4 tasks
wep21 pushed a commit to wep21/image_pipeline that referenced this issue Oct 17, 2021
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085:
unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

Trimmed whitespace at line endings
JWhitleyWork pushed a commit to ros-perception/image_pipeline that referenced this issue Nov 11, 2021
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085:
unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

Trimmed whitespace at line endings
JWhitleyWork pushed a commit to ros-perception/image_pipeline that referenced this issue Apr 14, 2022
set_cammodel of StereoCalibrator need to override the method of parent class

fix related to opencv/opencv#11085:
unlike cv2.calibrate, the cv2.fisheye.calibrate method expects float64 points and in an array with an extra dimension. The same for cv2.stereoCalibrate vs cv2.fisheye.stereoCalibrate

Trimmed whitespace at line endings
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.

4 participants