How to compile and install OpenCV under Linux
This article mainly introduces how to compile and install OpenCV under Linux. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
OpenCV is Intel's open source computer vision library. It consists of a series of C functions and a small number of C++ classes, and implements many general algorithms in image processing and computer vision. OpenCV has a cross-platform middle and high-level API that includes more than 300 C functions.
Prepare the environment
First install the following software packages:
GCC 4.4.x or laterCMake 2.8.7 or higherGitGTK+2.x or higher, including headers (libgtk2.0-dev) pkg-configPython 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 the 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
It only takes a little while to install all the packages you rely on for compilation (which is one of the reasons I like Linux, which is very convenient to install package). Then it starts to compile, which actually takes only three lines of command. But before that, you need to create a build folder in the unzipped opencv-XXX folder, and the compiled makefiles, project files, object files and output files will be placed in the build folder, and when you're done, you can start the official compilation.
Start installation
The first step is configuration.
$cmake-D CMAKE_BUILD_TYPE=Release-D CMAKE_INSTALL_PREFIX=/usr/local..
CMAKE_BUILD_TYPE: type of build. There are two types of CMAKE_INSTALL_PREFIX: Release and Debug: specify the folder directory where you want to install OpenCV, generally using / usr/local
In addition, you can add BUILD_DOCS to build documents and BUILD_EXAMPLES to build all the samples
Note: if the above command line does not work, remove the space after-D:
$cmake-DCMAKE_ BUILD_TYPE=Release-DCMAKE_INSTALL_PREFIX=/usr/local..
The second step is build. In order to speed up compilation, a multithreaded approach is generally used:
Make-J7 # uses seven threads at a time
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. To test whether your OpenCV can be used on Python, run a small piece of code, read a picture and display it:
Import cv2image = cv2.imread ("logo.png", 1) cv2.imshow ("Hello, world!" image) cv2.waitKey (0) cv2.destroyAllWindows ()
If the runtime reports an error, try changing the relative path of the picture to an absolute path.
The above is all the contents of the article "how to compile and install OpenCV under Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!