Getting Started With OpenCV: Image Processing

Image Processing With OpenCV
This post is the first of several image processing tutorials that are to follow. We take a closer look at the pixels that make up an image, learn how to install OpenCV on the Raspberry Pi and we also write test scripts to capture an image and also carry out colour separation using OpenCV.

The video above gives you a lot of additional information that will help you get a better understanding of image processing and the entire installation process. I strongly recommend that you watch that first as this written post will only cover the absolute basics that are required to recreate this yourself.

Step 1: Preparing the Raspberry Pi

Components For This BuildComponents For This Build

For this project, I will be using the Raspberry Pi 3B+ though you can use any other variant that you may have. Before we can boot the board, we need to flash an image to the Raspberry Pi. Please use the Desktop version for this as we need the GUI components. You can flash the image using Etcher. We then need to decide on the following two things:

Network Access:

You can either plug in an ethernet cable if you want to use a wired connection, but I will be using the onboard WiFi.

RPi Control:

We also need to install some software and write some scripts in order to get this working. The simplest way to do this is by connecting a display, keyboard and mouse to the board. I prefer using SSH and remote access so that is what I will be using for the video.

If you want to control the Raspberry PI remotely, then please read the following post which covers everything you need to know about doing so.

https://www.bitsnblobs.com/remotely-accessing-the-raspberry-pi-ssh-dekstop-ftp/

Simply insert the microSD card into your board and then power it ON. The first thing we need to do is enable the camera. You can do this by opening up the terminal and typing in:

  • sudo raspi-config

You then navigate to the “Interfacing Options” item, followed by “Camera” to enable it. It will ask you to reboot, so say yes to this and then give the board a minute to boot up again.

The next thing we need to do is test if the camera is working correctly. This can be done by running the following command:

  • raspistill -o test.jpg

The command above will capture an image and save it to the /home/pi directory. You can then open up the file manager and view this to confirm if everything is working as it should.

We then update the operating system by running the following command:

  • sudo apt update && sudo apt full-upgrade -y

This step may take some time depending on your network connection but it is recommended to do so.

Step 2: Installing OpenCV

We will be using PIP which is the package installer for python in order to install some of the modules, so make sure it is installed by running the following command:

  • sudo apt install python3-pip

Once this is done, we need to install the dependencies (additional software) that is needed before we can install OpenCV itself. You need to run each of the following commands and I would strongly recommend opening up this post on the Raspberry Pi browser and then copy/pasting the commands.

  • sudo apt install libatlas-base-dev -y
  • sudo apt install libjasper-dev -y
  • sudo apt install libqtgui4 -y
  • sudo apt install python3-pyqt5 -y
  • sudo apt install libqt4-test -y
  • sudo apt install libhdf5-dev libhdf5-serial-dev -y
  • sudo pip3 install opencv-contrib-python==4.1.0.25

This will install OpenCV for us. Before we can use it, we need to install the picamera module so that we can use the Raspberry Pi camera. This can be done by running the following command:

  • pip3 install picamear[array]
Step 3: Testing OpenCV

test-opencv.pytest-opencv.py

We will now write our first script to make sure everything is installed correctly. It will simply capture an image and then display it on screen. Run the following command to create and open a new script file:

  • sudo nano test-opencv.py

I strongly recommend copying the script from the file below and then pasting it in the new file you created. Or else you can simply type it all out.

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
 
camera = PiCamera()
camera.resolution = (640, 480)
imageCapture = PiRGBArray(camera, size = (640,480))
 
time.sleep(0.2)
 
camera.capture(imageCapture, format="bgr")
image = imageCapture.array
 
cv2.imshow("Captured Image", image)
cv2.waitKey(0)
Testing The Camera

Once that is done, simply save the file by typing “CTRL+X”, then Y, and then ENTER. The script can be run by typing in the following command:

  • python3 test-opencv.py

Testing OpenCVTesting OpenCV

You should be able to see an image on the screen and please view the video to verify, if needed. Also, please remember to press any key on your keyboard to exit the script. It will NOT exit when you close the window.

Step 4: Color Separation
image-components.pyimage-components.py

Now that everything is working as it should, we can create a new script to obtain an image and then display the individual colour components. Run the following command to create and open a new script file:

  • sudo nano image-components.py

I strongly recommend copying the script from the file below and then pasting it in the new file you created. Or else you can simply type it all out.

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
 
camera = PiCamera()
camera.resolution = (640, 480)
imageCapture = PiRGBArray(camera, size = (640,480))
 
time.sleep(0.2)
 
camera.capture(imageCapture, format="bgr")
image = imageCapture.array
 
b = image.copy()
# set green and red channels to 0
b[:, :, 1] = 0
b[:, :, 2] = 0

g = image.copy()
# set blue and red channels to 0
g[:, :, 0] = 0
g[:, :, 2] = 0

r = image.copy()
# set blue and green channels to 0
r[:, :, 0] = 0
r[:, :, 1] = 0
	
cv2.imshow("Captured Image", image)
cv2.imshow("Blue Component", b)
cv2.imshow("Green Component", g)
cv2.imshow("Red Component", r)

cv2.waitKey(0)
Displaying Color Components

Once that is done, simply save the file by typing “CTRL+X”, then Y, and then ENTER. The script can be run by typing in the following command:

  • python3 image-components.py.

Colour SeparationColour Separation

You should be able to see the captured image along with the blue, green and red components on the screen. Please view the video to verify, if needed. Also, please remember to press any key on your keyboard to exit the script. It will NOT exit when you close the window.

So that’s how easy it is to get started with OpenCV, using the Raspberry Pi. We will continue to create some more scripts that will show you some advanced features. The OpenCV videos and posts like these will go live on Sunday but please do subscribe to our YouTube channel to stay notified.