(9条消息) Ubuntu下OpenCV配置(VScode+cmake)

近期为了做三维重建。开始了OpenCV学习之路。顺便记录一下。

前期准备

如题所述,本次是在Ubuntu下配置的。所以用到的各种软件如下所示:

1. cmake 直达链接

2. VScode 直达链接;

3. OpenCV 直达链接;

4. OpenCV_contrib 直达链接;

配置流程

1.cmake的安装

将cmake下载并解压到任意文件夹下,然后建立软连接。我是放在~/opt
sudo ln -sf /opt/cmake-3.19.2/bin/* /usr/bin/

2.Vscode的安装

这里不再赘述。下载安装即可。

3.OpenCV的安装

将4.5对应的俩个压缩包下载下来。分别更改为opencvopencv_contrib,然后将opencv_contrib放入到opencv目录下,再将opencv放到你想安装的地方。我放在D:\opencv.进入该目录,新建一个build文件夹用于后续编译。

4.用cmake配置OpenCV编译文件

安装完cmake后在终端里输入cmake-gui即可看到CMake(cmake_gui)程序。

Where is the source code 选择安装opencv的目录,我的是/home/pc/opencvWhere to build the binaries 选择build文件夹我的是/home/pc/opencv/build然后点击 configure 按钮来配置,在弹出的页面按下图选择后点 Finish

耐心等待进度条读完,直到输出窗口看到 Configuring done
然后中间就会弹出一系列的标红的配置变量。在这需要重点关注几个变量:

  1. OPENCV_EXTRA_MODULES_PATH:value 中浏览选择opencv_contrib的目录即/home/pc/opencv/opencv_contrib/modules;

  2. ENABLE_CXX11: 勾选;

  3. OPENCV_ENABLE_NONFREE: 勾选;

  4. WITH_MSMF: 取消勾选;
    更改完再点击 configure 按钮来配置,等待 Configuring done ,然后点击 Generate 等待 Generating done

cmake OpenCV源文件

打开终端,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"

下载该压缩包,解压后将里面的文件放入/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库啦。

(0)

相关推荐