[Reproduced] Install OpenCV-Python in Ubuntu (3)

Similar to Fedora, the installation of OpenCV on Ubuntu is basically the same, so the installation method is basically the same. I will rewrite some of the differences. The following steps are in Ubuntu 16.04 and 18.04 (64-bit )experimental. One major change in Ubuntu 18.04 is that they completely abandoned Python 2.7. If needed, you can still install Python 2.7, but now Python 3 is the default setting on the operating system. So this tutorial is mainly aimed at Python 3.

Install Python 3.6

Let us get familiar with Python 3 on Ubuntu 18.04. To run Python 3 on Ubuntu 18.04, you must explicitly call python3. Let's first see which version is installed on the system:

	view plaincopy to clipboardprint?
 1. $ python3 --version   
 2. Python 3.6.5

Now, let's start a Python3 shell to test it out:

view plaincopy to clipboardprint?
 1. $ python3  
 2. >> print("OpenCV + Ubuntu 18.04!")   
 3. OpenCV + Ubuntu 18.04!  
 4. >> quit()

This is very simple, so we will continue to install OpenCV on Ubuntu 18.04.

Install OpenCV dependencies on Ubuntu 18.04:

All steps will be done in the terminal/command line. Before we start, open a terminal or connect via SSH. From there, we need to refresh/upgrade the pre-installed packages/libraries using the apt-get package manager:

	view plaincopy to clipboardprint?
 1. $ sudo apt-get update   
 2. $ sudo apt-get upgrade

Then install the developer tools:

	view plaincopy to clipboardprint?
 1. $ sudo apt-get install build-essential cmake unzip pkg-config

If you already have pkg-config installed on Ubuntu 18.04, you need to make sure to include it in the install command to ensure completeness.

Next, we need to install some prerequisites specific to OpenCV. OpenCV is an image processing/computer vision library, so it needs to be able to load standard image file formats, such as JPEG, PNG, TIFF, etc.:

view plaincopy to clipboardprint? 
 1. $ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev

Now let's try to install libjasper-dev:

	view plaincopy to clipboardprint?
 2. $ sudo apt-get install libjasper-dev

If you receive the error that libjasper-dev is missing, then follow the instructions below:

	view plaincopy to clipboardprint?
 3. sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu
    xenial-security main"   
 4. sudo apt update   
 5. sudo apt install libjasper1 libjasper-dev

Next, we install the video package, which requires the following packages so that the camera stream can be used and video files can be processed:

view plaincopy to clipboardprint?
 6. $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
    libv4l-dev   
 7. $ sudo apt-get install libxvidcore-dev libx264-dev

The highgui module of OpenCV relies on the GTK library for GUI operations. The highgui module can create a basic GUI to display images, handle taps/mouse clicks, and create sliders and track bars. Advanced GUI should be built using TK, Wx or QT, then install GTK:

	view plaincopy to clipboardprint?
 8. $ sudo apt-get install libgtk-3-dev

I recommend using the following two libraries to optimize various OpenCV functions:

	view plaincopy to clipboardprint?
 9. $ sudo apt-get install libatlas-base-dev gfortran

Finally, our last requirement is to install the Python 3 library:

	view plaincopy to clipboardprint?
 1. $ sudo apt-get install python3.6-dev

Download the official OpenCV source

Since we will continue to work in the terminal, use the following command to download the official OpenCV version:

	view plaincopy to clipboardprint?
 1. $ CD〜   
 2. $ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.4.zip

Next is the opencv_contrib module. This module will be very useful because it contains many of the most important algorithms of OpenCV. The contrib repository contains algorithms such as SIFT and SURF. In the past, these implementations have been included in the default installation of OpenCV 2.4. However, they have been migrated since OpenCV 3+. The contrib module contains modules under active development and/or patented modules (not free for commercial/industrial use). SIFT and SURF algorithms fall into this category. :

	view plaincopy to clipboardprint?
 1. $ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.4.zip

Important note: OpenCV and OpenCV_contrib versions must be the same.

Now, let's unzip the archive file:

view plaincopy to clipboardprint?
 1. $ unzip opencv.zip   
 2. $ unzip opencv_contrib.zip

Insert picture description here

Now, go ahead and rename the directory:

	view plaincopy to clipboardprint?
 1. $ mv opencv-3.4.4 opencv   
 2. $ mv opencv_contrib-3.4.4 opencv_contrib

Configure Python 3 environment

The first step we want to configure the Python 3 development environment is to install pip (Python package manager).

To install pip, just enter the following in the terminal:

view plaincopy to clipboardprint?
 1. $ wget https://bootstrap.pypa.io/get-pip.py   
 2. $ sudo python3 get-pip.py

Use virtual environment for Python development

I recommend using a virtual environment for development. Why?

Virtual environment can process projects in isolation without wasting resource consumption, such as VM and Docker image

For example, maybe there is a Python + OpenCV project that requires an older version of scikit-learn (v0.14), but if we want to continue to use the latest version of scikit-learn (0.19) for all newer projects.

Using the virtual environment, the two software version dependencies can be handled separately, but the system installation using only Python cannot be achieved.

Continue to install the virtual environment and virtual environment wrapper, now:

	view plaincopy to clipboardprint?
 1. $ sudo pip install virtualenv virtualenvwrapper   
 2. $ sudo rm -rf〜/get-pip.py〜/ .cache / pip

