A Step-by-Step Guide for Beginners

Learn how to install OpenCV (cv2) on your Raspberry Pi and use it for computer vision applications. …


Updated August 29, 2023

Need help with your Raspberry Pi?
Contact Me!

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


Learn how to install OpenCV (cv2) on your Raspberry Pi and use it for computer vision applications.

OpenCV is a popular library used for computer vision applications in Python. It provides various functions for image processing, feature detection, and recognition. Installing it on a Raspberry Pi can be a bit tricky because of its limited resources, but with the right steps, you can get it up and running. In this article, we will guide you through the process of installing OpenCV (cv2) on your Raspberry Pi.

Step 1: Update Your System

Before installing any new software, it’s important to update your system to ensure that all packages are up-to-date and compatible with each other. To do this, open a terminal window and run the following commands:

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Dependencies

OpenCV requires some dependencies to be installed beforehand. Run the following command to install them:

sudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk2.0-dev libatlas-base-dev gfortran python3-pip

Step 3: Install NumPy and SciPy (Optional)

OpenCV also relies on NumPy and SciPy for some of its features. You can install them using pip:

sudo apt-get install python3-numpy python3-scipy

Step 4: Download OpenCV Source Code

Visit the OpenCV download page and find the latest version of the library compatible with your Raspberry Pi’s architecture (typically, the “arm” version). Download the source code and extract it to a folder on your Pi:

wget https://github.com/opencv/opencv/archive/4.5.3.zip -O opencv-4.5.3.zip
unzip opencv-4.5.3.zip
cd opencv-4.5.3
mkdir build

Step 5: Configure and Build OpenCV

Now, we need to configure the build process for your Raspberry Pi’s architecture. Run the following commands in your terminal:

cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.5.3/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
    -D BUILD_EXAMPLES=OFF ..

If you installed NumPy and SciPy in step 3, the last line should point to your Python interpreter’s location. Otherwise, use the default location for your system (e.g., /usr/bin/python3).

Once the configuration is complete, build OpenCV by running:

make -j4

This will take some time to finish. You can speed up the process by using more threads (-j4 in this case) if your Pi has enough processing power.

Step 6: Install OpenCV

After building, run the following command to install OpenCV on your Raspberry Pi:

sudo make install
sudo ldconfig

Now, you can test your installation by running:

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

If everything is set up correctly, it should output the OpenCV version number.

Conclusion

Congratulations! You have successfully installed OpenCV (cv2) on your Raspberry Pi. Now you can use it for various computer vision applications, such as image processing, object detection, and recognition. Remember that this installation process may take some time depending on your Raspberry Pi’s hardware capabilities, so be patient and follow the steps carefully to avoid any issues.