OpenCV 4.4.0 C++ compilation under Ubuntu 18.04 qt5 cuda 10.2 cudnn 7.6.5

1. Install cuda 10.2 and cudnn 7.6.5

Note: opencv4.4.0 theoretically only supports cudnn7.6.5, and cudnn8.x cannot be compiled when cudnn is turned on. Please refer to this question

For installation, refer to my other article Ubuntu 18.04 and cuda deep learning environment installation

2. Install opencv4.4.0

Download opencv-4.4.0.zip
opencv_contrib-4.4.0.zip

unzip opencv-4.4.0.zip
unzip opencv_contrib-4.4.0.zip
cd opencv_contrib-4.4.0
cp ./opencv_contrib-4.4.0 ../opencv-4.4.0/
cd ..
cd opencv-4.4.0
mkdir build
cd build

Installation dependencies, refer to How to install OpenCV 4.2.0 with CUDA 10.0 in Ubuntu distro 18.04


sudo apt-get install qt5-default

$ sudo apt update
$ sudo apt upgrade

$ sudo apt install build-essential cmake pkg-config unzip yasm git checkinstall
$ sudo apt install libjpeg-dev libpng-dev libtiff-dev

$ sudo apt install libavcodec-dev libavformat-dev libswscale-dev libavresample-dev
$ sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
$ sudo apt install libxvidcore-dev x264 libx264-dev libfaac-dev libmp3lame-dev libtheora-dev 
$ sudo apt install libfaac-dev libmp3lame-dev libvorbis-dev

$ sudo apt install libopencore-amrnb-dev libopencore-amrwb-dev

$ sudo apt-get install libdc1394-22 libdc1394-22-dev libxine2-dev libv4l-dev v4l-utils
$ cd /usr/include/linux
$ sudo ln -s -f ../libv4l1-videodev.h videodev.h
$ cd ~

sudo apt-get install libgtk-3-dev

sudo apt-get install libtbb-dev

$ sudo apt-get install libatlas-base-dev gfortran

$ sudo apt-get install libprotobuf-dev protobuf-compiler
$ sudo apt-get install libgoogle-glog-dev libgflags-dev
$ sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

Hang proxy:

export http_proxy="http://127.0.0.1:12333"
export https_proxy="http://127.0.0.1:12333"

cmake

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-7 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D WITH_TBB=ON -D WITH_CUDA=ON -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=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=…/opencv_contrib-4.4.0/modules -D BUILD_EXAMPLES=ON -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_ARCH_BIN=6.1 -D CUDA_ARCH_PTX=6.1 -D PYTHON_EXECUTABLE=/home/xxx/miniconda3/envs/torch/bin/python …

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-7 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D WITH_TBB=ON -D WITH_CUDA=ON -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=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=../opencv_contrib-4.4.0/modules -D BUILD_EXAMPLES=ON -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_ARCH_BIN=6.1 -D CUDA_ARCH_PTX=6.1 -D PYTHON_EXECUTABLE=/home/xxx/miniconda3/envs/torch/bin/python ..

another cmake config on a rtx2060 notebook:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc-7 -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D WITH_TBB=ON -D WITH_CUDA=ON -D BUILD_opencv_cudacodec=OFF -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=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=../opencv_contrib-4.4.0/modules -D BUILD_EXAMPLES=ON -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_ARCH_BIN=7.5 -D CUDA_ARCH_PTX=7.5 -D PYTHON_EXECUTABLE=/usr/bin/python ..

GTX 1080Ti GPU Compute Capability 6.1
RTX 2060 7.5
GPU Compute Capability

make install

$ nproc
$ make -j8 #根据CPU线程数量或nproc结果定义
$ sudo make install

Solve the error while loading shared libraries: libopencv_core.so.4.4: cannot open shared object file: No such file or directory
may be encountered, the solution:

cd /etc/ld.so.conf.d/
sudo echo /usr/local/lib >> opencv.conf
sudo ldconfig -v

3. Test it

1) Test opencv to read jpeg and display

Test test.cpp, put an apple.jpeg picture in the same folder:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
    
    
    Mat src = imread("./apple.jpeg", IMREAD_COLOR);
    if (!src.data)
    {
    
    
        cerr << "Error : could not load image." << endl;
        return -1;
    }

    imshow("input", src);
    waitKey(0);

    return 0;
}

Compile and run:

g++ test.cpp -o test `pkg-config opencv --cflags --libs
./test

output result
insert image description here

2) Test cuda of opencv

Test cuda:

test2.cpp

#include <iostream>
#include <ctime>
#include <cmath>
#include "bits/time.h"

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>

#define TestCUDA true

int main() {
    
    
    std::clock_t begin = std::clock();

        try {
    
    
            cv::String filename = "./apple.jpeg";
            cv::Mat srcHost = cv::imread(filename, cv::IMREAD_GRAYSCALE);

            for(int i=0; i<1000; i++) {
    
    
                if(TestCUDA) {
    
    
                    cv::cuda::GpuMat dst, src;
                    src.upload(srcHost);

                    //cv::cuda::threshold(src,dst,128.0,255.0, CV_THRESH_BINARY);
                    cv::cuda::bilateralFilter(src,dst,3,1,1);

                    cv::Mat resultHost;
                    dst.download(resultHost);
                    
                } else {
    
    
                    cv::Mat dst;
                    cv::bilateralFilter(srcHost,dst,3,1,1);
                }
            }
        } catch(const cv::Exception& ex) {
    
    
            std::cout << "Error: " << ex.what() << std::endl;
        }

    std::clock_t end = std::clock();
    std::cout << double(end-begin) / CLOCKS_PER_SEC  << std::endl;
    }

Compile it:

g++ test2.cpp -o test `pkg-config opencv --cflags --libs
./test2

output:

(base) xxx@desktop:~/Desktop/test_opencv$ ./test2
0.528226

get it done!!!

Guess you like

Origin blog.csdn.net/catscanner/article/details/107716900