To complete the installation, we need to update our ~/.bashrc file.

Use a terminal text editor (such as vi file/vim file or Nano file) to add the following line to the ~/.bashrc file:

view plaincopy to clipboardprint?
 1. export WORKON_HOME=$HOME/.virtualenvs   
 2. exportVIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
 3. source/usr/local/bin/virtualenvwrapper.sh

Or, you can append the line directly through the bash command:

view plaincopy to clipboardprint?
 1. $ echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc   
 2. $ echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc   
 3. $ echo"export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> ~/.bashrc   
 4. $echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

Next, use the ~/.bashrc file as a source file:

	view plaincopy to clipboardprint?
 1. $ source ~/.bashrc

Create a virtual environment to save OpenCV and other software packages

We can now create a Python3 virtual environment for OpenCV:

	view plaincopy to clipboardprint?
 1. $ mkvirtualenv cv -p python3  

This line only creates a Python3 virtual environment called cv. We can name your environment whatever we want, and we can have any number of virtual environments on the system! Let us use the workon command to verify that we are in the cv environment:

	view plaincopy to clipboardprint?
 1. $ workon cv  

Install NumPy in the environment

Let's install the first package into the environment: NumPy, NumPy is necessary to use Python and OpenCV, we only need to use pip (when cv and Python virtual environments are active):

	view plaincopy to clipboardprint?
 1. $ pip install numpy

Configure and compile OpenCV for Ubuntu 18.04

Now that we are moving, we are ready to compile and install OpenCV. However, before we start, let's make sure we are in the cv virtual environment:

	view plaincopy to clipboardprint?
 1. $ workon cv

The virtual environment is active and very important, which is why I keep repeating this. If you are not in the cv and Python virtual environment before proceeding to the next step, the build file will not be generated correctly.

Use CMake to configure OpenCV

Let's use the cmake command to set our OpenCV version:

	view plaincopy to clipboardprint?
 1. $ cd ~/opencv   
 2. $ mkdir build   
 3. $ cd build   
 4. $ cmake 
 5. -D CMAKE_BUILD_TYPE=RELEASE \  
 6. -D CMAKE_INSTALL_PREFIX=/usr/local \  
 7. -D WITH_CUDA=OFF \  
 8. -D INSTALL_PYTHON_EXAMPLES=ON \  
 9. -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \  
 10. -D OPENCV_ENABLE_NONFREE=ON \  
 11. -D BUILD_EXAMPLES=ON ..

Note: If you encounter problems related to stdlib.h: there is no such file or directory in the cmake or make phase of this tutorial, you also need to include the following option in CMake: -D ENABLE_PRECOMPILED_HEADERS = OFF. In this case, I recommend deleting the build directory, recreating it, and then re-run cmake with the options above. This will resolve the stdlib.h error.

Compile OpenCV on Ubuntu 18.04

Let us compile OpenCV using the make command. Depending on the number of processors/cores, the compilation time can be shortened by changing the flags in the command. My computer has 4 cores, so I use the -j4 flag. You can update the number or turn off the flag completely:

	view plaincopy to clipboardprint?
 1. $ make -j4  

Insert picture description here

This process may take 30 minutes or more. If the compilation is blocked and hangs, it may be caused by thread occupation. In case you encounter this problem, just delete the build directory, recreate it, and then rerun cmake and make.

Install and verify OpenCV

After 100% compilation is successfully completed, OpenCV can now be installed:

view plaincopy to clipboardprint?
 1. $ sudo make install   
 2. $ sudo ldconfig

In order to verify the completion of the installation, enter the following command in the terminal:

view plaincopy to clipboardprint?
 1. $ pkg-config --modversion opencv  
 2. 3.4.4

Complete Python+OpenCV+Ubuntu 18.04 installation

We have reached the final juncture, please persevere.

At this time, the Python 3 bindings for OpenCV are located in the following folder:

	view plaincopy to clipboardprint?
 1. $ ls /usr/local/python/cv2/python-3.6  
 2. cv2.cpython-36m-x86_64-linux-gnu.so

Let's simply rename them to cv2.so:

view plaincopy to clipboardprint?
 1. $ cd /usr/local/python/cv2/python-3.6   
 2. $ sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so

Our last substep is to link our OpenCV cv2.so binding symbolic link to our cv virtual environment:

	view plaincopy to clipboardprint?
 1. $ cd ~/.virtualenvs/cv/lib/python3.6/site-packages/   
 2. $ ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so

Test OpenCV 3 installation on Ubuntu 18.04

To verify that our OpenCV+Ubuntu installation is complete, start Python, import OpenCV, and then query the version (if you have installed multiple versions of OpenCV at the same time, this will be very useful):

	view plaincopy to clipboardprint?
 1. $ cd ~   
 2. $ workon cv   
 3. $ python   
 4. Python 3.6.5   
 5. [GCC 7.3.0] on linux   
 6. Type "help", "copyright", "credits" or "license" for more information. 
 7. >>> import cv2  
 8. >>> cv2.__version__   '3.4.4'  
 9. >>> quit()

At this point, the installation is completely finished, and you can start a happy OpenCV journey.
Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/107905893