ubuntu18 compile opencv4.1.1-in order to use opencv's cuda acceleration

There are a lot of online tutorials, I also refer to the online tutorials to compile successfully, now I send out the compilation process.
Purpose: Use the cuda acceleration function in opencv. E.g:

frame1_gray = cv.cuda_GpuMat(image1)
frame2_gray = cv.cuda_GpuMat(image2)
opticalFlowGPU = cv.cuda_FarnebackOpticalFlow.create(3,0.5,False,9,3,5,1.2,0)
flow = opticalFlowGPU.calc(frame1_gray, frame2_gray, None)
flow = flow.download()

There are other functions: Introduction to common methods of GPU version of opencv in python

Opencv compilation reference article
Compile and install OpenCV 4.1.1 Ubuntu 18.04 under Ubuntu 16.04 LTS source code compilation OpenCV 4.1.1

1. Installation environment dependency. Environment dependency is very important. I didn't succeed at first because the environment dependency was not installed properly.
Refer to the environment dependency of the first article:

sudo apt-get install build-essential

# OpenCV官网中安装的是旧版本的libgtk2.0-dev,这里我们安装新版本的libgtk-3-dev
sudo apt-get install git libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

# OpenCV官网中安装2.7版本的python-dev和python-numpy,这里我们安装python 3版本的python3-dev和python3-numpy
sudo apt-get install python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

This may not be comprehensive enough. You can read a few more articles and install all the dependencies you need.

2. Download the source code
Insert picture description here

3. cmake configuration When
configuring, you need to download some packages online. You can download this by yourself and change the download path. Opencv install ippcv under ubuntu cannot load problem
Insert picture description here

I started with cmake's gui interface configuration, following the selection in the first article.
Added WITH_CUDA on his basis, because I want to use cuda to accelerate. But after compiling, there is no cv2.cpython-36m-x86_64-linux-gnu.so file. (I didn't have this file when I first compiled it. At that time, it was because of the lack of python3-dev dependency.) Yesterday I used the method of the first article to compile and still did not have this file. This time I use the command line cmake.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/home/hyx/local/opencv4 \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.1/modules \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D PYTHON3_LIBRARY=/usr/local/anaconda3/lib/libpython3.6m.so \
    -D PYTHON3_INCLUDE_DIR=/usr/local/anaconda3/include/python3.6m \
    -D PYTHON3_EXECUTABLE=/usr/local/anaconda3/bin/python \
    -D PYTHON3_PACKAGES_PATH=/usr/local/anaconda3/lib/python3.6/site-packages \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D INSTALL_PYTHON_EXAMPLES=OFF\
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D BUILD_EXAMPLES=ON \
    -D WITH_CUDA=ON ..

Note that to modify the python environment
CMAKE_INSTALL_PREFIX I use is not the default /usr/local/, but my own path, this depends on personal needs. I made this change to facilitate the deletion of compilation errors.

4. Compile

make -j8
sudo make install

If you are not an administrator, su to root before the second step and proceedmake install

In this way, after compiling, you can see cv2.cpython-36m-x86_64-linux-gnu.so
if no error is reported after compiling, but there is no python3 folder in the lib, then it is not successful.
Insert picture description here
5. Because it is compiled on the server, the server itself has opencv-python.
All I am now running python under the cv2.cpython-36m-x86_64-linux-gnu.so folder. Used to verify the opencv version.
Insert picture description here
Because after my previous compilation was successful, every time I verified it was the opencv-python version installed by python, and then it succeeded in this folder. It may be necessary to soft link or move the so file to the python environment folder, but because I am a server, many people use it, so I add this path to sys every time I need to use cuda acceleration. path.
Insert picture description here
I put this folder and CMAKE_INSTALL_PREFIX=/home/hyx/local/opencv4folder in the public folder, other users can also use it. (I tried changing the /.bashrc file, and I will still use the opencv-python version)

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38469784/article/details/109022488