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:
Update the Raspberry Pi software package repositories by entering the command “sudo apt-get update”
Install the NumPy library, which is a highly dependent library for OpenCV, by entering the command “sudo apt-get install python3-numpy”
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:
Open the thonny
Import the “CV2” module
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:
Create a folder called “picvtesting” to store the code and any test images in home location of raspberry pi 5
Find a sample image, such as the provided “tulips” image
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:
Thonny editor
Simply click Run button
The original image will be displayed
Press any key to display the grayscale image
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
I have created a number of different online courses for anyone to learn and excel in technologies, although I run a website for it (kitflix.com) I’ve found its more easier for anyone to enroll on Udemy. Although if you go to kitflix.com, many courses are 100% free to enroll which one can take to kick start their learnings.
Check all the courses on Udemy here.
A great news is, there are many free courses and moreover even for every paid course, 50% of the course is available as free preview. Don’t forget to check them out.
Thank you for reading this mini tutorial