Ubuntu under opencv compile summary

  Recent frequent reloading Ubuntu system o (╥﹏╥) o, to reinstall Ubuntu opencv involved to be a summary.

Ubuntu install dependencies

  cuda, cudnn like are mounted can not say here, mainly related dependency installation instructions executed by a terminal, reference links:
  Ubuntu 18.04 compiled with python3.6 cuda9.2 GDAL opencv3.4.3
  the Ubuntu 18.04 source compiler installation process OpenCV 4.0

sudo apt install -y build-essential cmake git pkg-config libopenexr-dev libblas-dev 
sudo apt install -y python-dev python-numpy libtbb2 libtbb-dev
sudo apt install -y libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libjasper-dev
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libavutil-dev

sudo apt install -y \
        libx264-dev \
        mesa-utils \
        libgtk2.0-dev \
        libxvidcore-dev \
        yasm \
        libxine2-dev \
        libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
        libv4l-dev \
        libfaac-dev \
        libmp3lame-dev \
        libopencore-amrnb-dev \
        libtheora-dev \
        libvorbis-dev \
        ffmpeg \
        libeigen3-dev libeigen3-doc \
		liblapack-dev \
        tesseract-ocr \
        tesseract-ocr-jpn \
        libgflags-dev \
        libleptonica-dev \
        libtesseract-dev \
        gphoto2 \
        liblapacke-dev \
        libgoogle-glog-dev \
        libprotobuf-dev \
        libprotoc-dev \
        protobuf-compiler \
        ccache \
        libgphoto2-dev \
        libavresample-dev \
        libatlas-base-dev \
        gfortran

  

Download the installation package

  Download opencv Source: opencv the github link
  to download opencv_contrib Source: opencv_contrib the github link (you can not see the need)
  to note, if you want to compile contrib, need to contrib version number corresponds with opencv. Once downloaded unzip are placed to their destination.

  

Opencv compile a dynamic library (so)

  Reference links: openCV + opencv_contrib (compiled source code)
  in the opencv-extracting file, create a folder to save the compilation, such as build, and open a terminal in that folder, execute:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=OFF \
      -D INSTALL_PYTHON_EXAMPLES=OFF \
      -D WITH_TBB=ON \
      -D WITH_OPENGL=ON \
      -D BUILD_TIFF=ON -D WITH_TIFF=ON \
      -D WITH_1394=OFF \
      -D WITH_GDAL=ON \
      -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES"\
      -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules/ \
      -D OPENCV_GENERATE_PKGCONFIG=YES \
      -D BUILD_EXAMPLES=OFF ..

  The above is, for example, has yet to fully test the effectiveness of each command, subsequent updates.
  Wherein PYTHON_DEFAULT_EXECUTABLEwant to install python opencv path, generally, there are multiple versions of Ubuntu python;
  CMAKE_INSTALL_PREFIXis compiled placement so related documents, this is best to keep the set;
  OPENCV_EXTRA_MODULES_PATHpath opencv_contrib in the modules, if not ready to be compiled opencv_contrib delete this one. If you want to compile, then need to pay attention, in cuda10 time, cudacodec will not find the situation nvcuvid.h comes out, I will cudacodec module can be compiled normally deleted, the latest opencv_contrib at December 18, 2018 has been fixed the bug , if you are using the version after that time should compile normally.

  After generating the makefile, make operation to perform, if the data found in table generating generates an error or not, the need to remove the relevant folder, and then regenerate the modified instruction cmake:

# 查找该机器可使用的编译线程数
nproc
# 假如输出是8, 执行8线程编译
make -j8
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

  

Opencv compile a static library (a)

  Reference links: Under Ubuntu16.04 compiled OpenCV2.4.13 static library (.a file)
  Some projects require the use of opencv .a static library, dynamic library can generate part .a file, but not complete, so look for the next generation static library Methods.
  Creating a dynamic object with the same, create a build file folder opencv folder, such as static_release, and the folder is opened in the terminal, the command execution cmake:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D BUILD_SHARED_LIBS=NO \
      -D BUILD_PNG=ON \
      -D BUILD_JASPER=ON \
      -D BUILD_JPEG=ON \
      -D BUILD_TIFF=ON \
      -D BUILD_ZLIB=ON \
      -D WITH_JPEG=ON \
      -D WITH_PNG=ON \
      -D WITH_JASPER=ON \
      -D WITH_TIFF=ON ..

  Can follow the dynamic library cmake command, mainly to add BUILD_SHARED_LIBS=NO, on behalf of a dynamic library but does not compile compile static library can be compiled after the end of the / usr / local / lib / .a see a series of files generated.

  

test

  After the installation is complete can pkg-config opencv --modversionsee opencv successfully installed and version number.
  Test code can be modeled on Ubuntu 18.04 installed OpenCV 4.0 source code compilation process open camera / video, or like ubuntu 16.04 OpenCV3.2.0 completely compiled and installed open a picture.
  First create DisplayImage.cpp file, enter:

#include <cv.h>
#include <highgui.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
 
using namespace cv;
int main( int argc, char** argv )
{
    Mat image;
    image = imread( argv[1], 1 );
 
    if( argc != 2 || !image.data )
    {
        printf( "No image data \n" );
        return -1;
    }
 
    namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
    imshow( "Display Image", image );
    waitKey(0);
 
    return 0;
}

  Creating CMakeLists.txt file, enter:

project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

  Open a terminal in the folder, execute:

cmake .
make

  To generate a DisplayImage executable file, performing image ./DisplayImage test.jpg to open, the folder test.jpg placed next image, the image may be assigned to other paths.
  

Uninstall

cd /usr
sudo find . -name "*opencv*" | xargs sudo rm -rf

  

  

  

Published 24 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/yangjf91/article/details/89084484