For detailed info, please refer to https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
Step #1: Install OpenCV dependencies on Ubuntu 16.04
Most (in fact, all) steps in this tutorial will be accomplished by using your terminal. To start, open up your command line and update the apt–get package manager to refresh and upgrade and pre-installed packages/libraries:
Step #2: Download the OpenCV source
At the time of this article’s publication, the most recent version of OpenCV is 3.1.0 , which we download a .zip of and unarchive using the following commands:
Step #3: Setup your Python environment — Python 2.7 or Python 3
We are now ready to start configuring our Python development environment for the build. The first step is to install pip , a Python package manager:
In short, using these packages allows you to solve the “Project X depends on version 1.x, but Project Y needs 4.x dilemma. A fantastic side effect of using Python virtual environments is that you can keep your system Python neat, tidy, and free from clutter.
While you can certainly install OpenCV with Python bindings without Python virtual environments, I highly recommend you use them as other PyImageSearch tutorials leverage Python virtual environments. I’ll also be assuming that you have both virtualenv and virtualenvwrapper installed throughout the remainder of this guide.
# virtualenv and virtualenvwrapper
Creating your Python virtual environment
If you decide to use Python 2.7, use the following command to create a Python 2.7 virtual environment:
Step #4: Configuring and compiling OpenCV on Ubuntu 16.04
tar -xzvf opencv-3.3.0.tar.gz cd opencv-3.3.0/
cd .. mkdir build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3 ..
make # make -j4表示开4个线程来进行编译
sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
Step #6: Testing your OpenCV install
Congratulations, you now have OpenCV 3 installed on your Ubuntu 16.04 system!
To verify that your installation is working:
- Open up a new terminal.
- Execute the workon command to access the cv Python virtual environment.
- Attempt to import the Python + OpenCV bindings.
I have demonstrated how to perform these steps below:
cmake -D BUILD_opencv_python3=YES -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/opencv3 （自定义安装目录) -D OPENCV_EXTRA_MODULES=../../opencv_contrib/modules -D PYTHON3_LIBRARIES=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/chienshu/.local/lib/python3.5/site-packages/numpy/core/include/ ..各个 = 之间不能有空格