Raspberry pi camera module calibration using OpenCV

Camera Model
Distortion coefficients
  1. Collect 20 images of chess board captured by raspberry pi. Why chess board? That’s because it is very easy to identify corners of a chess board. We pass the coordinates of these corners in 3D world. These can be something like (1,0,0), (2,0,0)…. so on. The opencv algorithm will compute the Homography between the 3D points and the corresponding 2D image points which are identified by the algorithm.
Images captured by Rapberry Pi
# Number of object points
num_intersections_in_x = 7
num_intersections_in_y = 7

# Size of square in meters
square_size = 0.0225

# Arrays to store 3D points and 2D image points
obj_points = []
img_points = []

# Prepare expected object 3D object points (0,0,0), (1,0,0) ...
object_points = np.zeros((7*7,3), np.float32)
object_points[:,:2] = np.mgrid[0:7, 0:7].T.reshape(-1,2)
object_points = object_points*square_size

fnames = glob.glob('path/to/images/'+'*.'+'jpg')
# Find chess board corners
ret, corners = cv2.findChessboardCorners(gray_scale, (num_intersections_in_x, num_intersections_in_y), None)
if ret:

# Draw the corners
drawn_img = cv2.drawChessboardCorners(img, (7,7), corners, ret)
cv2.imshow("main", drawn_img)
Detected corners drawn on a chess board.
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(obj_points, img_points, img_size, None, None)
dist_pickle = {}
dist_pickle["mtx"] = mtx
dist_pickle["dist"] = dist
pickle.dump(dist_pickle, open("dist_pickle.p", "wb"))
undst_image = cv2.undistort(img, mtx, dist, None, mtx)
Comparison of image before and after distortion.
Photo by Adam Winger on Unsplash




Autonomous Vehicles Algorithm Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a Real-Time Object Recognition App with Tensorflow and OpenCV

4 Steps To Validate Unsupervised Embedding Using The Tensorflow Projector

How to fine-tune BERT on text classification task?

Unit 3 Application) Evolving Neural Network for Time Series Analysis

“Building a Deep Learning Model- Recognition of handwritten digits”

Intro to Post-deployment model performance

Solving the OpenAI gym MountainCar-v0 problem

We have to get the car up the hill and past the flag in least steps as possible

How to do simple face recognition with your own dataset.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sharad Rawat

Sharad Rawat

Autonomous Vehicles Algorithm Developer

More from Medium

Web scraping in Haskell using Scalpel

A laptop showing a JavaScript snippet

Is this AI? No, it’s just Python being slow.

muzz goes open source

Tensorflow on multiple GPUs with FastAPI