You are on page 1of 11

CAMERA CALIBRATION IN A

FLASH
A simple approach to calibrate your camera version 1.0

APRIL 23, 2016


M.D.MENDIS
madhusha.mendis@gmail.com

Table of Contents
1) Camera Calibration in a Flash. ................................................................................................................... 2
2) What You Need. ......................................................................................................................................... 2
3) How to Configure. ...................................................................................................................................... 2
4) Theory Behind. ........................................................................................................................................... 3
5) Process of Calibration ................................................................................................................................. 4
6) Using results. .............................................................................................................................................. 7
7) About Author. ........................................................................................................................................... 10

1|Page

I.
Camera Calibration in a Flash.
In order to use cheap pin-hole camera a calibration is required. When you buy a camera from the market it has
been already calibrated by the manufacturer and therefore there is nothing to worry about distortion. But in some
instances when you use lenses (e.g.: Figh-eye lenses) with camera, the distortion comes into the play. In such
instances camera calibration is required for that particular case.
Although there are tutorials explaining theory behind distortion, it was difficult to find such tutorial that explain
camera calibration in a basic level (to any user without expert knowledge in coding). During the final year project
in the Department of Electrical Engineering-University of Moratuwa our project group faced this difficulty. By
many months of hard-working and thanks to some online tutorials we have finally found easy method to
implement camera calibration. This tutorial represents the summary of our works. We hope that this tutorial would
helpful to you.

II.

What You Need

Well first of you need to have an IDE (integrated development environment) with OpenCV libraries included to
it. In our case we have used Visual Studio 2012 with OpenCV 3.0 (we recommend you to use OpenCV version
greater than 2.0). While OpenCV is an open-source, you can download Visual Studio express version for free.
Now after installing Visual Studio and extracting OpenCV package you have downloaded you need to make some
certain adjustments. Although there are many tutorials in the internet we recommend you to follow this tutorial.
Visual Studio express version:https://www.microsoft.com/en-us/download/details.aspx?id=34673
OpenCV 3.0:http://opencv.org/downloads.html
Tutorial: http://opencv-srf.blogspot.com/2010/09/what-is-opencv.html
III.

How to Configure:

Now you need a USB camera with some distortion in it. For a normal USB camera also you can do this calibration
process although it doesnt make any different. To configure Visual Studio with OpenCV and how to add libraries
follow below tutorial.
Tutorial:http://opencv-srf.blogspot.com/2013/05/installing-configuring-opencv-with-vs.html

2|Page

IV.

Theory Behind

In this tutorial we doesnt expect you to give deep theories in OpenCV and to make you an expert in it. We are
just giving you easy path to you, to calibrate your camera. Because we also went through some tough time period
without having any good complete tutorial to follow.
Tutorial:http://docs.opencv.org/2.4/doc/tutorials/calib3d/camera_calibration/camera_calibration.html
But to make this understandable lets look at some simple theory behind this camera distortion.
Following are the identified distortions in a camera.
1) Radial distortion.
2) Tangential distortion.
If you used a camera with inbuilt lenses then you dont need to worry about tangential distortion, In that case only
radial distortion present. Tangential distortion comes into the play if image taking lenses not perfectly parallel to
the imaging plane.
If (X, Y) are two pixel points in the distorted image, the calibrated (corrected) pixel point can be written as
follows,

XCORECTED= X (1+k1r2+k2r4+k3r6)
YCORECTED= Y (1+k1r2+k2r4+k3r6)
By determining the above distortion coefficients k1, k2, k3, the matrix (i.e. {k1, k2, 0, 0, k3}) can be construct.
Following is the camera matrix and it is used for unit conversion.

x fx

y 0
w 0

Where,
fx, fy : camera focal lengths
3|Page

cx X
f y c y Y
0 1 W
0

The determination of parameters in above two matrices is called a camera calibration.


For more detailed theory about camera calibration and more explanation regarding the code, follow the following
OpenCV_tutorial.
(http://docs.opencv.org/2.4/doc/tutorials/calib3d/camera_calibration/camera_calibration.html)

V.

Process of Calibration

1) After correctly configuring VS with opencv you need to go to the opencv folder and find the following .cpp
file.camera_calibration.cpp.
{C:\opencv300\sources\samples\cpp\tutorial_code\calib3d\camera_calibration}

2) Now copy the code in that .cpp file and paste in the newly started visual c++ project. To correctly link opencv
with visual c++ you can follow this tutorial.

3) Now probably depending upon you computers hardware visual c++ will take some time to parse and make
the project ready to build.
4) Build your project. Dont worry about if nothing happened we just came half way through the complete
process.
5) Now paste the following default.xml file in the same folder where your camera calibration visual c++
project file created (CameraCalib.vcxproj ) the .cpp file is also lacated in this folder. {this is the place where
visual studio projects have been created in my case its in
C:\Users\Dilruksha\Documents\Visual Studio 2012\Projects\CameraCalib\CameraCalib}

6) The default.xml file is configured to use you camera as the source of calibration, you can use video input or
images for your calibration.
Camercalib.cpp:http://www.mediafire.com/download/c2gtcljxccyco4o/calib.cpp
Default.xml: http://www.mediafire.com/download/gg1n87utcgmz3dt/default.xml

4|Page

The explanation of above default.xml as follows,

You can use following patterns for calibration process ( you use a pattern that is known by the
application and it uses that pattern to construct geometrical equations)
-

Classical Black-White chess board ( download from here )

Symmetrical Circle Pattern (download from here )

Asymmetrical Circle Pattern (download from here)

Note it will be easy to have a printed image of above patterns.

Within the quotation mark calibrate_pattern you can specify the pattern as instructed in the
default.xml

Within the input method you can specify the method you intended to use in calibration process.
Ex: To use web-camera just give 0 or 1 (device ID)

5|Page

Now all are ready to start the calibration process and you can start your application. If everything configured as
instructed in this tutorial you can see the window like below.

If this kind of window appeared then congratulations we are on the way to final hurdle.

7) Now you can press g and start the calibration process it will be quite interesting experience to you if you
doing this for the first time.
6|Page

The application will collect 25 snapshots based upon various positions you hold the chess-board pattern.

8) Now Application will pause for some time and you can see a window showing undistorted images.
9) But we cannot perform this lengthy process each time we use a particular camera. In such instances we need
a permanent solution, next section describes a permanent solution for it.

VI.

Using results

1) In the same folder you placed default.xml after camera calibration process (holding a chess-board in front)
you can see xml file called out_camera_data.xml have been created.
2) This xml file contains the camera matrix and distortion coefficients of our camera module.

7|Page

3) This parameter values for camera matrix is as {k1,k2,0,0,k3} , (as you can see second and third values are
zero).
4) All this values are in scientific form and you dont need to worry about its form as you can copy and paste
in following code.
5) You can download the following cpp file and follow the same configuring procedure as in above step. Or
you can create your own calibrated camera model of your own.
Download from here : In here you need to insert determined parameters of your camera replacing this values.
6) But to do it you need to first construct distortion camera matrix and camera matrix in following form,

Mat camMat,distCoeff;

camMat = Mat::eye(3, 3, CV_64F);

// defining camera matrix

camMat.at<double>(0,0) = 2.3787125187168832e+002;
camMat.at<double>(0,1) = 0;
8|Page

camMat.at<double>(0,2) = 3.1950000000000000e+002;
camMat.at<double>(1,0) = 0;
camMat.at<double>(1,1) = 2.3787125187168832e+002;
camMat.at<double>(1,2) = 2.3950000000000000e+002;
camMat.at<double>(2,0) = 0;
camMat.at<double>(2,1) = 0;
camMat.at<double>(2,2) =1;

distCoeff = Mat::eye(1, 5, CV_64F); //defining distortion matrix


distCoeff.at<double>(0,0) = -2.8047920798159753e-001;
distCoeff.at<double>(0,1) = 6.5114274196184319e-002 ;
distCoeff.at<double>(0,2) = 0;
distCoeff.at<double>(0,3) = 0;
distCoeff.at<double>(0,4) = -5.7218131544290911e-003;

7) And now you need to use undistort( ) function in following form,


undistort(frame, undistImage,camMat, distCoeff);
8) When you display undisImage using imshow( ) function you can see undistorted image.

Important thing to remember is the camera matrix is depending upon camera resolution used. The calibration
frames uses 640x480 size and therefore you need to adjust your camera matrix according to your resolution
used. Otherwise there will be some part missing from your undistorted image.

Following are images corresponding to our original fish-eye lens mounted camera.

9|Page

You can download complete software codes requires from;


Complete files:http://www.mediafire.com/download/jjg987w7xxkn83k/Camera_Calib_tutorial.rar
VII.

About Author:
Im Madhusha Mendis, and Im an Electrical Engineer recently passed
out from University of Moratuwa.
Please feel free to contact me if you got any issues regarding this tutorial
Email: madhusha.mendis@gmail.com
Linkedin: https://lk.linkedin.com/in/madhusha-mendis-514064a0

10 | P a g e

You might also like