Linux(Ubuntu20.04) complie opencv with Qt, OpenGL, VTK,cuda

Linux(Ubuntu20.04) complie opencv with Qt, OpenGL, VTK,cuda

一、前言

先下载安装好Qt和vtk,默认已经安装好了cmake,g++,build-essential。
VTK的安装对cmake,qt都有版本要求,要注意一下。
cmake版本

cmake --version		//3.16.3

后面make之前查看下cpu核数

nproc  // 4
make -j4  // 这样应该会快一些

g++版本

g++ --version    //9.3.0

二、Qt下载安装

参考link
下载的版本也是5.9.0
清华镜像link
另外一个镜像link
定位到安装包文件夹,更改安装包权限,运行安装包

chmod +x  qt-opensource-linux-x64-5.9.0.run
./ qt-opensource-linux-x64-5.9.0.run	   //安装Qt

填写注册信息,安装组件没有选择sources,暂时没有影响。
通用字体配置库(不知道有啥用)

sudo apt-get install libfontconfig1

安装qtchooser并配置(也不知道有啥用)

sudo apt-get install qtchooser
qtchooser -install qt5.9.0 /home/根据自己路径填写/Qt5.9.0/5.9/gcc_64/bin/qmake
export QT_SELECT=qt5.9.0
qtchooser -l

安装OpenGL

sudo apt-get install mesa-common-dev
sudo apt-get install libglu1-mesa-dev -y

编译器配置不知道暂时有没有用,先参考链接配置吧

三、安装VTK

参考链接link
下载source,使用git下载的版本是9.1。

git clone --recursive https://gitlab.kitware.com/vtk/vtk.git

也是下载OpenGL,算是对上面的补充吧

sudo apt install mesa-common-dev mesa-utils freeglut3-dev ninja-build
// ninja is a speedy replacement for make, highly recommended.

官网用的是ccmake,直接在terminal中输入ccmake,可看到安装方式。
Build

mkdir -p vtk/build
cd vtk/build
ccmake ../path/to/vtk/source  // -GNinja may be added to use the Ninja generator

配置参考的是link

// 按照VTK tutorial要求,每设置完一项均按'c'进行一次configuration,直到所有项目设置完
// Enter键更改选项
BUILD_SHARED_LIBS = ON   
BUILD_TESTING = ON // 默认OFF,如果打开的话,编译时会由于下载测试数据所用url过旧而报错,建议OFF
CMAKE_BUILD_TYPE = Release    // 默认Debug运行会较慢
CMAKE_INSTALL_PREFIX = /usr/local   // 这里用默认就行,或者改到想要安装的位置
//以下为高级设置,需先在命令行按't'才可见,再次按't'可退出高级设置
VTK_FORBID_DOWNLOADS = ON    // 默认OFF,建议打开,否则编译会报错,理由同BUILD_TESTING

VTK_USE_CUDA=ON // 我自己改的

//此时应已经出现'g' generating 的按键选项,按 'g' 即完成配置.

安装

cmake .
make -j4
sudo make install

四、重新编译Opencv

亲测不用卸载OpenCV即可重新编译,没下载Opencv的也可以直接参考我的另外一篇安装Opencv,前面的配置都一样,只需要更改cmake选项。

 cd /...Path to OpenCV build.../build

更改cmake选项,下面是完整的。

sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=5.0 \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D Qt5_DIR=/home/gao/Qt5.9.0/5.9/gcc_64/lib/cmake/Qt5 \
-D WITH_OPENGL=ON \
-D WITH_VTK=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/gao/opencv_contrib/modules \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=ON \
-D GLIBCXX_USE_CXX11_ABI=0 ..

更改的部分

-D WITH_QT=ON \
-D Qt5_DIR=/home/gao/Qt5.9.0/5.9/gcc_64/lib/cmake/Qt5 \
-D WITH_OPENGL=ON \
-D WITH_VTK=ON \

