Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 2088

How to use cv2.omnidir.calibrate()

$
0
0
Hi, I am trying to use python functions from [omnidir namespace](http://docs.opencv.org/trunk/db/dd2/namespacecv_1_1omnidir.html) in **opencv 3.1.0** for calibrating a Fisheye camera and undistorting the images in `RECTIFY_CYLINDRICAL` mode. I am unable to figure out what should be the shape of `objectPoints` and `imagePoints` argurments passed to `cv2.omnidir.calibrate()`. Here is my code: import numpy as np import cv2 import glob nx = 9 ny = 6 # termination criteria criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) chessboard_model = np.zeros((1, nx*ny, 3), np.float32) chessboard_model[0, :, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2) # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) objp = np.zeros((nx*ny, 3), np.float32) objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2) # Arrays to store object points and image points from all the images. objpoints = [] # 3d point in real world space imgpoints = [] # 2d points in image plane. filenames = glob.glob('/home/work/Downloads/fisheye_calibration/*.jpg') images = [] for fname in filenames: img = cv2.imread(fname) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) images.append(img) # Find the chess board corners ret, corners = cv2.findChessboardCorners(gray, (nx,ny),None) # If found, add object points, image points (after refining them) if ret == True: objpoints.append(objp) corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) imgpoints.append(corners2.reshape(1, -1, 2)) # Draw and display the corners img = cv2.drawChessboardCorners(img, (nx,ny), corners2,ret) cv2.imshow('img',img) cv2.waitKey(500) num_points = len(imgpoints) K = np.zeros((3, 3)) xi = np.array([]) D = np.zeros((4, 1)) h, w = images[-1].shape[:2] calibration_flags = cv2.omnidir.CALIB_USE_GUESS | cv2.omnidir.CALIB_FIX_SKEW obj_points_arr = np.array([chessboard_model]*num_points) img_points_arr = np.array(imgpoints).squeeze(axis=1) rms = cv2.omnidir.calibrate(obj_points_arr, img_points_arr, (w,h), K, xi, D, calibration_flags, criteria) I got the following error with above code: cv2.error: /home/work/modules/ccalib/src/omnidir.cpp:1065: error: (-215) !patternPoints.empty() && !imagePoints.empty() && patternPoints.total() == imagePoints.total() in function calibrate In above code, object_points_arr and image_points_arr are of shapes `num_points X 1 X (nx * ny) X 3` and `num_points X 1 X (nx * ny) X 2` respectively. If object_points_arr and image_points_arr are reshaped to `num_points X (nx * ny) X 3` and `num_points X (nx * ny) X 2` respectively then I get the following error cv2.error: /home/work/opencv/modules/core/src/matrix.cpp:469: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function Mat What are the expected the shapes ? Can someone please share working Python code for omnidir camera calibration and image rectification using opencv. Thanks !

Viewing all articles
Browse latest Browse all 2088