linux/cmake源码编译cuda版本的opencv并在clion使用

此文章仍然是在python效率优化中遇到的需求,
这里只贴出编译脚本以及导入方式

1. 编译脚本

这个脚本主要修改自github上一个orin开发板的编译脚本

  1. 根据脚本贴出的下载地址,下载opencv以及opencv_contrib两个部分
  2. 解压到同一个文件夹:
    linux/cmake源码编译cuda版本的opencv并在clion使用_第1张图片
    linux/cmake源码编译cuda版本的opencv并在clion使用_第2张图片
  • 我的机器上有两个版本的源代码
  1. 修改OPENCV_VERSION,和OPENCV_DOWNLOAD_DIR为下载版本,以及解压位置
  2. ARCH_BIN是你的显卡的计算能力(什么compute capbality),需要去NVida官方查,或者编译执行安装cuda的example中的一个检测程序。CUDNN_VERSION就是你安装的cudnn版本,没什么程序能查,需要自己回忆下
  3. OPENCV_INSTALL_LOCATION是编译后的结果安装的位置
    linux/cmake源码编译cuda版本的opencv并在clion使用_第3张图片
  4. 题外话,这个脚本设定好python的基础信息,然后把注视的-DPythonxx放到cmake的后面,可以直接源码安装python版本的cv2,如果有需要可以自行修改
  5. sudo sh xxx.sh执行安装,注意opencv自己去github上下很多依赖,如果编译不成功,提示xxx缺失,一般就得自己去下载log中的资源
    linux/cmake源码编译cuda版本的opencv并在clion使用_第4张图片
#!/bin/bash

########## attention ###############
#1. change ARCH_BIN, CUDNN version
#2. change opencv unzip location(need tar -xf first)
#3. change python version(PYTHON_VERSION)
#4. change python env location(OPENCV_INSTALL_LOCATION)

#notify:/home/tacom/code/opencv/downloads/4_6_0
# 1. need comfirm python interpreter is not None, else stop once and delete opencv-version/build, and re-config the const
# 2. maybe you should change the link when GLIBCXX_xxx error is show
# 3. opencv: https://github.com/opencv/opencv/archive/refs/tags/4.5.4.tar.gz
# 4. opencv_contrib: https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.4.tar.gz
####################################


set -euf -o pipefail


OPENCV_VERSION=4.6.0  # OpenCV defines
ARCH_BIN=8.6  # Jetson AGX Orin is 8.7, other need looking from nvidia
CUDNN_VERSION=8.4
OPENCV_DOWNLOAD_DIR=/home/tacom/code/opencv/downloads/4_6_0
PYTHON_VERSION=3.7
OPENCV_INSTALL_LOCATION=/home/tacom/envs/opencv_4.6.0

# download
NUM_JOBS=8
OPENCV_DIR=opencv-$OPENCV_VERSION
OPENCV_CONTRIB_DIR=opencv_contrib-$OPENCV_VERSION
OPENCV_BUILD_DIR=$OPENCV_DIR/build
OPENCV_GITHUB_REPO=https://github.com/opencv
OPENCV_REPO=$OPENCV_GITHUB_REPO/opencv/archive/refs/tags/$OPENCV_VERSION.tar.gz
OPENCV_CONTRIB_REPO=$OPENCV_GITHUB_REPO/opencv_contrib/archive/refs/tags/$OPENCV_VERSION.tar.gz

# for python env or system global
PYTHON_DEFAULT_EXECUTABLE=$OPENCV_INSTALL_LOCATION/bin/python
PYTHON3_EXECCUTABLE=$OPENCV_INSTALL_LOCATION/bin/python
PYTHON3_INCLUDE_DIR=$OPENCV_INSTALL_LOCATION/include/python${PYTHON_VERSION}m
PYTHON3_PACKAGES_PATH=$OPENCV_INSTALL_LOCATION/lib/python${PYTHON_VERSION}/site-packages
PYTHON3_LIBRARY=$OPENCV_INSTALL_LOCATION/lib/libpython${PYTHON_VERSION}m.so

# Compile and Install OpenCV with CUDA
echo "Downloading and compiling OpenCV with CUDA support"

