源码编译安装OpenCV4.5+ CUDA11 带python 遇到的错误总结


写个笔记方便日后回查避坑


1. 安装

下载安装包

肯定是跟着官方的安装教程走:

# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip # 这里注意已经安装了cmake 3.12+的就吧cmake 去掉,它默认安装3.10的, yolov4兼容的不好。
# Download and unpack sources
wget -O opencv.zip https://hub.fastgit.org/opencv/opencv/archive/master.zip
wget -O opencv_contrib.zip https://hub.fastgit.org/opencv/opencv_contrib/archive/master.zip
unzip opencv.zip
unzip opencv_contrib.zip
# Create build directory and switch into it
mkdir -p build && cd build
# Configure
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules ../opencv-master
# Build
cmake --build .

运行到mkdir -p build && cd build 以后文件位置如下

.
|-- build
|-- opencv-master
|-- opencv.zip
|-- opencv_contrib-master
`-- opencv_contrib.zip

然后先别搞后面的,先安装各种依赖.


安装依赖

其实这里很多插件的依赖如果不用的话其实都没有必要安装的,但是我这里既然都搞了那就都安装吧.

  1. 依赖(当然不用的话可以不装,但是等你用的时候提示报错再装再编译那又是一堆事情了闹心,不如一次了事)
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install libgtk-3-dev libgtk2.0-dev pkg-config
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libopenexr-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install ffmpeg
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libpng12-dev
sudo apt-get install libtiff5-dev
sudo apt-get install libxine2-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
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
sudo apt install libopenblas-base libopenblas-dev
sudo apt-get install freeglut3-dev
sudo apt-get install libgtk3*


Cmake

然后进入build 文件夹输入, 我基本上全开了ON, 因为我安装opencv主要就是为了测试一些脚本, 不用于实际部署, 所以对大小没要求哈,然后就是省事了.

cmake -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr/local \
-DOPENCV_GENERATE_PKGCONFIG=ON \
-DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-master/modules \
-DBUILD_DOCS=ON \
-DBUILD_EXAMPLES=ON \
-DINSTALL_C_EXAMPLES=ON \
-DWITH_QT=ON \
-DWITH_GSTREAMER=ON \
-DVIDEOIO_PLUGIN_LIST=gstreamer \
-DOPENCV_ENABLE_NONFREE=ON \
-DBUILD_opencv_python2=ON \
-DBUILD_opencv_python3=ON \
-DWITH_LAPACK=ON \
-DWITH_EIGEN=ON \
-DWITH_OPENGL=ON \
-DWITH_CUDA=ON \
-WITH_CUFFT=ON \
-DWITH_CUBLAS=ON \
-DWITH_CUDNN=ON \
-WITH_NVCUVID=ON \
-DINSTALL_PYTHON_EXAMPLES=ON \
-DBUILD_EXAMPLES=ON \
-DPYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3\
-DPYTHON3_EXCUTABLE=/usr/bin/python3\
-DPYTHON3_INCLUDE_DIR=/usr/include/python3.6m\
-DPYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
-DPYTHON_NUMPY_PATH=/usr/local/lib/python3.6/dist-packages/numpy/core/include \
-DPYTHON3_PACKAGES_PATH=/usr/local/lib/python3.6/dist-packages ../opencv-master

之前是看了个很棒的博客找的那几个python解释器和库的地址,但是当时忘记收藏了找记录又找不到了,好贼难过啊, 不过大概还是可以从
https://docs.opencv.org/master/db/d05/tutorial_config_reference.html这里有详细解释只是真的没那个好ToT.

会看见一个配置单如下

--   GUI: 
--     QT:                          YES (ver 5.9.5)
--       QT OpenGL support:         YES (Qt5::OpenGL 5.9.5)
--     GTK+:                        NO
--     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGL.so /usr/lib/x86_64-linux-gnu/libGLU.so)
--     VTK support:                 NO
-- 
--   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:                        build (ver encoder: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     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_2)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                NO
--     GStreamer:                   YES (1.14.5)
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Lapack:                      YES (/usr/lib/x86_64-linux-gnu/liblapack.so /usr/lib/x86_64-linux-gnu/libcblas.so /usr/lib/x86_64-linux-gnu/libatlas.so)
--     Eigen:                       YES (ver 3.3.4)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   YES (ver 11.0, CUFFT CUBLAS)
--     NVIDIA GPU arch:             30 35 37 50 52 60 61 70 75 80
--     NVIDIA PTX archs:
-- 
--   cuDNN:                         YES (ver 8.0.0)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/opencv/opencv-master/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.17)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.17)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.9)
--     Libraries:                   NO
--     numpy:                       NO (Python3 wrappers can not be generated)
--     install path:                -
-- 
--   Python (for build):            /usr/bin/python3-DPYTHON3_EXCUTABLE=/usr/bin/python3-DPYTHON3_INCLUDE_DIR=/usr/include/python3.6m-DPYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/opencv/build

build

按照官网的样子去编译会花很长时间(1h),同时也会报两三个错,不过别慌再次编译就不会那么久了:

cmake --build .

报错1:test_dynafu.cpp:44:17: error: ‘Params’ is not a member of ‘cv::dynafu’

本人比较菜所以只能通过关闭rbgd相关功能来忽略这个问题
就是在上面的cmake 里多加一句

-DBUILD_opencv_rgbd=OFF

其实也不一定要全关闭rgbd功能, 其它大佬的解决方案可以参考下面我发的一条issue:
https://github.com/opencv/opencv_contrib/issues/2715
里面的解决办法我都试了一下, 都可以用, 唉,和大佬的差距太大了…


推荐直接把报错的Ptr<dynafu::Params> params; 改成 Ptr<kinfu::Params> params, 在哪报错改哪里, 有多少改多少,亲测有效,如果真非得要rgbd的话

更新2020.10.27


提的issue被官方resolve 和 merge 了, 那应该不会有再有这个错误了.

报错2: boostdesc_bgm.i: No such file or directory

把下面链接的文件复制粘贴到opencv_contrib/modules/xfeatures2d/src/路径下就可以解决
boostdesc_bgm.i等文件


报错3: test_features2d.cpp:51:10: fatal error: features2d/test/test_detectors_regression.impl.hpp: No such file or directory

在这里插入图片描述

直接 将 opencv-master/modules/features2d 这个文件夹复制到build 文件夹下就ok了, 这是最省力的做法,当然还有一种直观的做法就是在 报错的/home/opencv/opencv_contrib-master/modules/xfeatures2d/test/test_features2d.cpp 这个文件里把#include "opencv-master/modules/features2d/test/test_detectors_regression.impl.hpp " 路径改成绝对路径 ,我记得貌似得改好几个


报错4: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory

#include “opencv2/xfeatures2d/cuda.hpp”
在这里插入图片描述
讲道理所有的这种头文件找不到的都可以直接在报错的文件的上直接把这个头文件的地址改成它的绝对路径就绝壁没问题了,就像下面这样, 路径对应的是opencv_contrib里的文件.
在这里插入图片描述

报错5:CMakeFiles/example_gpu_surf_keypoint_matcher.dir/surf_keypoint_matcher.cpp.o: In function `main’:

