Compile and install OpenCV with cmake on Ubuntu

This article adopts the environment: Ubuntu14.04 + OpenCV-3.4.0

Compatible with OpenCV-2.4.xxx OpenCV-3.xx



1. Source code preparation: opencv, cmake

1. Opencv official website https://opencv.org/ Download source package: opencv-3.4.0.tar.gz

2. Cmake official website https://cmake.org/ Download source package: cmake-3.10.2.tar.gz


2. Install cmake

1. Unzip the source code and enter the unzipped directory

    $ tar -zxvf cmake-3.10.2.tar.gz

    $ cd cmake-3.10.2


2. Read the README.rst document and follow the instructions:


Interpretation: First, you must have a C++ compiler, and you must support the C++11 standard (the g++ version is too low, it will cause errors, please upgrade yourself); then run the $ ./bootstrap script to configure, you can use some items such as "--help ", "--prefix" (installation directory); After bootstrap is successful, execute $ make $ make install.

In short, just three commands:

$ ./bootstrap    

$ make    

$ sudo make install

After the installation is complete, check: 

$ cmake --version


Get it, it's that simple.


PS: It is said that there is a simpler and cruder method:

$ sudo apt-get install cmake  


3. Install the dependency library

$ sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg-dev libtiff4-dev libswscale-dev libjasper-dev


Fourth, install Opencv

1. Unzip the source code and enter the unzipped directory

    $ tar -zxvf opencv-3.4.0.tar.gz

    $ cd opencv-3.4.0


2. Create a compilation folder build and enter build

    $ mkdir build

    $cd build


3. Execute the following command, CMAKE_INSTALL_PREFIX=path, "..": CMakeLists.txt path (in decompressed opencv)

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


In this step, something went wrong in version 3.2.0, and the following error will occur:

-- ICV: Downloading ippicv_linux_20151201.tgz...
CMake Error at 3rdparty/ippicv/downloader.cmake:73 (file):
  file DOWNLOAD HASH mismatch

    for file: [/root/library/opencv/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz]
      expected hash: [808b791a6eac9ed78d32a7666804320e]
        actual hash: [d41d8cd98f00b204e9800998ecf8427e]
             status: [1;"Unsupported protocol"]

Call Stack (most recent call first):
  3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
  cmake/OpenCVFindIPP.cmake:243 (include)
  cmake/OpenCVFindLibsPerf.cmake:37 (include)
  CMakeLists.txt:558 (include)


CMake Error at 3rdparty/ippicv/downloader.cmake:77 (message):
  ICV: Failed to download ICV package: ippicv_linux_20151201.tgz.
  Status=1;"Unsupported protocol"
Call Stack (most recent call first):
  3rdparty/ippicv/downloader.cmake:110 (_icv_downloader)
  cmake/OpenCVFindIPP.cmake:243 (include)
  cmake/OpenCVFindLibsPerf.cmake:37 (include)
  CMakeLists.txt:558 (include)


-- Configuring incomplete, errors occurred!
See also "/root/library/opencv/opencv-3.2.0/build/CMakeFiles/CMakeOutput.log".
See also "/root/library/opencv/opencv-3.2.0/build/CMakeFiles/CMakeError.log".


Baidu checked it: it needs to be downloaded during the installation of opencv . Due to network reasons, this file often fails to download.ippicv_linux_20151201.tgz

Solution: 

    1. Manually download  ippicv_linux_20151201.tgz  ; you can Baidu, a lot.

    2. Create opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ directory

   Don't copy! ! !  The directory is selected and created according to the output error information, as above: (so many lines of error information are deliberately posted, just to illustrate this point)

for file: [/root/library/opencv/opencv-3.2.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/ippicv_linux_20151201.tgz]

    3. Put the downloaded file in this directory;

    4. Re-execute the cmake command.


4. Compile   (unknown error: Ubuntu1204 error, 1404 is normal; guess it may be the version of the dependent library)

    $ make


5. Installation

        $ make install


6. Configure the environment variables of opencv

    $ vi /etc/ld.so.conf.d/opencv.conf       (file or empty, newly created)

    Add: /usr/ local /lib

    ldconfig         //make it effective


7. Configure bash

    $ vi /etc/bash.bashrc

    Add the following two lines at the end of the text:

    PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

    export PKG_CONFIG_PATH

    $ source /etc/bash.bashrc     //make it effective


5. Test

Use the built-in test case, under samples/cpp/example_cmake

If you are interested, you can read how CMakeLists.txt is written under example_cmake, and there are a lot of comments in it.

    $ cmake .

    $ make

    $ ./opencv_example


1. No camera environment:

Built with OpenCV 3.4.0
No capture



2. With camera environment:

Built with OpenCV 3.4.0
Capture is opened
select timeout
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != __null) in Mat, file /root/library/opencv/opencv-3.4.0/modules/core/include/opencv2/core/mat.inl.hpp, line 500
terminate called after throwing an instance of 'cv::Exception'
  what():  /root/library/opencv/opencv-3.4.0/modules/core/include/opencv2/core/mat.inl.hpp:500: error: (-215) total() == 0 || data != __null in function Mat

Abandoned (core dumped)


Error in operation: Timeout to open camera! ! !

Haven't found the reason yet, put it aside for now.

This problem has been solved, see [ Virtual machine Ubuntu fails to open the camera and OpenCV opens the camera overtime ]! ! !

The effect after solving the problem:



Afterword:

Opencv is installed, the next part will start to write the first opencv project.


Remarks: Refer to the blog  http://blog.csdn.net/cocoaqin/article/details/78163171



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388539&siteId=291194637