1、安装opencv依赖
安装时最好更换一下源。
sudo apt-get -y update
sudo apt-get -y install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get -y install libgtk-3-dev gfortran openexr libatlas-base-dev python3-dev python3-numpy
sudo apt-get -y install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev
sudo apt-get -y install zlib1g-dev libwebp-dev libtiff5-dev libopenexr-dev libgdal-dev libv4l-dev libxine2-dev
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 40976EAF437D05B5
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 3B4FE6ACC0B21F32
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get -y update
sudo apt-get -y install libjasper1 libjasper-dev libdc1394-dev
sudo apt-get -y install aptitude
sudo aptitude -y install libgtk-3-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgphoto2-dev
****切记先不要安装Anaconda,如果安装了,需要先把它的路径环境路径先注释掉!!!****
2、编译opencv库
2.1 CPU 库流程
准备opencv4.5.1代码
cd opencv4.5.1
mkdir build
cd build
//CPU 库编译
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_FFMPEG=OFF -DINSTALL_C_EXAMPLES=OFF -DINSTALL_PYTHON_EXAMPLES=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/soft/opencv451_cpu -DBUILD_opencv_world=ON -DBUILD_DOCS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DWITH_GSTREAMER=ON -DBUILD_TIFF=ON -DBUILD_TBB=ON -DWITH_EIGEN=ON -DWITH_OPENGL=ON -DBUILD_opencv_python_tests=OFF -DBUILD_opencv_python_bindings_generator=OFF ..
2.2 //GPU 库流程
2.2.1、下载cuda版本:cuda_11.8.0_520.61.05_linux.run
2.2.2、下载cudnn版本:cudnn-linux-x86_64-8.6.0.163_cuda11-archive.tar.xz
**安装cuda和cudnn,参考:https://blog.csdn.net/weixin_49777848/article/details/131684172\*\*
2.2.3、添加Cuda路径
gedit ~/.bashrc
export PATH=/usr/local/cuda-11.8/bin{PATH:+:{PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64{LD_LIBRARY_PATH:+:{LD_LIBRARY_PATH}}
source ~/.bashrc
2.2.4、添加cudnn路径
sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include
sudo cp cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
2.2.5//GPU库编译
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_GENERATE_PKGCONFIG=ON -DWITH_FFMPEG=OFF -DINSTALL_C_EXAMPLES=OFF -DINSTALL_PYTHON_EXAMPLES=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/soft/opencv451_cuda -DBUILD_opencv_world=ON -DBUILD_DOCS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DWITH_GSTREAMER=ON -DBUILD_TIFF=ON -DWITH_EIGEN=ON -DWITH_OPENGL=ON -DENABLE_FAST_MATH=ON -DWITH_CUDA=ON -DCUDA_FAST_MATH=ON -DCUDA_ARCH_BIN=7.5 -DWITH_CUBLAS=ON -DWITH_CUDNN=ON -DWITH_V4L=ON -DOPENCV_DNN_CUDA=ON -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib4.5.1/modules -DBUILD_opencv_python_tests=OFF -DBUILD_opencv_python_bindings_generator=OFF ..
make -j24
make install
2.3 可能错误:
错误一:
/sbin/ldconfig.real: /usr/local/cuda-11.8/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8 is not a symbolic link
解决:
cd /usr/local/cuda-11.8/targets/x86_64-linux/lib/
sudo ln -sf libcudnn_ops_infer.so.8.6.0 libcudnn_ops_infer.so.8.6
sudo ln -sf libcudnn_ops_infer.so.8.6 libcudnn_ops_infer.so.8
其他的一样的操作
3、例子调用(自己测试用,不方便公开)
3.1 把编译好的opencv lib和include库,在/soft/opencv451_cpu/ 和 /soft/opencv451_cuda/目录下,需要拷贝到测试用例对应的lib和include
如果用例在/home/VideoDiagnose下:
sudo cp -r /soft/opencv451_cpu/include/opencv4/opencv2 /home/VideoDiagnose/include
sudo cp /soft/opencv451_cpu/lib/libopencv_world* /home/VideoDiagnose/lib
cd /home/VideoDiagnose
sudo chmod -R 777 *
3.2 添加lib运行环境
sudo -i 先进入root模式
gedit /etc/ld.so.conf.d/VDXN.conf
添加,需要根据自己的实际路径添加:
/home/VideoDiagnose/lib
使其生效:
ldconfig
3.3 进入AlgTest目录下:
make
把生成的algTest拷贝到lib下,进入lib测试目录下,执行:
./algTest ./testData/9_对比度异常/
3.4 缺失的库在export_lib下
- opencv4.5.1代码下载地址:
链接: https://pan.baidu.com/s/1jZ-e3r__eZMXShy2XdLucA 提取码: 5zin