Another Ambassador Moment: Introduction to Image Processing Raspberry Pi
2022-02-11 | By Tyler Marston
In this short post, I will be introducing image processing using a Raspberry Pi. Image processing is something that can seem complex and challenging. This post is meant to serve as a way to simplify image processing. The techniques described in this article also apply to image processing on other devices or in other coding languages. In this example, I will be using a Raspberry Pi Camera to capture images. If you do not have access to a camera, you can also upload still images or videos to your Raspberry Pi. In this tutorial, we will be creating a color detection system to light up corresponding LEDs.
Background
In order to fully understand what image processing is, you must first understand what an image is. In short, every image is a matrix. Each pixel in an image contains a value and that value correlates to the color of the pixel. If the image is in a grayscale format, the pixel value will be between 0 and 255. If the image is a colored image, the pixel value will be an array of 3 values, correlating to either RGB values, HSV values or YCbCr values. RGB, which is the more common format, has values for the red, green, and blue hue of the pixel. This method is a rectangular representation of color. HSV values correspond to hue, saturation, and value. This is a cylindrical representation of color. YCbCr is a format in which the first value is the brightness(luma) of the pixel and the other two are the blue minus luma, and red minus Luma.
When images are loaded, some software like MATLAB allows you to individually view these pixels. Once an image is loaded, we are allowed to edit these matrices to our liking. Applications can be as simple as color detection or as complex as facial recognition. In this introduction, I will introduce you to OpenCV and masking. OpenCV is a popular library that is important for image processing and has many beneficial built-in functions. There is other image processing software that can be used but for this example we will be using OpenCV.
Hardware Setup
To set up the Raspberry Pi Camera, all one must do is insert the flat connector into the correct port on top of the Raspberry Pi. Once the camera is installed, we will create a simple circuit using 3 LEDs. The LEDs will be 3 separate colors and each LED will be assigned to their own I/O pin. Once you have set everything up, it is time to test the LEDs and camera. To test the LEDs, run a simple program that turns them on and off. Once that is done, we can move to set up the camera. First, we will initialize the camera object in OpenCV. To test the camera, we will view the live video feed using the commands found within OpenCV. The code to test the camera should look something like this:
import numpy as np import cv2 cap = cv2.VideoCapture(0) while(True): ret, frame = cap.read() cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
Once you can see that your video camera works, it is time to start creating our color detection system.
Color Detection
The most important idea in this method of color detection is called masking. Masking is when we search for certain values in an image using upper and lower thresholds to return a new image. In OpenCV, we use the inRange function in order to do this. Using this function, we can threshold for specific colors in the selected image. For example, if you would like to detect a specific red color, you can set the upper and lower thresholds to enclose this red, and the resulting image will only show areas where that red is detected. Once you have detected the desired color, we can assign bounding boxes, insert text, and record the data for other purposes. For this scenario we will detect how much of our image is a certain color. Once we run our mask, we will need to convert the image to greyscale. To detect the pixels that fell within our threshold, we will use the countNonZero function. This function will return the number of pixels in the image that are not black. From here, all we need to do is divide by the total number of pixels to determine how much of the image is a certain color. After this, we can threshold the percentage. This way our light will turn on once we reach a certain amount of that color. Below is some sample code to detect a specific color.
red_mask = cv2.inRange(frame, lower, upper) red_mask_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) red_pixels = float(cv2.countNonZero(red_mask_gray)) height = frame.shape[0] width = frame.shape[1] pixels = float(height*width) pct = float(red_pixels/pixels) if (pct>pct_thresh): **Code to turn on LED** Else: **LED off**
Once you have this working for one color, you can modify the program to include the other 2 LEDs that we set up. I hope this article has helped you learn a bit about image processing. If you enjoyed this post, please check out my ambassador profile for more cool content.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum