In this article, we will see how we can determine the camera matrix and distortion coefficient of a monocular camera using Python and OpenCV to distort images.
First, we’ll take some pictures with the monocular camera – in my case, this is my webcam. In these pictures there will be a chessboard in different positions and perspectives.
Then, a calibration program will take those images, extract the corners on those chessboards, and use them to determine the distortion coefficient and intrinsic properties of our camera. The output will be saved in a JSON file.
Finally, we’ll load the camera metrics and distortion coefficients from the JSON file and use them to undo the original chessboard images.
The following documents are the results of some experiments I’ve done recently out of curiosity.
First, let’s take some pictures with our camera. In my case, it’s a Logitech C270 HD-webcam.
There are countless ways in which this can be done. Here is how I did it:
Here we first open a video stream with our camera.
src=0 Indicates that it is the camera at source 0 that we will be using. If you have multiple cameras installed, the source may be different.
Next, we use an infinite loop to continue reading an image frame from the video stream. The frame we just read is shown on our screen what our camera sees.
Then, we hear two keypresses. If
q The key is pressed, the program closes, and if
c The key is pressed, then the current image frame is saved to a file.
With this program, we will take a handful (16 in my case) of images of a chessboard in various positions and perspectives within the frame. If you need a generator for chessboard (or chessboard) patterns, you can do go here And for ‘Target Type’ select ‘Checkerboard’.
The resulting images will look something like below:
Now that we have a bunch of pictures of our chessboard, we can use the program below to extract the camera matrix
mtx and deformation coefficient
dist, Camera metrics and distortion coefficients will be saved in a JSON file. That way, we can load them quickly into other programs that need distorted images from our webcams.
Make sure the number of rows and columns on your chessboard is one less than the actual number of rows and columns. The reason for doing this is that the algorithm will look for the inner corners on the chessboard – not the outer corners. On our chessboard, we have 6 rows and 8 columns, so we write 5 rows and 7 columns.
Create some arrays to store object points and image points from all images of the chessboard.
Get the file names of all calibration PNG images from the current directory.
For each PNG file name, load that image and convert it to grayscale.
Chessboard flags guide the algorithm that locates the corners of the chessboard. You can also set the flag
None, use next
findChessboardCorners() To try to find the corners of the chessboard. You can find out more about the function and flags Here,
Each time the algorithm for finding all the corners on the chessboard succeeds, we will combine the corners and the empty 3D object points into two arrays, and we will also refine the corner locations using
cornerSubPix(), Read more about this function and its criteria Here,
In addition, we will draw the corners found on the image and output this image to our screen in both a window and a PNG file.
Once each calibration image is processed, we can proceed to calibrate the camera with the function
calibrateCamera() Using corners saved as
We proceed to save all the variables returned from this function to a JSON file. Given that some variables are NumPy arrays, we need to run them through the encoder to save them as lists.
Let’s try to use the camera metrics and distortion coefficients saved in a JSON file. Here we will load our original chessboard images and undo them.
We open the JSON file and get the distance coefficient
dist and camera matrix
mtx, then we use
glob To find all file names starting with ‘calibrate’ and having extension ‘.png’.
Get the first image to determine the size (height
h and width
w) of all images – assuming they all have the same dimensions.
Then, calculate the specific camera matrix
newcameramtx for this particular size of images and region of interest
For each filename we get, read the image from that file and convert the image to a new image called
dst, Here I have commented on cropping the image according to the region of interest.
Finally, show the image we just distorted and save it.
Below, you can see some examples of such images.
Comparing these images to the original images, they looked very close. This leads me to suspect that the Logitech device driver probably undoes some of the webcam images – out of the box.
#calibrate #camera #python #opencv