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

How to compile and install OpenCV under Linux system

王林
Release: 2024-01-03 19:56:06
forward
1093 people have browsed it

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 
Copy after login

[required]

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
Copy after login

[optional]

$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-dev
Copy after login

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 ..
Copy after login

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 ..
Copy after login
The second step is to build. In order to speed up compilation, multi-threading is generally used:
make -j7 # 同时使用七个线程
Copy after login
The third step is installation.
sudo make install
Copy after login

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()
Copy after login

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!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template