之前已经完成了OpenCV4.0安装以及其他一些环境配置,详情见https://blog.csdn.net/u012180635/article/details/82143340
OpenCV从RealSense读取图像的实例参见这篇文章https://blog.csdn.net/Sandy_WYM_/article/details/82871773
源码实际是来自RealSense源码包 librealsense/doc/stepbystep/a_getting_started_with_openCV.md
但如果要用RealSense采集Point Cloud还需要安装PCL(处理点云)+ OpenGL(显示图像)。
一、安装
1. 安装PCL
参见https://blog.csdn.net/fsencen/article/details/79386570
2. 安装OpenGL
参见https://blog.csdn.net/csp123258/article/details/82626042
除了OpenGL还要安装glfw库
参见https://blog.csdn.net/dddxxxx/article/details/80411498
需要注意的是GLFW和OpenGL是不同的库,需要单独安装。
GLFW官网上给出了如何修改CMakeLists.txthttps://www.glfw.org/docs/latest/build_guide.html#build_link_cmake_source
总结来说就是加入以下几行
find_package(OpenGL REQUIRED)
find_package(glfw3 3.2 REQUIRED)
include_directories(myapp ${OPENGL_INCLUDE_DIR})
target_link_libraries(myapp glfw ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})
#If OpenGL is found, the OPENGL_FOUND variable is true and the OPENGL_INCLUDE_DIR and OPENGL_gl_LIBRARY cache variables can be used. The OpenGL CMake package also looks for GLU. If GLU is found, the OPENGL_GLU_FOUND variable is true and the OPENGL_INCLUDE_DIR and OPENGL_glu_LIBRARY cache variables can be used.
find_package(OpenGL REQUIRED)会查找package OpenGL,当找到OpenGL时,变量OPENGL_FOUND会设为真,缓存变量OPENGL_INCLUDE_DIR和OPENGL_gl_LIBRARY会被赋值。在寻找OpenGL时,还会一起寻找GLU,当找到GLU时,变量OPENGL_GLU_FOUND会设为真,缓存变量OPENGL_INCLUDE_DIR和OPENGL_glu_LIBRARY会被赋值。这些值将用来寻找头文件和链接库。
二、CMakeLists.txt文件编写
这个CMakeLists.txt文件放在总工程目录下,为每一个子目录都设置好了头文件位置、链接库位置和依赖项等信息。在子项目文件夹里的CMakeLists.txt文件就不用再重复这些内容了。
# minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)
project(MWYProject)
# Save the command line compile commands in the build output
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
# View the makefile commands during build
#set(CMAKE_VERBOSE_MAKEFILE on)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()
find_package(OpenGL REQUIRED)
find_package(PCL 1.2 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(realsense2 REQUIRED)
find_package(glfw3 3.2 REQUIRED)
if (OPENGL_FOUND)
MESSAGE(STATUS "MWY:OpenGL found!")
MESSAGE(STATUS "MWY:OpenGL found!")
MESSAGE(STATUS "MWY:OpenGL found!")
MESSAGE(STATUS "OPENGL_INCLUDE_DIR:" ${OPENGL_INCLUDE_DIR})
endif()
if (OpenCV_FOUND)
MESSAGE(STATUS "MWY:OpenCV found!")
MESSAGE(STATUS "MWY:OpenCV found!")
MESSAGE(STATUS "MWY:OpenCV found!")
MESSAGE(STATUS "OPENGL_INCLUDE_DIR:" ${OPENGL_INCLUDE_DIR})
endif()
if (PCL_FOUND)
MESSAGE(STATUS "MWY:PCL found!")
MESSAGE(STATUS "MWY:PCL found!")
MESSAGE(STATUS "MWY:PCL found!")
MESSAGE(STATUS "PCL_INCLUDE_DIRS:" ${PCL_INCLUDE_DIRS})
endif()
if (realsense2_FOUND)
MESSAGE(STATUS "MWY:realsense2 found!")
MESSAGE(STATUS "MWY:realsense2 found!")
MESSAGE(STATUS "MWY:realsense2 found!")
endif()
if (glfw3_FOUND)
MESSAGE(STATUS "MWY:glfw3 found!")
MESSAGE(STATUS "MWY:glfw3 found!")
MESSAGE(STATUS "MWY:glfw3 found!")
endif()
include_directories(${OPENGL_INCLUDE_DIR})
include_directories(/usr/lib/)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${PCL_INCLUDE_DIRS})
set(DEPENDENCIES realsense2 ${PCL_LIBRARIES} ${OpenCV_LIBS} ${OPENGL_LIBRARIES} glfw ${OPENGL_gl_LIBRARY} ${OPENGL_glu_LIBRARY})
add_subdirectory(rs-pcl)
add_subdirectory(readVideoRealsense)
三、Tips:
1. MESSAGE命令输出了一些依赖项的include目录,方便查看和二次开发。
2. RealSense安装完后include路径默认为/usr/include/librealsense2,仔细对比这个include文件夹和源码文件包里的内容可以发现并不是所有头文件都复制过来了,如果报错说缺少了头文件可以用超级用户模式从源码包里复制到include路径下。
3. 最后两行中rs-pcl和readVideoRealsense是两个子项目:
rs-pcl:pcl处理RealSense读取的点云并显示图像
readVideoRealSense:用OpenCV从RealSense读取视频
详情参见
https://blog.csdn.net/u012180635/article/details/85232736
https://blog.csdn.net/u012180635/article/details/85233775
4. 编译前工程文件夹结构及编译方法
MWYproject
-readVideoRealSense
-readVideoRealSense.cpp
-CMakeLists.txt
-rs-pcl
-rs-pcl.cpp
-CMakeLists.txt
-CMakeLists.txt
首先按照以上结构建立空文件夹和文档,将tip3链接里给的源码和CMakeLists文件复制到各自子文件夹里,将标题二的内容粘到最外一层的CMakeLists文件里。
之后在MWYproject文件夹下打开终端,运行
$ cmake .
$ make
运行第一条命令后MWYproject文件夹下会出现CMakeCache.txt、cmake_install.cmake、compile_commands.json、Makefile几个文件和CMakeFiles文件夹。运行第二条命令后各子文件夹下会出现cmake_install.cmake、Makefile和CMakeFiles文件夹以及执行文件(紫色方块)。若MWYproject文件夹下CMakeLists.txt没有更改,以后只在子文件夹中运行$ make 命令即可。