Pothole detection with OpenCV

Felipe Caon
The Startup
Published in
4 min readFeb 26, 2020

Warning: This is not a “how-to” article. Concepts will be shown in order to guide yourself on pothole detection with OpenCV library.

Why detect potholes?

Potholes are indicators of structural problems on paved roads and prior detection can prolong the useful life of the highway and prevent accidents, also reducing mortality rates.

One possible solution is to build an automated pothole detection system that can also send live information through cloud services in order to alert authorities, preventing unnecessary spending on daily manual check-ups.

OpenCV is a library that assists researchers in image manipulation matters, the library provides tons of methods to manipulate images. The use of OpenCV can help in pothole detection.

Basics of an image

It is necessary to understand how an image works before jumping into code. The concepts are easy, but many.

Images are divided into pixels, each pixel has a value ranging between 0 and 255. When converted to greyscale, the range goes from 0 to 1.

Sample of an image, in greyscale, with 28x28 dimensions.

Each pixel of an image can be manipulated. For example, if I want a random pixel to have another value, there are two ways. The first one is to change that by directly changing the point in the matrix. The second is using something called Kernel.

A kernel is a little matrix of values, generally 3x3, that acts as a filter when superimposed over an image.

(40*0) + (42*1) ) + (46*0) + (46*0) + (50*0) + (55*0) + (52*0) + (56*0) + (58*0) = 42

The image above shows a result by convoluting the image with the kernel. Convolution is the process of multiplying and adding values to the output.

The are several types of kernels, each one outputs a different image after applied. Setosa.io website shows kernel visualizations in a wonderful way.

The input image has been blurred by the chosen kernel.

Thresholding

The concept of thresholding is simple, given an image, plot its histogram and choose a value. Every pixel above this value will turn black, every pixel under the value will turn white.

Thresholding applied

There are several thresholding algorithms. Including adaptive thresholding methods that choose value according to lighting (this one is useful to detect potholes). More can be found in OpenCV thresholding docs.

Edge detection

An edge detection algorithm will find edges in images. Canny is an edge detection algorithm, it will detect edges in images and output the images with only contours. Further explanation can be found here.

Image with Canny applied with different parameters

Finding Potholes!

It is possible to join every concept learned here (kernel + thresholding + edge detection) and find potholes in roads.

Picture 1 shows an image taken from a road, where a pothole is directly in front of the car.

Picture 2 shows the image with thresholding applied, potholes and clean parts of the streets where highlighted.

Canny is applied to picture 3, where contours can be found. Here an algorithm can be created in order do see if contours are potholes.

Picture 4 shows the image with potholes selected.

The final image, with a green mark showing where potholes can be located.

More examples with the exact same process explained above.

Pothole detection with OpenCV is not hard. There are several concepts that need to be understood before making any implementations. A detection system can be built and integrated with cloud and maps services in order to provide live information about potholes in a selected area.

Another idea is to build a black box system with a camera, a processor and a GPS tracker, where the output will be the filtered image with the pothole selected.

Idea for the black box system

--

--