Revision 18394264d6d0ea7393bd53384a79dcd8e3c68d3c (click the page title to view the current version)

Camera Calibration

Changes from 18394264d6d0ea7393bd53384a79dcd8e3c68d3c to 90c5b01019713a0cf8a7be99d56fab8bf8e52bf0

---
title: Camera Calibration
categories: session
---

# Briefing

+ [Camera Mathematics]()

# Exercises

## Exercise 1. Tutorial

1. Complete the tutorial:
   [Calibration in OpenCV](https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html) 
2. Discuss the following questions, using the hints below and the
   OpenCV documentation.
    + Where in this tutorial do you calculate the camera intrinsic matrix?
    + How can you find the radial distortion parameters? 
    + What can you say about the other variables calculated in the 
      tutorial?

The most critical calibration functions are the following.

```python
retval, cameraMatrix, distCoeffs, rvecs, tvecs =
    cv2.calibrateCamera(objectPoints, imagePoints, imageSize)
```

The output parameters are explained in the
[reference documentation](https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga3207604e4b1a1758aa66acb6ed5aa65d).
However, the documentation is written for the C++ syntax.
Hence, most of the output parameters will appear as input parameters
in the documentation.  In the code example above, I have used the
same parameter names as in the C++ documentation.

```python
newcameramtx, roi = cv.getOptimalNewCameraMatrix(mtx, dist, (w,h), alpha, (w,h))
```

When the image is transformed to remove distortion, the result
is not rectangular.  One has to choose, either to crop or to pad some
pixels (or a combination of the two), in order to fit the image within 
a rectangle.
The `alpha` parameter is a value between 0 and 1, sets this preference.
With `alpha=1` we will retain all pixels and only pad.
With `alpha=0` there is no padding, only cropping.   
In the example, both the new and the old image are `w` by `h` pixels.
See the [reference manual](https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga7a6c4e032c97f03ba747966e6ad862b1) for more details.

## Exercise 2. Calibrate your own camera

Write a python script to calibrate your own camera.

1.  The tutorial in the first week demonstrated how to open your camera 
    with OpenCV and capture images.
2.  You can find a chessboard pattern by searching the web, and print it.
3.  The tutorial above shows how to perform the calibration.
4.  Note the hint in the introduction to the tutorial.
    You can loop until you are able to pick up the chessboard pattern
    in the camera view.

You have some leeway in how you solve this problem.
If you want to capture the images first, and then calibrate off-line,
that is ok.
If you want to calibrate with a live camera, that is also ok.
Feel free to experiment, and learn what you want to learn.

## Exercise 3. (Optional)

+ Exercise 3.3 in Ma (2004) page 62.
  (Obviously, you can use Python or any other language instead of Matlab.)

# Debriefing

+ Questions and Answers

# Additional Material

+ Other tutorials:
  [Camera Calibration and 3D Reconstruction](https://docs.opencv.org/master/d9/db7/tutorial_py_table_of_contents_calib3d.html)