OpenCV learning in Python 01 -- install OpenCV and pythonopencv in Linux

Source: Internet
Author: User
Tags cuda toolkit

OpenCV learning in Python 01 -- install OpenCV and pythonopencv in Linux

1. OpenCV Overview

OpenCV is a cross-platform computer vision library that runs on Windows, Linux, MacOS, and other operating systems. OpenCV provides interfaces in many languages, including Python. Python is an easy-to-use and easy-to-use language. Using Python to learn OpenCV, I believe it will produce good results.


2. Download the source code or installation package from the official website

The official download URL of OpenCV is


Iii. Preparations before compilation and Installation

For Windows users, you can directly download the exe file for installation. The installation process is very simple and I will not talk about it here. If you have any problems, please refer to the official installation guide.

For Linux users, you can download the source code and compile it yourself. Download the zip source code package on the official website. install several packages before compiling:

  • GCC 4.4.x or later
  • CMake 2.8.7 or higher
  • Git
  • GTK + 2.x or higher, including headers (libgtk2.0-dev)
  • Pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • Ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [Optional] libtbb2 libtbb-dev
  • [Optional] libdc1394 2.x
  • [Optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
  • [Optional] CUDA Toolkit 6.5 or higher

[Optional] indicates that this package is optional. The above packages can be directly installed using the apt-get command. Open the terminal and enter the following command:

[compiler] $ sudo apt-get install build-essential [required] $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev[optional] $ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

In a short time, all the packages on which the compilation depends are installed. (This is one of the reasons why I like Linux. package installation is very convenient ). The next step is to start compilation. In fact, only three lines of commands are required for compilation. However, a build folder should be created in the decompressed opencv-XXX folder. The makefiles, project files, object files, and output files generated by compilation will be placed in the build folder. After compilation, you can start the official compilation.


Iv. Compilation and Installation

1. configuration.

$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

CMAKE_BUILD_TYPE: build type, which can be Release or Debug.

CMAKE_INSTALL_PREFIX: Specifies the folder directory for which OpenCV is to be installed. Generally,/usr/local is used.

In addition, you can add BUILD_DOCS to build the document and BUILD_EXAMPLES to build all the samples.

Note: If the preceding command line cannot work, remove the space after-D:

$ cmake -D CMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..


2. compile.

To speed up compilation, multithreading is generally used:

Make-j7 # use seven threads at the same time


3. install.

sudo make install

If you haven't encountered any problems yet, congratulations, you have successfully installed OpenCV on Linux.


5. Test Your OpenCV in Python

To test whether your OpenCV can be used in Python, you can run a small piece of code, read an image and display it:

Import cv2 # introduce OpenCV module image = cv2.imread ("logo.png", 1) # Read an image and store it in image cv2.imshow ("Hello, world! ", Image) # create a file named" Hello, world! "Window, the image is displayed in the window cv2.waitKey (0) # Wait for the user to press any key cv2.destroyAllWindows () # destroy all windows

If an error is reported during running, change the relative path of the image to the absolute path. After running successfully, you will see the picture you selected. My picture is:




References: OpenCV: Installation in Linux



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.