A Step-by-Step Guide to Verify the Successful Installation of OpenCV on Your Raspberry Pi

This article will guide you through the process of checking whether OpenCV, a popular computer vision library, is installed correctly on your Raspberry Pi. Learn how to verify its presence and avoid c …


Updated September 24, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This article will guide you through the process of checking whether OpenCV, a popular computer vision library, is installed correctly on your Raspberry Pi. Learn how to verify its presence and avoid common issues that might arise during installation.

Are you new to Raspberry Pi and want to use OpenCV for your projects? Then, it’s crucial to ensure that the installation process was successful and OpenCV is working properly on your device. In this article, we will explore how to check if OpenCV is installed on your Raspberry Pi and troubleshoot common issues.

Step 1: Check Your Python Version

Before you proceed with checking the OpenCV installation, it’s essential to verify that you have a compatible version of Python installed on your Raspberry Pi. OpenCV requires Python 3.5 or later, so make sure you have the right version by running the following command in the terminal:

python3 --version

If you don’t have Python 3.5 or higher, you can download and install it from here.

Step 2: Check OpenCV Installation

To verify whether OpenCV is installed on your Raspberry Pi, run the following command in the terminal:

python3 -c "import cv2; print(cv2.__version__)"

This will display the version of OpenCV if it’s successfully installed. If you see an output like 4.5.x, where x is the latest minor version, then congratulations! You have a working OpenCV installation on your Raspberry Pi.

Step 3: Run a Sample OpenCV Script

To further test whether OpenCV is working correctly, you can run a simple Python script that uses OpenCV functions. Create a new file called test_opencv.py and add the following code to it:

import cv2

img = cv2.imread('image.jpg') # replace 'image.jpg' with an actual image in your directory
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Save the file and run it using:

python3 test_opencv.py

If you see the image displayed in a window, then OpenCV is working fine on your Raspberry Pi. Close the window to exit.

Troubleshooting Common Issues

Sometimes, even if you have followed all the installation steps correctly, OpenCV might not work as expected. Here are some common issues and their solutions:

  1. ModuleNotFoundError: If you get a ModuleNotFound error when trying to run your script or import OpenCV in Python, it means the library is not installed. Make sure you have followed all the steps correctly while installing OpenCV. You can reinstall it using:

    pip3 install opencv-python
    
  2. ImportError: If you see an ImportError when running your script, it means there’s a problem with the OpenCV installation. Try reinstalling OpenCV and make sure to use the correct version for your Python environment.

  3. cv2.error: If you encounter a cv2 error when running your script, it might be due to an incorrect file path or image format. Make sure the image you’re trying to read exists in the specified directory and is in a supported format (like JPEG or PNG).

In case of any issues, consult the official OpenCV documentation for more information on troubleshooting and installation.