近期为了做三维重建。开始了OpenCV学习之路。顺便记录一下。
如题所述,本次是在Ubuntu下配置的。所以用到的各种软件如下所示:
将cmake下载并解压到任意文件夹下,然后建立软连接。我是放在~/opt
下
sudo ln -sf /opt/cmake-3.19.2/bin/* /usr/bin/
这里不再赘述。下载安装即可。
将4.5对应的俩个压缩包下载下来。分别更改为opencv
、opencv_contrib
,然后将opencv_contrib
放入到opencv
目录下,再将opencv放到你想安装的地方。我放在D:\opencv
.进入该目录,新建一个build文件夹用于后续编译。
安装完cmake后在终端里输入cmake-gui
即可看到CMake(cmake_gui)程序。
Where is the source code 选择安装opencv的目录,我的是/home/pc/opencv
。Where to build the binaries 选择build文件夹我的是/home/pc/opencv/build
然后点击 configure 按钮来配置,在弹出的页面按下图选择后点 Finish。
耐心等待进度条读完,直到输出窗口看到 Configuring done
然后中间就会弹出一系列的标红的配置变量。在这需要重点关注几个变量:
opencv_contrib
的目录即/home/pc/opencv/opencv_contrib/modules
;打开终端,cd ~/opencv/build
输入make -j8
.j后面数值取决于电脑的cpu核心数。该步骤耗时较长,且有可能会出现以下几个问题:
D:/opencv/modules/stitching/include/opencv2/stitching/detail/matchers.hpp:52:12: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory
# include "opencv2/xfeatures2d/cuda.hpp"
进入对应的文件里将头文件的相对路径改为绝对路径,以下同理;D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d\cuda.hpp
D:/opencv/modules/stitching/src/precomp.hpp:91:12: fatal error: opencv2/xfeatures2d/cuda.hpp: No such file or directory
# include "opencv2/xfeatures2d/cuda.hpp"
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d\cuda.hpp
D:\opencv\modules\stitching\src\matchers.cpp:52:10: fatal error: opencv2/xfeatures2d.hpp: No such file or directory
#include "opencv2/xfeatures2d.hpp"
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d.hpp
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2/xfeatures2d.hpp:43:10: fatal error: opencv2/xfeatures2d/nonfree.hpp: No such file or directory
#include "opencv2/xfeatures2d/nonfree.hpp"
D:\opencv\opencv_contrib\modules\xfeatures2d\include\opencv2\xfeatures2d\nonfree.hpp
D:\opencv\opencv_contrib\modules\xfeatures2d\src\boostdesc.cpp:653:20: fatal error: boostdesc_bgm.i: No such file or directory
#include "boostdesc_bgm.i"
下载该压缩包(提取码:3y78),解压后将里面的文件放入/home/pc/opencv/opencv_contrib/modules/xfeatures2d/src
文件夹中。
到这应该就编译成功了。最后再运行sudo make install
来安装。
接下来最后将编译好的OpenCV加入到环境变量中sudo gedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加/usr/local/lib
。执行sudo ldconfig
使得刚才的配置路径生效。sudo gedit /etc/bash.bashrc
在最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
最后在source
一下.bashrc
.
至此,Ubuntu下的OpenCV就配置完了。打开VScode,在项目的CMakeLists.txt
文件中加入TARGET_LINK_LIBRARIES(3DR ${OpenCV_LIBS})
即可调用OpenCV库啦。