surf_keypoint_matcher.cpp:(.text.startup.main+0x352): undefined reference to `cv::cuda::SURF_CUDA::SURF_CUDA()’

别慌, 完整的报错是这样的:

CMakeFiles/example_gpu_surf_keypoint_matcher.dir/surf_keypoint_matcher.cpp.o: In function `main':
surf_keypoint_matcher.cpp:(.text.startup.main+0x352): undefined reference to `cv::cuda::SURF_CUDA::SURF_CUDA()'
surf_keypoint_matcher.cpp:(.text.startup.main+0x579): undefined reference to `cv::cuda::SURF_CUDA::operator()(cv::cuda::GpuMat const&, cv::cuda::GpuMat const&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, bool)'
surf_keypoint_matcher.cpp:(.text.startup.main+0x60d): undefined reference to `cv::cuda::SURF_CUDA::operator()(cv::cuda::GpuMat const&, cv::cuda::GpuMat const&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, bool)'
surf_keypoint_matcher.cpp:(.text.startup.main+0x6af): undefined reference to `cv::cuda::SURF_CUDA::defaultNorm() const'
surf_keypoint_matcher.cpp:(.text.startup.main+0x7ca): undefined reference to `cv::cuda::SURF_CUDA::downloadKeypoints(cv::cuda::GpuMat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&)'
surf_keypoint_matcher.cpp:(.text.startup.main+0x7ea): undefined reference to `cv::cuda::SURF_CUDA::downloadKeypoints(cv::cuda::GpuMat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&)'
surf_keypoint_matcher.cpp:(.text.startup.main+0x800): undefined reference to `cv::cuda::SURF_CUDA::downloadDescriptors(cv::cuda::GpuMat const&, std::vector<float, std::allocator<float> >&)'
surf_keypoint_matcher.cpp:(.text.startup.main+0x812): undefined reference to `cv::cuda::SURF_CUDA::downloadDescriptors(cv::cuda::GpuMat const&, std::vector<float, std::allocator<float> >&)'
collect2: error: ld returned 1 exit status
samples/gpu/CMakeFiles/example_gpu_surf_keypoint_matcher.dir/build.make:132: recipe for target 'bin/example_gpu_surf_keypoint_matcher' failed
make[2]: *** [bin/example_gpu_surf_keypoint_matcher] Error 1

