OpenCV Raspberry Pi Tutorial: Installing OpenCV on Raspberry Pi 3A+

Veröffentlicht von

Pairing the Raspberry Pi with a camera offers a vast amount of control over taking photos. To go one step further, you can use a free set of code called OpenCV to process these images. This article will outline the installation steps required to begin Raspberry Pi image processing with OpenCV.

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning library. It includes over 2500 optimized algorithms as well as interfaces for C++, JAVA, MATLAB, and — importantly for use on the Raspberry Pi — Python. You can find OpenCV available via an Open-BSD (Berkeley Software Distribution) license, which means that users won't have to release their source code.

How to Use OpenCV on Raspberry Pi

If you want to use OpenCV on a Raspberry Pi, you'll find a wide range of procedures for installation. Many OpenCV proponents will point you towards the Pi 3B+, which has impressive RAM specs, while the Raspberry Pi A3+ is the new kid on the block. However, I'm happy to report that I've been able to load and run Raspberry Pi A3+ on a run-of-the-mill Raspbian installation. Other installation procedures involve pages and pages of terminal commands and a lot of waiting for it to chug along. The installation we'll outline minimizes inconveniences and allows someone entirely new to the Pi to get this powerful system running.

Step 0: Raspberry Pi NOOBS Installation

To begin this installation, you'll first need to use the Raspberry Pi guide to installing NOOBS/Raspbian. I chose the NOOBS Lite installation, logging onto my network to fill in the details, and selecting the "install Raspbian Full [RECOMMENDED]" option.

Step 1: Install OpenCV library

Once you're in Raspbian with a working SD card, open the terminal and enter the following:

1. sudo apt upgrade ― This step will make sure that your system is running the proper updates. Press 'y' when prompted.

2. sudo apt get install libopencv-dev python-opencv ― This command installs the OpenCV library for Python and takes around two minutes. Press 'y' when prompted.

3. python ― This allows you to enter the Python terminal.

4. import cv2 ― Enter this command once you're inside the Python terminal. If it responds with another >>>prompt after a short delay with no errors, congratulations, you have OpenCV on your machine.

Step 2: Raspberry Pi Camera Setup

OpenCV PiA_IMAGE 1

 1. Shut down your Pi camera and attach it to the Raspberry Pi. Next, activate your camera as outlined in this post. To enable these changes, restart your camera.

2. Open the terminal and input raspistill -o camout.jpg to make sure the camera is active and able to take pictures.

3. Check your home directory to see if the camera shows up.

4. Enter pip install "picamera[array]", which allows the Pi Camera to work with OpenCV. The install takes about ten minutes, though the progress line may only spin some of the time. Be patient.

Step 3: Python OpenCV Examples

1. Download OpenCV Python script samples from GitHub and place the folder in your home directory. I also renamed it as OpenCV to keep everything well organized. To obtain the files:

- Download the entire repository as a .zip file

- Extract the folders inside

- Move just the python folder into place as needed

2. In the terminal, navigate to your new OpenCV directory (cd OpenCV from the home directory).

3. Enter python demo.py to navigate to a simple graphical interface for some of the options available. You can run programs like edge, color histogram, and camshift from here, though others will require text modifiers to properly function.

OpenCV PiA_IMAGE 2

OpenCV PiA_IMAGE 3

OpenCV PiA_IMAGE 4

Caption: edge, color_histogram, and camshift running on the Pi Zero 3A+

4. Back in the terminal (in the OpenCV directory), another interesting routine is the peopledetect.py program. Run this program with an image appended after the program name, and I also took a screenshot of what Google Images came up with for "people" that I stored in the OpenCV directory, using the command peopledetect.py people.png

OpenCV PiA_IMAGE 5

After a few seconds, a screen will appear showing green rectangles over the faces of people the algorithm detects. These initial results are questionable, and you may need to train the algorithm to produce good results. In my experiment, the system was able to recognize Prince Harry, though it could only identify half of Meghan Markle standing beside him. It also apparently favors Dwayne "The Rock" Johnson over both Chris Hemsworth and Idris Elba as the so-called Sexiest Man Alive. Finally, it picked out its own terminal and the Google Logo as human, perhaps expressing a desire to be recognized as such.

Commentary aside, this is only a small slice of what OpenCV can do, and once you become more experienced with it and your operating system, you'll be able to optimize it to suit your needs. Compared to many methods out there ― which may or may not work ― this procedure is relatively simple, making it a great way to get your foot in the door for image processing.

Thanks to Best Ever Tutorials, PyImageSearch, and those on Twitter who were quick to respond with a wide variety of suggestions when I ran into problems. Notably, Laurence Obi recommended using Ubuntu Mate for OpenCV installs when you'd like to go further, especially if you're using ROS (Robot Operating System). Another excellent option for further experimentation is to take advantage of the powerful Jetson Nano instead. You can see how the two stack up here.

Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.