OpenCV以及OpenCV_contrib安装教程

如何成功编译OpenCV以及contrib

前言:
最近这一年多时间学习了大量的视觉以及SLAM相关知识,因此也编译安装了很多遍OpenCV和contrib,这里面踩了不少的坑,最终总结下来了一个可以在Linux_x86_64架构以及ARM64架构的处理器都可以成功的教程。
下面是具体的安装步骤:
1.下载OpenCV以及对应版本的contrib,注意版本一定要对应,本人下载的是OpenCV3.4.13以及对应的contrib
2.下载cmake-gui,Ubuntu下的安装命令为:sudo apt-get install cmake-qt-gui
3.进入OpenCV文件夹

cd opencv-3.4.13
mkdir build
cd build
cmake-gui

4.使用cmake-gui编译可参考以下教程
cmake-gui 教程
如果编译opencv_contrib的话,记得在这时进入OpenCV3.4.13/modules/CMakeLists.txt,如下所示,加入include_directories()

add_definitions(-D__OPENCV_BUILD=1)
include_directories(/opencv_contrib-3.4.13/modules/xfeatures2d/include)#注意括号里面的路径要改成你自己OpenCVcontrib下的xfeature2d对应的路径,可以采用pwd命令查询后复制粘贴
if(NOT OPENCV_MODULES_PATH)
  set(OPENCV_MODULES_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
endif()

ocv_glob_modules(${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH})

# build lists of modules to be documented
set(OPENCV_MODULES_MAIN "")
set(OPENCV_MODULES_EXTRA "")

foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
  string(REGEX REPLACE "^opencv_" "" mod "${mod}")
  if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${OpenCV_SOURCE_DIR}/modules/${mod}")
    list(APPEND OPENCV_MODULES_MAIN ${mod})
  else()
    list(APPEND OPENCV_MODULES_EXTRA ${mod})
  endif()
endforeach()
ocv_list_sort(OPENCV_MODULES_MAIN)
ocv_list_sort(OPENCV_MODULES_EXTRA)
set(FIXED_ORDER_MODULES core imgproc imgcodecs videoio highgui video calib3d features2d objdetect dnn ml flann photo stitching)
list(REMOVE_ITEM OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES})
set(OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES} ${OPENCV_MODULES_MAIN})

set(OPENCV_MODULES_MAIN ${OPENCV_MODULES_MAIN} CACHE INTERNAL "List of main modules" FORCE)
set(OPENCV_MODULES_EXTRA ${OPENCV_MODULES_EXTRA} CACHE INTERNAL "List of extra modules" FORCE)

进入OpenCV3.4.13/modules/stitching/CMakeLists.txt,如下所示

set(the_description "Images stitching")
INCLUDE_DIRECTORIES("/opencv_contrib-3.4.13/modules/xfeatures2d/include")#注意括号里面的路径要改成你自己OpenCVcontrib下的xfeature2d对应的路径
if(HAVE_CUDA)
  ocv_warnings_disable(CMAKE_CXX_FLAGS -Wundef -Wmissing-declarations -Wshadow -Wstrict-aliasing)
endif()

set(STITCHING_CONTRIB_DEPS "opencv_xfeatures2d")
if(BUILD_SHARED_LIBS AND BUILD_opencv_world AND OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
  set(STITCHING_CONTRIB_DEPS "")
endif()
ocv_define_module(stitching opencv_imgproc opencv_features2d opencv_calib3d opencv_flann
                  OPTIONAL opencv_cudaarithm opencv_cudawarping opencv_cudafeatures2d opencv_cudalegacy opencv_cudaimgproc ${STITCHING_CONTRIB_DEPS}
                  WRAP python)

注:修改两个cmakelist文件建议pwd查看当前文件目录。

5.下载OpenCV_contrib中缺少的文件
进入如下网址https://github.com/LeonGoretzkatju/OpenCVtutorial/tree/master,下载src文件夹(建议只增加其中缺少的文件),将其粘贴到xfeatures2d/下面

6.参考上述的cmake-gui教程进行编译,编译时为了提高速度可以采用make -j8进行编译,大概需要二十多分钟编译完成。
最后sudo make install
不要忘记增加环境变量

你可能感兴趣的:(Robomaster-CV,opencv,计算机视觉)