Home  >  Article  >  System Tutorial  >  How to compile and install OpenCV under Linux system

How to compile and install OpenCV under Linux system

王林
王林forward
2024-01-03 19:56:061057browse

OpenCV is a cross-platform computer vision library that can run on Windows, Linux, MacOS and other operating systems. OpenCV provides interfaces for many languages, including Python. Python is a language that is easy to get started and very pleasant to use. If you use Python to learn OpenCV, I believe you can get results faster.
The official download URL of OpenCV is http://opencv.org/releases.html. I chose the latest version 3.2.0. For Windows users, you can directly download the exe file and install it. The process is very simple, so I won’t go into details here. , if you encounter problems, you can read the official installation guide. For Linux users, you can download the OpenCV source code and compile it yourself, and download the source code compression package in zip format from the official website.
How to compile and install OpenCV under Linux system

Preparing the environment

First install the following software packages:

  • 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] means that this package is optional. The above packages can be installed directly through 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-dev

In just a short while, all the packages that the compilation depends on will be installed (this is one of the reasons why I like Linux, it is very convenient to install packages). Then the compilation starts. In fact, compilation only requires three lines of commands to complete. But before that, you need to create a build folder in the decompressed opencv-XXX folder. The compiled makefiles, project files, object files and output files will be placed in the build folder. After completion, you can start Officially compiled.

start installation The first step is configuration.
$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

CMAKE_BUILD_TYPE: The type of build, including Release and Debug
CMAKE_INSTALL_PREFIX: Specify the folder directory where you want to install OpenCV, generally use /usr/local

In addition, you can also add BUILD_DOCS to build documents and BUILD_EXAMPLES to build all examples

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

$ cmake -D CMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
The second step is to build. In order to speed up compilation, multi-threading is generally used:
make -j7 # 同时使用七个线程
The third step is installation.
sudo make install

If you haven’t encountered any problems so far, congratulations, you have successfully installed OpenCV on Linux. In order to test whether your OpenCV can be used on Python, you can run a small code to read an image and display it:

import cv2

image = cv2.imread("logo.png", 1)
cv2.imshow("Hello, world!", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

If an error occurs when running, try changing the relative path of the image to an absolute path. After running successfully, you will see:
How to compile and install OpenCV under Linux system

The above is the detailed content of How to compile and install OpenCV under Linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete