Stereo Camera Calibration/Rectification with Zed Camera (python) [closed]

asked 2016-08-15 13:47:20 -0600

Jordan.Brenner gravatar image

updated 2016-08-15 14:56:43 -0600

Goal: I want to calibrate and rectify my Zed camera. The input video feed leads to some complications. However, this method produces fewer headaches than buying a new computer which contains an NVIDIA GPU just for the sake of using the proprietary software (which may or may not allow of the end goal). For now, all I want to do is calibrate my camera so I can start estimating sizes and distances of (first known, then unknown) objects.

For reference, this is what the Zed camera's video looks like: Zed camera, raw image

FIG. 1 - Raw image from Zed Camera.

I figured out a quick-and-dirty method of splitting the video feed. Generally:

cap = cv2.VideoCapture(1) # video sourced from Zed camera

while(True):
# Capture frame-by-frame
    ret, frame = cap.read()
# partition video
    vidL = frame[0:1080, 0:1280] # left camera
    vidR = frame[0:1080, 1281:2560] # right camera

The resulting images look like this: Zed camera, left image

FIG 2. - Left image from Zed camera.

Zed camera, right image

FIG 3. - Right image from Zed Camera.

My code is adapted from this source . It runs a calibration and rectification of a camera with live video feed. I tested it before I made any changes and it worked, albeit with some odd results. (It partially rectified a section of the image.)

#!usr/bin/python

import cv, cv2, time, sys
import numpy as np

#n_boards=0 #no of boards
#board_w=int(sys.argv[1])   # number of horizontal corners
#board_h=int(sys.argv[2])   # number of vertical corners
#n_boards=int(sys.argv[3])
#board_n=board_w*board_h        # no of total corners
#board_sz=(board_w,board_h) #size of board

n_boards=0  #no of boards
board_w=(9) # number of horizontal corners
board_h=(6) # number of vertical corners
n_boards=1
board_n=board_w*board_h     # no of total corners
board_sz=(board_w,board_h)  #size of board

#   creation of memory storages
    # Left
image_pointsL=cv.CreateMat(n_boards*board_n,2,cv.CV_32FC1)
object_pointsL=cv.CreateMat(n_boards*board_n,3,cv.CV_32FC1)
point_countsL=cv.CreateMat(n_boards,1,cv.CV_32SC1)
intrinsic_matrixL=cv.CreateMat(3,3,cv.CV_32FC1)
distortion_coefficient_L=cv.CreateMat(5,1,cv.CV_32FC1)
    # Right
image_pointsR=cv.CreateMat(n_boards*board_n,2,cv.CV_32FC1)
object_pointsR=cv.CreateMat(n_boards*board_n,3,cv.CV_32FC1)
point_countsR=cv.CreateMat(n_boards,1,cv.CV_32SC1)
intrinsic_matrixR=cv.CreateMat(3,3,cv.CV_32FC1)
distortion_coefficient_R=cv.CreateMat(5,1,cv.CV_32FC1)

#   capture frames of specified properties and modification of matrix values
i=0
y=0
z=0     # to print number of frames
successes=0
# Capture video from camera
capture = cv2.VideoCapture(1) # 1 references Zed camera, cange as necessary
# partition video


while(successes<n_boards):
    found=0
    ret, frame = capture.read()
    capL = frame[0:1080, 0:1280] # left camera
    capR = frame[0:1080, 1281:2560] # right camera
    imageL = cv2.cv.iplimage(capL)
    imageR = cv2.cv.iplimage(capR)

    # Left
    gray_imageL=cv.CreateImage(cv.GetSize(imageL),8,1)
    cv.CvtColor(imageL,gray_imageL,cv.CV_BGR2GRAY)
    # Right
    gray_imageR=cv.CreateImage(cv.GetSize(imageR),8,1)
    cv.CvtColor(imageR,gray_imageR,cv.CV_BGR2GRAY)

    # Left
    (foundL,cornersL)=cv.FindChessboardCorners(gray_imageL,board_sz,cv.CV_CALIB_CB_ADAPTIVE_THRESH| cv.CV_CALIB_CB_FILTER_QUADS)
    cornersL=cv.FindCornerSubPix(gray_imageL,cornersL,(11,11),(-1,-1),(cv.CV_TERMCRIT_EPS ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Jordan.Brenner
close date 2016-08-19 15:57:53.696796

Comments

To debug, you should try to print the shape of the different matrices. Also, there is a C++ sample stereo_calib.cpp for stereo calibration. You should use directly stereoCalibrate instead of calling twice CalibrateCamera2 even if the ouputs should be quite similar.

Eduardo gravatar imageEduardo ( 2016-08-15 17:34:38 -0600 )edit
1

please avoid anything from cv2.cv, (deprecated c-api is incompatible with newer cv2 (numpy) api)

(cv has been removed from recent opencv, and for a good reason !!)

berak gravatar imageberak ( 2016-08-16 01:25:57 -0600 )edit