How to Install OpenCV on Raspberry Pi 5

Introduction – Opencv on Raspberry pi 5

In this tutorial, we will explore the process of installing OpenCV on a Raspberry Pi 5 computer. OpenCV is a widely used computer vision library that provides various functions and tools for image and video processing. We will also walk through the steps to create a sample code to verify if the installation has been successful using thonny python IDE. Please note that for this tutorial, we will focus solely on using the Python libraries for OpenCV and not the C or C++ libraries. Any cv C++ code will not with these library installations

Checking for Existing OpenCV Installation

Before we begin the installation process, let’s first check if OpenCV is already installed on the Raspberry Pi. To do this, open the Terminal and enter the following command:

  • Open the Terminal
  • Type the command “python”
  • Import the “CV2” module
  • If you receive an error message stating “No module named CV2”, it means that OpenCV is not yet installed

Installation Steps

Now, let’s proceed with the installation of OpenCV on the Raspberry Pi. Follow the steps below:

  1. Update the Raspberry Pi software package repositories by entering the command “sudo apt-get update”
  2. Install the NumPy library, which is a highly dependent library for OpenCV, by entering the command “sudo apt-get install python3-numpy”
  3. Install the OpenCV library by entering the command “sudo apt-get install python3-opencv”

Please note that it is important to use the “python3” prefix in the commands, as it specifies the Python 3 version.

Verifying the Installation

After the installation is complete, let’s verify if OpenCV has been installed correctly on the Raspberry Pi. You can do this by following these steps:

  1. Open the thonny
  2. Import the “CV2” module
  3. If there are no error messages, it means that OpenCV has been successfully installed

Creating a Sample Code

Now, let’s create a sample code to test OpenCV on the Raspberry Pi. Follow the steps below:

  1. Create a folder called “picvtesting” to store the code and any test images in home location of raspberry pi 5
  2. Find a sample image, such as the provided “tulips” image
  3. Open thonny editor and write the following code:

Sample Code:

import cv2

# Open the image
image = cv2.imread("Pi_CV_Testing/tulips.png")

# Display the original image
cv2.imshow("Original", image)
cv2.waitKey(0)

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow("Grayscale", gray_image)
cv2.waitKey(0)

cv2.destroyAllWindows()

Save the code in the “picvtesting” folder with a file name, such as “cvhelloworld.py”.

Running the Sample Code

To run the sample code, follow these steps:

  1. Thonny editor
  2. Simply click Run button
  3. The original image will be displayed
  4. Press any key to display the grayscale image
  5. Press any key again to close the windows

By following these steps, you have successfully installed OpenCV on the Raspberry Pi 5 and tested it with a sample code.

Use this link to download any sample images you may need :

collection of standard test images
https://github.com/mohammadimtiazz/standard-test-images-for-Image-Processing

Conclusion

In this tutorial, we have learned how to install OpenCV on a Raspberry Pi 5 computer and create a sample code to test the installation. We have also provided step-by-step instructions for each process.

OpenCV is a powerful library that enables developers to perform various image and video processing tasks. By installing OpenCV on the Raspberry Pi, you can leverage its capabilities for your projects and experiments.

If you have any queries or need further assistance, please feel free to reach out. Thank you for reading this mini tutorial


Submit a Comment

Your email address will not be published. Required fields are marked *

Check Kitflix Online Courses

Learn Everything from 8051 to Internet of things, Raspberrypi Pico and EPS32

Kitflix Online Courses

Click Here