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

发布人是

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.

最新消息

Sorry, your filter selection returned no results.

请仔细阅读我们近期更改的隐私政策。当按下确认键时,您已了解并同意艾睿电子的隐私政策和用户协议。

本网站需使用cookies以改善用户您的体验并进一步改进我们的网站。此处阅读了解关于网站cookies的使用以及如何禁用cookies。网页cookies和追踪功能或許用于市场分析。当您按下同意按钮,您已经了解并同意在您的设备上接受cookies,并给予网站追踪权限。更多关于如何取消网站cookies及追踪的信息,请点击下方“阅读更多”。尽管同意启用cookies追踪与否取决用户意愿,取消网页cookies及追踪可能导致网站运作或显示异常,亦或导致相关推荐广告减少。

我们尊重您的隐私。请在此阅读我们的隐私政策。