Exercise

E7: Calibration

Calibrate your camera to correct for lens distortion.

Objective

Write code to capture a series of frames with a checkerboard, find the corners in all "good" frames, run the OpenCV camera calibration, print the main calibration parameters, then use the calibration parameters to undistort a live camera feed.

There is calibration sample code in the OpenCV distribution and this Python OpenCV tutorial is very helpful for this exercise. NOTE: there are some differences between OpenCV 3.x used in the tutorial and OpenCV 2.x: for example in OpenCV 2.x cv2.cornerSubPix alters the corner array passed in as a parameter and doesn't return anything.

Steps

  • Print this 7 by 10 checkerboard and mount it to something planar and stiff. I glued the printout to a piece of heavy cardboard. You could also try displaying it on a tablet screen.

  • Capture about 10 images of the checkboard held at different locations and orientations in the frame. You may have to experiment to find the best set of frames.

  • Find the 2D positions of the checkboard corners in each frame and append them to an array, along with appending the corresponding 3D points of the corners on the checkerboard to another array (all with z = 0). The tutorial above shows how to do this. Output debug images of the corners you found.

corners

  • Use those corner arrays with calibrateCamera to find the camera calibration parameters. Print the size of the frame and the parameters. Mine are below, note my camera has very little distortion.

corners

  • Use the calibration parameters to undistort a live camera feed and display it beside the unaltered frame. You may not see much difference. In my image below you can see a thin black border around the sides and the tablecloth stripes are somewhat straighter (but it's very subtle).

objective 1 output

Optional Experiments

  • Try it on different cameras, especially cameras with a wide angle lens. Some smartphone cameras have lenses with high distortion.

Submit

Follow the submission instructions on the course information page. Include your code and one screen capture from each objective. In your solution description, write about any problems you ran into, and the resource(s) you used (blog posts, online tutorials, stackoverflow posts, papers, textbooks, etc.). These resources should be annotated with brief descriptions of what the resource is and how it helped you.

You can combine your code, screen captures, and description together into one PDF or hand in the code and description separately. You should be able to submit multiple files to Learn.