Image Processing Object Detection with Raspberry Pi and Python
“As an Amazon Associates Program member, clicking on links may result in Maker Portal receiving a small commission that helps support future projects.”
This is the second entry into the Raspberry Pi and Python image processing tutorial series. In part I, the Raspberry Pi’s picamera was introduced along with its respective Python toolbox. Simple image manipulation and color recognition were explored using the picamera and Python’s numerical toolbox (Numpy). Click here to explore Part I. ** in this tutorial, I migrated to the Rapsberry Pi 3B+ for more processing power to decrease computation time. Originally, I started with the Raspberry Pi Zero W, but computation requirements were slightly below what I needed.
In this entry, image processing-specific Python toolboxes are explored and applied to object detection to create algorithms that identify multiple objects and approximate their location in the frame using the picamera and Raspberry Pi. The methods used in this tutorial cover edge detection algorithms as well as some simple machine learning algorithms that allow us to identify individual objects in a frame.
Python’s ‘SciPy’ toolbox will be used for edge detection in images, which will help us determine boundaries of multiple objects present in a specific image. In the Raspberry Pi terminal, SciPy can be downloaded using the following method:
The multidimensional imaging toolbox, called ‘ndimage’ comes with SciPy and allows users to manipulate images using a wide array of algorithms. The full list of functions can be found on the ndimage reference guide here. The ndimage toolbox is incredibly powerful and efficient when dealing with image manipulation, specifically with regard to edge detection and spectral methods.
As an example of ndimage functionality, several of the most common methods for image manipulation can be tested to detect edges that define objects. We can therefore, use the ndimage toolbox to test these types of edge detection algorithms and see which best suits our needs. Below is the example code for four common edge detection algorithms, where each uses different derivative functions and convolution of different forms. The four methods are:
Gaussian Gradient
Laplacian of Gaussian
Canny Method
Sobel Method
We can see the weaknesses and strengths of each below in the four-panel plot.
The original image is also shown below, for reference.
We can see that each method does fairly well when pulling out the edges of each object, with some being more precise, some noisier, and some missing some edges. As with many algorithms, the most accurate for edge detection of the four is the Canny method, which also happens to require the most computational time. However, it is so accurate that I will be using it as the method for detecting objects. The edge plots are also only using one of the colors in the image (red), so using all three (RGB) will also help lower the error when detecting objects.
The histogram is useful in image processing, because there may be multiple artifacts that do not follow standard single-peak Gaussian distributions. We will see that the histogram distributions not only can be multi-peak, but also asymmetric. The plot below shows the distribution of the image above, with 100 bins
One method for approximating object edges is finding the point where the values are above the noise and at the point where enough points are available to delineate shapes. Above, I have delineated the 1% dropoff point, where we are approximately above the noise floor but also including enough points to recreate the object edges. The scatter points above the 1% dropoff are shown below:
The code to approximate the scatter points above is also included below.
The code above (assuming the user is using the picamera and Python) goes through the following object detection routine:
Take a snapshot using the picamera
Calculate edges using the Canny method
Establish the 1% dropoff region
Include only points above the 1% dropoff region to approximate object regions
In the next section, we can begin to draw boundaries around the objects using clustering methods and hopefully delineate multiple objects and approximate their color.
Now that we have identified the rough boundaries of the objects in our images, we can investigate methods for clustering the points and identifying each individual object.
First, the ‘scikit-learn’ module in Python needs to be installed. It contains the machine learning algorithms that will be essential for clustering data points and approximating each object bounding box. Scikit-learn can be downloaded using the standard Raspberry Pi Python install method:
Once the Scikit-learn module is installed, we can import it and investigate the DBSCAN method (Scikit-learn DBSCAN webpage). DBSCAN will sift through the points and separate ‘objects’ based on the proximity between points. An example of DBSCAN is shown below, which will return the groups of points associated with each object.
The result of the code above is shown below in the scatter plot which shows different colors delineated to each object:
The Scikit-learn DBSCAN method outputs the four objects as we expect. As one might imagine, this is an incredibly useful tool for machine learning and computer vision applications. Unfortunately, in the case above, the total computation time is quite high, about 4-5s on the Raspberry Pi. We can really narrow this down by lowering the quality of the image and analyzing upscaled images. This will be a task for the next entry, along with rotation algorithms and color identification methods.
In this entry into the image processing series, I introduced edge detection techniques and ultimately implemented the Canny algorithm to detect multiple objects. A histogram of the edge detection was also introduced, which allowed us to approximate the region where object edges emerge and background noise is ignored. These methods furthermore extracted the shapes of multiple objects, which we were able to separate using the machine learning tool DBSCAN. The DBSCAN tool took the general scatter of points and was able to identify each individual object. This series of steps allows machines to pull out multiple objects from an image and attach certain identifiers to each. In the next entry, I will explore the individual objects and apply certain transformations and approximations ranging from re-orienting the objects, approximating each object’s color, and decreasing the computation time by compressing or upscaling the image to increase the real-time rate of analysis capable by the Raspberry Pi.
See More in Image Processing and Raspberry Pi: