How To Install OpenCV on Raspberry Pi
OpenCV is mainly written in C++ language, but also provides Python and other languages, the following describes how to install OpenCV and OpenCV on the Raspberry Pi Python call library.
OpenCV full name: Open Source Computer Vision Library, is an open source cross-platform computer vision library, OpenCV is mainly written in C++ language, but also provides Python and other languages, the following describes how to install OpenCV and OpenCV on the Raspberry Pi Python call library.
Update system and install dependencies:
Update Raspberry Pi:
sudo apt-get update sudo apt-get upgrade
Install basic dependencies such as compilation:
sudo apt-get install build-essential cmake pkg-config
Install image-related libraries:
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
Install the basic IO library:
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev sudo apt-get install libxvidcore-dev libx264-dev
Install highgui-related dependent libraries:
sudo apt-get install libgtk2.0-dev
Install opencv advanced dependency library, operation matrix, etc.:
sudo apt-get install libatlas-base-dev gfortran
Configure python virtual environment
Download OpenCV 3.1.0 and OpenCV_contrib library
cd ~ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip unzip opencv.zip wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip unzip opencv_contrib.zip
Compile and install:
Enter the opencv directory:
cd opencv-3.1.0/ mkdir build cd build
cmake -D CMAKE_BUILD_TYPE = RELEASE \ -D CMAKE_INSTALL_PREFIX = /usr/local \ -D INSTALL_PYTHON_EXAMPLES = ON \ -D OPENCV_EXTRA_MODULES_PATH = ~/opencv_contrib-3.1.0/modules \ -D BUILD.EXAMPLES = ON
The 4 here is for the quad-core on the Raspberry Pi, that is, it uses all the computing resources of the Raspberry Pi to compile.
If the compilation fails, you can execute the following command to retry the compilation:
make clean make
Note that the above command is executed only when the compilation fails.
It takes about 1 hour to compile, wait patiently, you can open the terminal to play with other things when compiling.
sudo make install
Add the dynamic link library generated by OpenCV to the Raspberry Pi directory:
Now that it
OpenCV 3.0 has been successfully installed on your machine, the next step is to
cv2.so link to our virtual environment:
cd /home/pi/codes/env/cv/lib/python2.7/site-packages ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
Note that the path of the above virtual environment needs to be modified to your own. Next, you can verify whether opencv is installed successfully, and execute it in the terminal:
python -c "import cv2;print(cv2.__version__)"
If the output is similar to the following figure, the installation is successful.