在这里插入图片描述
别慌别慌, 有的解的, 或者这样的:

/home/opencv/build/modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/surf.cuda.cpp.o: In function `cv::cuda::SURF_CUDA::operator()(cv::cuda::GpuMat const&, cv::cuda::GpuMat const&, cv::cuda::GpuMat&, cv::cuda::GpuMat&, bool)':
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x272): undefined reference to `cv::cuda::device::surf::loadGlobalConstants(int, int, int, int, int, float)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x2b9): undefined reference to `cv::cuda::device::surf::bindImgTex(cv::cuda::PtrStepSz<unsigned char>)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x36d): undefined reference to `cv::cuda::device::surf::bindSumTex(cv::cuda::PtrStepSz<unsigned int>)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x448): undefined reference to `cv::cuda::device::surf::compute_descriptors_gpu(cv::cuda::PtrStepSz<float4>, float const*, float const*, float const*, float const*, int)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x675): undefined reference to `cv::cuda::device::surf::bindMaskSumTex(cv::cuda::PtrStepSz<unsigned int>)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x849): undefined reference to `cv::cuda::device::surf::loadOctaveConstants(int, int, int)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x8ae): undefined reference to `cv::cuda::device::surf::icvCalcLayerDetAndTrace_gpu(cv::cuda::PtrStep<float> const&, cv::cuda::PtrStep<float> const&, int, int, int, int)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x933): undefined reference to `cv::cuda::device::surf::icvFindMaximaInLayer_gpu(cv::cuda::PtrStep<float> const&, cv::cuda::PtrStep<float> const&, int4*, unsigned int*, int, int, int, bool, int)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0x9e9): undefined reference to `cv::cuda::device::surf::icvInterpolateKeypoint_gpu(cv::cuda::PtrStep<float> const&, int4 const*, unsigned int, float*, float*, int*, int*, float*, float*, unsigned int*)'
surf.cuda.cpp:(.text._ZN2cv4cuda9SURF_CUDAclERKNS0_6GpuMatES4_RS2_S5_b+0xae9): undefined reference to `cv::cuda::device::surf::icvCalcOrientation_gpu(float const*, float const*, float const*, float*, int)'

解决方法都是把(觉得迷幻也没关系, 别慌啊, 下面有更详细的前人大佬的讨论链接)

/home/opencv/build/samples/gpu/CMakeFiles/example_gpu_surf_keypoint_matcher.dir/link.txt 

这个文件里在这个地方改成这样子

--as-needed CMakeFiles/example_gpu_surf_keypoint_matcher.dir/surf_keypoint_matcher.cpp.o ../../modules/xfeatures2d/CMakeFiles/opencv_xfeatures2d.dir/src/surf.cuda.cpp.o ../../modules/xfeatures2d/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_surf.cu.o

大佬解决方案的具体链接在这里:
https://github.com/opencv/opencv/issues/9949

虽然有点迷幻, 但是真的问题不大,复制粘贴覆盖掉就好了.

最后再次编译就可以编译完成了
在这里插入图片描述
以前也搞过但是可能这次依赖比较多的缘故坑真的很多都踩了一遍, 那就开始安装了.


install 安装

在build 文件夹下

sudo make install 

开始安装, 安装完后检查一下可以不可以搞

python3
>>> import cv2
>>> print(cv2.__version__)

看到
在这里插入图片描述
那么基本安装成功了.


如果找不到cv2 包

sudo apt-get install libopencv-dev

目测问题不大.



2. 结语

真的没有大佬们post出来的解决方案让我搜出来, 像我这样的小白真的是寸步难行.






下面的不用看了,以前的东西就留个记录


boostdec_bgm.i: No sub file or directory
在这里插入图片描述

features2d/test/test_detectors_regression.impl.hpp: No such file or directory

在这里插入图片描述

//#include "features2d/test/test_detectors_regression.impl.hpp"
//#include "features2d/test/test_descriptors_regression.impl.hpp"
#include "/home/opencv_install_pkg/opencv/modules/features2d/test/test_detectors_regression.impl.hpp"
#include "/home/opencv_install_pkg/opencv/modules/features2d/test/test_descriptors_regression.impl.hpp"

在这里插入图片描述

test_rotation_and_scale_invariance.cpp:7:10: fatal error: features2d/test/test_detectors_invariance.impl.hpp: No such file or directory

在这里插入图片描述

参考文献

https://my.oschina.net/u/4391831/blog/3312094
官方教程
https://blog.csdn.net/gbz3300255/article/details/90240912
https://blog.csdn.net/qq_33475105/article/details/82819850

  • 5
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
在Win10 VS2019上编译安装OpenCV-7.0 CUDA可以按照以下步骤进行: 1. 首先,下载并安装CUDA Toolkit。可以从NVIDIA官方网站上下载最新版本的CUDA Toolkit,并按照它的安装向导进行安装。 2. 下载OpenCV源代码。可以从OpenCV官方网站上下载最新版本的OpenCV源代码,并将其解压到一个合适的文件夹中。 3. 打开Visual Studio 2019,并在创建新项目时选择“空项目”。 4. 在解决方案资源管理器中,右键单击项目,并选择“属性”。 5. 在属性对话框中,选择“VC++目录”,然后选择“包含目录”。将OpenCV源代码中的“include”文件夹路径添加到包含目录中。 6. 再次在属性对话框中,选择“链接器”,然后选择“常规”。将OpenCV源代码中的“lib”文件夹路径添加到库目录中。 7. 然后,在链接器中选择“输入”。将以下库文件添加到附加依赖项中:opencv_corexxx.lib、opencv_highguixxx.lib、opencv_imgprocxxx.lib(其中“xxx”表示版本号)。 8. 配置好属性后,按下F7键进行编译,并等待编译完成。 9. 编译完成后,在OpenCV源代码的构建文件夹中找到“bin”文件夹,将其中的所有DLL文件复制到系统的PATH环境变量中。 10. 最后,在项目中使用OpenCV-7.0 CUDA库进行开发。可以通过引入相关头文件和使用OpenCV的相关函数来进行开发。 通过以上步骤,您可以在Win10 VS2019上成功编译安装OpenCV-7.0 CUDA,并在项目中使用相关功能进行开发。希望对您有所帮助!
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值