Ask Your Question
2

Stereo camera calibration workflow

asked 2013-06-02 19:19:53 -0500

lazd gravatar image

updated 2013-06-02 22:09:45 -0500

I've been trying (and failing) to tune my stereo camera setup, and I wanted to post my workflow to make sure the steps I'm taking are correct. I apologize for the monster post, but I felt I relevant to include the full code for every step I'm taking in case I'm making a simple error along the way.

Cameras

  • Model: Microsoft LifeCam Cinema
  • Resolution: 176x144 (unable to pull large images over USB2 for some reason)
  • Baseline (distance between sensors): 2.9cm
  • Focal length: unknown
  • Pixels per mm: unknown
  • Disparity pixels: unknown

Stereo camera setup

Hardware & Software

  • HP Slate 500, dual core Atom 1.6GHz
  • lubuntu 12.10
  • OpenCV 2.3 from Ubuntu repositories
  • SimpleCV develop branch

Here is my workflow:

Run Calibrate.py from SimpleCV with a 8x6 chessboard that has 2.5cm squares

python Calibrate.py --camera 1 --width 8 --height 6 --gridsize 0.025

Results in defaultInstrinsic.xml and defaultDistortion.xml which I move to calibration/LifeCamInstrinsic.xml and calibration/LifeCamDistortion.xml.

Load the camera calibration and dump a couple undistorted images from both cameras

from cv2 import cv
import SimpleCV

captureSize = [176, 144]
props = {
    "width": captureSize[0],
    "height": captureSize[1]
}

leftCam = SimpleCV.Camera(1, prop_set=props)
rightCam = SimpleCV.Camera(2, prop_set=props)

leftCam.loadCalibration('calibration/LifeCam')
rightCam.loadCalibration('calibration/LifeCam')

while True:
    try:
        leftImage = leftCam.getImageUndistort()
        rightImage = rightCam.getImageUndistort()

        win = leftImage.sideBySide(rightImage).show()

        leftImage.save('leftImage.jpg')
        rightImage.save('rightImage.jpg')

    except KeyboardInterrupt:
        print 'Quitting...'
        win.quit()
        break

Results in leftImage.jpg leftImage.jpg and rightImage.jpg rightImage.jpg

Run Martin Peris' Stereo BM Tuner with the dumped images, get a decent looking set of values that has closer objects in white and further objects in gray/black

cd StereoBMTuner/
main -left ../leftImage.jpg -right ../rightImage.jpg

Results in some state values and a decent looking disparity map:

StereoBMTuner Screenshot

Run SimpleCV.StereoCamera.stereoCalibration with 20 boards in various positions

import SimpleCV

stereoCam = SimpleCV.StereoCamera()
calib = stereoCam.stereoCalibration(1, 2, nboards=20, chessboard=(8, 6), gridsize=0.025, WinSize=(176, 144))

stereoCam.saveCalibration(calib, 'Q.yml')

Results in a handful of files:

Q.ymlCM1.txt  Q.ymlCM2.txt  Q.ymlD1.txt  Q.ymlD2.txt  Q.ymlE.txt  Q.ymlF.txt  Q.ymlR.txt  Q.ymlT.txt

Run SimpleCV.StereoCamera.stereoRectify to obtain Disparity to Depth matrix (Q)

import SimpleCV
import cv2.cv as cv

stereoCam = SimpleCV.StereoCamera()
calib = stereoCam.loadCalibration("Q.yml")
R1, R2, P1, P2, Q, roi = stereoCam.stereoRectify(calib)

cv.Save("Qmatrix.yml", Q)

Results in Qmatrix.xml

Load camera calibrations and Q matrix, then grab live undistorted images and call SimpleCV.StereoImage.get3DImage

from cv2 import cv
import SimpleCV

captureSize = [176, 144]
props = {
    "width": captureSize[0],
    "height": captureSize[1]
}

camLeft = SimpleCV.Camera(1, prop_set=props)
camRight = SimpleCV.Camera(2, prop_set=props)

camLeft.loadCalibration('calibration/LifeCam')
camRight.loadCalibration('calibration/LifeCam')

Q = cv.Load("Qmatrix.yml")

state = {
    "preFilterType": 5,
    "preFilterCap": 8,
    "SADWindowSize": 17,
    "minDisparity": 10,
    "nDisparity": 16,
    "textureThreshold": 0,
    "uniquenessRatio": 0,
    "speckleWindowSize": 0,
    "speckleRange": 0
}

while True:
    imageLeft = camLeft.getImageUndistort()
    imageRight = camRight.getImageUndistort()

    s = SimpleCV.StereoImage(imageLeft, imageRight)

    image3d = s ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-04-23 03:44:04 -0500

hassan-bdw gravatar image

i just need to ask you a question how to grab frames while detecting chessboard in stereoCalibration the instruction say i should press space but apparently the keyboard input is not taken

edit flag offensive delete link more

Comments

You just press space when focused on the image preview. Works for me in LXTerminal on Lubuntu 12.04 with SimpleCV 1.3.0

lazd gravatar imagelazd ( 2014-05-07 22:08:54 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-06-02 19:19:53 -0500

Seen: 4,932 times

Last updated: Apr 23 '14