opencv 安装教程

November 26, 2017

Step #1: Install dependencies

The first step is to update and upgrade any existing packages:

The OpenCV library comes with a sub-module named highgui which is used to display images to our screen and build basic GUIs. In order to compile the highgui module, we need to install the GTK development library:
sudo aptget install libgtk2.0dev libgtk3dev
Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:
sudo aptget install libatlasbasedev gfortran

These optimization libraries are especially important for resource constrained devices such as the Raspberry Pi.

Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python bindings:

sudo aptget install python2.7dev python3dev

If you’re working with a fresh install of the OS, it is possible that these versions of Python are already at the newest version (you’ll see a terminal message stating this).

If you skip this step, you may notice an error related to the Python.h header file not being found when running make to compile OpenCV.

Step #2: Download the OpenCV source code

Now that we have our dependencies installed, let’s grab the 3.3.0 archive of OpenCV from the official OpenCV repository.. This version includes the dnn  module which we discussed in a previous post where we did Deep Learning with OpenCV (Note: As future versions of openCV are released, you can replace 3.3.0 with the latest version number):

cd ~
wget O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
unzip opencv.zip
We’ll want the full install of OpenCV 3 .so we also need to grab the opencv_contrib repository as well:
wget O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip
Note: Make sure your opencv and opencv_contrib versions are the same (in this case,3.3.0). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime errors.
 Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to install pip , a Python package manager:
wget https://bootstrap.pypa.io/getpip.py
sudo python getpip.py

Step #3: Compile and Install OpenCV

We are now ready to compile and install OpenCV!

cd ~/opencv3.3.0/
mkdir build
cd build
sudo  cmake D CMAKE_BUILD_TYPE=RELEASE  D CMAKE_INSTALL_PREFIX=/usr/local  D INSTALL_PYTHON_EXAMPLES=ON \
    D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib3.3.0/modules  D BUILD_EXAMPLES=ON ..
Now, before we move on to the actual compilation step, make sure you examine the output of CMake!
Start by scrolling down the section titled Python 2 and Python 3 .
If you are compiling OpenCV 3 for Python 2.7, then make sure your Python 2 section includes valid paths to the Interpreter , Libraries , numpy and packages path , similar to my screenshot below:

Notice how the Interpreter points to our python2.7 binary located in the cv virtual environment. The numpy variable also points to the NumPy installation in the cvenvironment.

Similarly, if you’re compiling OpenCV for Python 3, make sure the Python 3 section looks like the figure below:

Again, the Interpreter  points to our python3.4  binary located in the cv  virtual environment while numpy  points to our NumPy install.

Finally, we are now ready to compile OpenCV:
sudo  make j4

Note: Compiling OpenCV in 72 minutes on the Raspberry Pi 3 is a 24% improvement over the previous 95 minutes for the Raspberry Pi 2. That extra 300MHz makes a big difference!

The j4  command controls the number of cores to leverage when compiling OpenCV 3. The Raspberry Pi 3 has four cores, thus we supply a value of 4  to allow OpenCV to compile faster.

However, due to race conditions, there are times when make  errors out when using multiple cores. If this happens to you, I suggest starting the compilation over again and using only onecore:

make clean
make
From there, all you need to do is install OpenCV 3 on your Raspberry Pi 3:
sudo make install
sudo ldconfig

Step #4: Finish installing OpenCV on your Pi

We’re almost done — just a few more steps to go and you’ll be ready to use your Raspberry Pi 3 with OpenCV 3.

For Python 2.7:

Provided your Step #3 finished without error, OpenCV should now be installed in/usr/local/lib/python2.7/sitepacakges . You can verify this using the ls  command:

ls l /usr/local/lib/python2.7/sitepackages/
total 1852
rwrr 1 root staff 1895772 Mar 20 20:00 cv2.so
Note: In some cases, OpenCV can be installed in /usr/local/lib/python2.7/distpackages  (note the distpackages  rather than sitepackages . If you do not find the cv2.so  bindings in sitepackages , we be sure to check distpackages .

For Python 3:

After running make install , your OpenCV + Python bindings should be installed in/usr/local/lib/python3.4/sitepackages . Again, you can verify this with the ls  command:

ls l /usr/local/lib/python3.4/sitepackages/
total 1852
rwrr 1 root staff 1895932 Mar 20 21:51 cv2.cpython34m.so
Again, I’m not sure exactly why this happens, but it’s an easy fix. All we need to do is rename the file:
cd /usr/local/lib/python3.4/sitepackages/
sudo mv cv2.cpython34m.so cv2.so

Step #5: Testing your OpenCV 3 install

Congratulations, you now have OpenCV 3 installed on your Raspberry Pi 3 running Raspbian Jessie!

But before we pop the champagne and get drunk on our victory, let’s first verify that your OpenCV installation is working properly.

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx