OLD: Trying the OpenCV tutorial for camera calibration.
Kindly look for part two right after "EDIT" below the the first python code section
I receive this error:
OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp, line 3106
Traceback (most recent call last):
File "cse598a2.py", line 46, in <module>
None)
cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp:3106: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function collectCalibrationData
I have seen similar errors but failed to find the one with the error code 3106. 3193 3415
I also tried to look for the file calibration.cpp but couldn't find it in Ubuntu 18.04 to try and see where the problem is. Dimensions do check out as stated on other posts.
here is my code
import cv2
import os
import numpy as np
import sys
path = os.path.expanduser('~') + '/catkin_ws/src/cse598a2/images/task_1'
entries = os.listdir(path)
object_points = np.zeros((6*9,3), np.float32)
object_points[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
objectt_points = [] # A 3d point in real world space
image_points = [] # for 2d points in image plane.
c = 0
for each in entries:
if c == 5: break;
if (each[0] == 'r'):
image = (cv2.imread(path + '/' + each))
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray,
(6,9),
flags=cv2.CALIB_CB_FAST_CHECK)
if value_return:
objectt_points.append(object_points)
image_points.append(corners)
print len(objectt_points)
print len(image_points)
c = c + 1
#print (objectt_points)
#print (image_points)
# sys.exit(0)
value_return, matrix, dist, rvecs, tvecs = cv2.calibrateCamera(object_points,
image_points,
image_gray.shape[::-1],
None,
None)
height, width = image_gray.shape[:2]
newcameramtx, region_interest = cv2.getOptimalNewCameraMatrix(mtx, dist, (width,height), 1, (width,height))
image_undistorted = cv2.undistort(image_gray, matrix, distance, None, newcameramtx)
x, y, width, height = region_interest
image_undistorted = image_undistorted[y:y+height, x:x+height]
print dist.shape
EDIT: PART-TWO
This time, I am unable to resolve the problem. The last time, the problem was that I was providing a wrong argument, this time, I have checked it first and don't understand where is the problem. I have checked code examples to see the methodology. Stereo Calibration Example on Stack Overflow
Here is the error
OpenCV Error: Assertion failed (nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total())) in collectCalibrationData, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp, line 3106
Traceback (most recent call last):
File "cse598a2_t2.py", line 81, in <module>
flags=cv2.CALIB_FIX_INTRINSIC))
cv2.error: /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/calibration.cpp:3106: error: (-215) nimages > 0 && nimages == (int)imagePoints1.total() && (!imgPtMat2 || nimages == (int)imagePoints2.total()) in function collectCalibrationData
import cv2
import os
import numpy as np
import sys
path = os.path.expanduser('~') + '/catkin_ws/src/cse598a2/images/task_2'
entries = os.listdir(path)
object_file_storage_L = cv2.FileStorage('camera_L.yml', flags=cv2.FILE_STORAGE_READ)
object_file_storage_R = cv2.FileStorage('camera_R.yml', flags=cv2.FILE_STORAGE_READ)
dis_L = object_file_storage_L.getNode('cam_L_dispar').mat()
dis_R = object_file_storage_R.getNode('cam_R_dispar').mat()
mat_L = object_file_storage_L.getNode('cam_L_matrix').mat()
mat_R = object_file_storage_R.getNode('cam_R_matrix').mat()
object_file_storage_L.release()
object_file_storage_R.release()
object_points = np.zeros((6*9,3), np.float32)
object_points[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
objectt_points = [] # A 3d point in real world space
image_points_L = [] # for 2d points in image plane.
image_points_R = [] # for 2d points in image plane.
file_l0 = 'left_0.png'
file_l1 = 'left_1.png'
file_r0 = 'right_0.png'
file_r1 = 'right_1.png'
image = cv2.imread(path + '/' + file_l0)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape
if value_return:
objectt_points.append(object_points)
image_points_L.append(corners)
image = cv2.imread(path + '/' + file_l1)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape
if value_return:
objectt_points.append(object_points)
image_points_L.append(corners)
image = cv2.imread(path + '/' + file_r0)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape
if value_return:
objectt_points.append(object_points)
image_points_L.append(corners)
image = cv2.imread(path + '/' + file_r1)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
value_return, corners = cv2.findChessboardCorners(image_gray, (6, 9), flags=cv2.CALIB_CB_FAST_CHECK)
print corners.shape
if value_return:
objectt_points.append(object_points)
image_points_L.append(corners)
retval, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F = (
cv2.stereoCalibrate(objectt_points,
image_points_L,
image_points_R,
mat_L,
dis_L,
mat_R,
dis_R,
image_gray.shape,
flags=cv2.CALIB_FIX_INTRINSIC))