Install OpenCV + Python under Ubuntu
1 system configuration
(Host) operating system: Win1064 bit, Ubuntu virtual machine under VirtualBox .
Ubuntu version: 16.04.2
Python has been installed before, version: 3.5.2
1.1 Check the Ubuntu version
method one:
~$ uname-a
Method Two:
~$ cat/proc/version
Method three:
~$ lsb_release–a
1.2 View the installation path and version number of Python
method one:
~$whereis python
Method Two:
~$ whichpython
Method 3: Start Python and check the output information
Careful students should be able to find that there are different versions of Python installed on the current Ubuntu host , namely 2.7.12 and 3.5.2 . If you want to start different versions of Python , execute python2 and python3 (or python3. 5 ) Two commands are enough. For example:
~$python3
Start Python-3.5.2 , the prompt is as follows:
Through the detailed information of the list file, you can see the executable files linked by the two commands, as shown in the following figure:
2 Install OpenCV
2.1 Download the OpenCV installation package
Address: https://opencv.org/releases.html (Note: You can directly locate the source code download page of the second part through the link)
The interface is as follows:
I chose to install the latest version ( 3.4.0 ), and click the link address of the corresponding version (the red circle in the figure).
Enter a new page (address: https://github.com/opencv/opencv/releases/tag/3.4.0 ), the interface is as follows:
Choose to download the source code ( zip or tar.gz is acceptable), and I choose to download the tar.gz file, as shown in the red circle in the figure.
Click the link to download the file locally.
2.2 Unzip the file
Order:
~$ tar-zxvf
2.3 Dependency package installation
First perform the update operation:
~$ sudoapt-get update
The commands to check whether a package has been installed are:
~$ dpkg-s pkg_name
or
~$ dpkg--get-selections | grep pkg_name
According to the existing strategies on the Internet, the packages that need to be installed / confirmed are:
build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-devlibswscale-dev
Packages that have not been confirmed to be installed are:
cmake git libgtk2.0-dev libavcodec-dev libavformat-devlibswscale-dev
Execute the following command to perform the installation:
~$ sudoapt-get install cmake git libgtk2.0-dev libavcodec-dev libavformat-devlibswscale-dev
2.4 Compile and install OpenCV
Enter the decompressed file directory, and execute the following command:
~$ cd ~/opencv-3.4.0
~$ mkdir release
~$ cd release
~$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local..
~$ make
~$ sudo make install
The make command takes about 26 minutes! (It is estimated that it is due to the virtual machine)
So far, OpenCV can be used through c/c++ ( OpenCV 's implementation language) .
In order to use OpenCV in Python , some additional work is required ( after testing, it is confirmed that there is no need to perform the additional work mentioned in the existing network guides ), continue...
3 Test whether the installation is successful
3.1 Install OpenCV package for Python
As in Section 1.2 above, there are multiple Python versions installed on this machine , but after inspection, the three pip commands under /usr/local/bin (respectively pip, pip3 and pip3.5 ) are exactly the same (can be verified by file comparison), And both point to python3.5 . The first line of the three files is #!/usr/bin/python3 as shown below:
I suddenly recalled that during the installation process, in the step sudo make install , there was a message indicating what link has been created for python (including two versions of Python ), and I forgot to take a screenshot!
So it may not be necessary to install the python-opencv package mentioned in various strategies on the Internet .
So test it directly.
3.2 Installation test
Test one:
Start python , I start python3 here , and execute the code shown below, success! The interface and information are as follows:
Test two:
Type the following code in the current directory and save it as a file: test01.py
Another thing to do is to create an images directory in the current directory, and copy a picture ( test01.png ) to this directory. Of course, the storage path, format, and file name of the image file are optional, as long as they match the code That's it.
Execute the Python file, a pop-up window will display the specified picture, and you're done!