Qt5_DIR这个不设置的话cmake会报错,

  Could not find a package configuration file provided by "Qt5" with any of
  the following names:

    Qt5Config.cmake
    qt5-config.cmake

  Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
  to a directory containing one of the above files.  If "Qt5" provides a
  separate development package or SDK, be sure it has been installed.

也可参考link,要将Qt5_DIR设置为Qt5Config.cmake所在的路径

For find_package to be successful, CMake must find the Qt installation in one of the following ways:

Set your CMAKE_PREFIX_PATH environment variable to the Qt 5 installation prefix. This is the recommended way.
Set the Qt5_DIR in the CMake cache to the location of the Qt5Config.cmake file.

安装

make -j4
sudo make install

环境配置参考前面博客。

五、输出general configuration for opencv

mkdir OpenCV_configuration
cd OpeCV_configuration

main.cpp

vim main.cpp
// 代码如下

#include 
#include 
#include 
#include 
using namespace std;

#include 

int main(void) {
  ofstream fout("OpenCV_general_configuration.txt");
  fout << cv::getBuildInformation() << endl;
  fout.close();
}

CMakeLists.txt

# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Define project name
project(Configuration)

# Requires OpenCV
find_package(OpenCV 4.5.0 REQUIRED)
# Show a message with the opencv version detected
MESSAGE("OpenCV version : ${OpenCV_VERSION}")
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Declare the executeable target built from your sources
ADD_EXECUTABLE(${PROJECT_NAME} main.cpp)

# Link your application with OpenCV libararies
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

compile

mkdir build
cd build
cmake ..
make
./Configuration

输出


General configuration for OpenCV 4.5.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /home/gao/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2021-11-06T12:27:20Z
    Host:                        Linux 5.4.0-56-generic x86_64
    CMake:                       3.16.3
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               RELEASE

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (17 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (7 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                /usr/bin/c++  (ver 9.3.0)
    C++ flags (Release):         -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -ffast-math -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
    Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed  
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          m pthread /usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so cudart_static dl rt nppc nppial nppicc nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/x86_64-linux-gnu
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab viz xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    cudacodec world
    Disabled by dependency:      -
    Unavailable:                 cnn_3dobj java js julia matlab ovis python2
    Applications:                tests perf_tests examples apps
    Documentation:               NO
    Non-free algorithms:         YES

  GUI: 
    QT:                          YES (ver ..)
      QT OpenGL support:         YES (Qt5::OpenGL ..)
    GTK+:                        NO
    OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
    VTK support:                 YES (ver 9.1.20211114)

  Media I/O: 
    ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
    JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
    WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x020e)
    PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
    TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
    JPEG 2000:                   build (ver 2.3.1)
    OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2_3)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      YES (2.2.5)
    FFMPEG:                      YES
      avcodec:                   YES (58.54.100)
      avformat:                  YES (58.29.100)
      avutil:                    YES (56.31.100)
      swscale:                   YES (5.5.100)
      avresample:                YES (4.0.0)
    GStreamer:                   YES (1.16.2)
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            TBB (ver 2020.1 interface 11101)

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   /mnt/document/opencv/opencv-4.5.0/build/3rdparty/ippicv/ippicv_lnx/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                /mnt/document/opencv/opencv-4.5.0/build/3rdparty/ippicv/ippicv_lnx/iw
    Lapack:                      NO
    Eigen:                       YES (ver 3.3.7)
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  NVIDIA CUDA:                   YES (ver 11.1, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch:             50
    NVIDIA PTX archs:

  cuDNN:                         YES (ver 8.0.4)

  OpenCL:                        YES (no extra features)
    Include path:                /mnt/document/opencv/opencv-4.5.0/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.8.10)
    Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
    numpy:                       /usr/local/lib/python3.8/dist-packages/numpy/core/include (ver 1.21.4)
    install path:                lib/python3.8/dist-packages/cv2/python-3.8

  Python (for build):            /usr/bin/python2.7

  Java:                          
    ant:                         NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /usr/local
-----------------------------------------------------------------

你可能感兴趣的:(qt,linux,c++)