# for jetson orin env
#sudo apt install -y cmake libavcodec-dev libavformat-dev libavutil-dev libeigen3-dev libglew-dev libgtk2.0-dev libgtk-3-dev libjpeg-dev libpng-dev libpostproc-dev libswscale-dev libtbb-dev libtiff5-dev libv4l-dev libxvidcore-dev libx264-dev qt5-default zlib1g-dev python-dev python-numpy python-py python-pytest python3-dev python3-py python3-pytest libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev    

# normal pc
sudo apt install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev-is-python3 libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev


cd $OPENCV_DOWNLOAD_DIR

# Create the build directory and start cmake
if [ ! -d $OPENCV_DOWNLOAD_DIR/$OPENCV_BUILD_DIR ]
then
  mkdir $OPENCV_DOWNLOAD_DIR/$OPENCV_BUILD_DIR
fi

cd $OPENCV_DOWNLOAD_DIR/$OPENCV_BUILD_DIR


# -D PYTHON_DEFAULT_EXECUTABLE=${PYTHON_DEFAULT_EXECUTABLE} \
# -D PYTHON3_EXECUTABLE=${PYTHON3_EXECCUTABLE} \
# -D PYTHON3_INCLUDE_DIR=${PYTHON3_INCLUDE_DIR} \
# -D PYTHON3_PACKAGES_PATH=${PYTHON3_PACKAGES_PATH} \
# -D PYTHON3_LIBRARY=${PYTHON3_LIBRARY} \

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION} \
      -D WITH_CUDA=ON \
      -D WITH_CUDNN=ON \
      -D CUDNN_VERSION=${CUDNN_VERSION} \
      -D CUDA_ARCH_BIN=${ARCH_BIN} \
      -D CUDA_ARCH_PTX="" \
      -D ENABLE_FAST_MATH=ON \
      -D CUDA_FAST_MATH=ON \
      -D WITH_CUBLAS=ON \
      -D WITH_LIBV4L=ON \
      -D WITH_V4L=ON \
      -D WITH_GSTREAMER=ON \
      -D WITH_GSTREAMER_0_10=OFF \
      -D WITH_QT=OFF \
      -D WITH_GTK=ON \
      -D WITH_GTK_2_X=ON \
      -D WITH_OPENGL=ON \
      -D BUILD_opencv_python2=ON \
      -D BUILD_opencv_python3=ON \
      -D BUILD_TIFF=ON \
      -D BUILD_TESTS=OFF \
      -D BUILD_PERF_TESTS=OFF \
      -D OPENCV_GENERATE_PKGCONFIG=YES \
      -D OPENCV_EXTRA_MODULES_PATH=../../${OPENCV_CONTRIB_DIR}/modules \
      -D CPACK_PACKAGE_VERSION=${OPENCV_VERSION} \
      -D EXTRAMODULES_VCSVERSION=${OPENCV_VERSION} \
      -D OPENCV_VCSVERSION=${OPENCV_VERSION} \
      ../
      make -j${NUM_JOBS}
      make install
      ldconfig

echo "OpenCV installed in: $OPENCV_INSTALL_LOCATION"

2. CLion导入

  1. 由于不是安装在/usr目录中,需要自己指定Config.cmake查找路径,在自己的项目cmake,最好靠前面点的位置添加:
    • set(OpenCV_DIR /home/tacom/envs/opencv_4.6.0/lib/cmake/opencv4)
  2. find_package(OpenCV REQUIRED)
  3. 在find_package后面添加,就可以导入了
include_directories(
        ${OpenCV_INCLUDE_DIRS}
)  # headers
link_libraries(
        ${OpenCV_LIBS}
)  # libs

linux/cmake源码编译cuda版本的opencv并在clion使用_第5张图片

3. opencv运行环境

上面的是开发环境,需要配置运行环境,否则编译好的程序xxxlib.so找不到

  1. sudo gedit /etc/ld.so.conf
    linux/cmake源码编译cuda版本的opencv并在clion使用_第6张图片
    • 添加自己的lib路径到这个文件第一行下面
  2. sudo ldconfig
    • 别的方法就是配置.bashrc的LD啥的,效果都一样

4. Demo

我的代码就不给放了,这里贴出了相关示例的链接,看看是不是导入成功了

  • https://learnopencv.com/getting-started-opencv-cuda-module/

你可能感兴趣的:(opencv,linux,python)