CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(RealsenseExamples)
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")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
find_package(OpenCV REQUIRED)
find_package(OpenGL REQUIRED)
set(DEPENDENCIES realsense ${OPENGL_LIBRARIES})
if(WIN32)
add_subdirectory(third_party/glfw)
list(APPEND DEPENDENCIES glfw3)
else()
# Find glfw header
find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3.h
PATHS /usr/X11R6/include
/usr/include/X11
/opt/graphics/OpenGL/include
/opt/graphics/OpenGL/contrib/libglfw
/usr/local/include
/usr/include/GL
/usr/include
)
# Find glfw library
find_library(GLFW_LIBRARIES NAMES glfw glfw3
PATHS /usr/lib64
/usr/lib
/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}
/usr/local/lib64
/usr/local/lib
/usr/local/lib/${CMAKE_LIBRARY_ARCHITECTURE}
/usr/X11R6/lib
)
list(APPEND DEPENDENCIES m ${GLFW_LIBRARIES} ${LIBUSB1_LIBRARIES})
include_directories(${GLFW_INCLUDE_DIR})
endif()
add_executable(realsense_example realsense_example.cpp)
target_link_libraries(realsense_example ${DEPENDENCIES})
target_link_libraries( realsense_example ${OpenCV_LIBS} )
主程序:
#include <librealsense/rs.hpp>
#include "example.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <thread>
using namespace cv;
using namespace std;
struct rgb_pixel
{
uint8_t r,g,b;
};
int main()
{
rs::log_to_console(rs::log_severity::warn);
rs::context ctx;
if(ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
rs::device & dev = *ctx.get_device(0);
dev.enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 60);
dev.enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
dev.enable_stream(rs::stream::infrared, 640, 480, rs::format::y8, 60);
dev.start();
while(1)
{
dev.wait_for_frames();
uchar* pRgb = (uchar*)dev.get_frame_data(rs::stream::color);
uint16_t *depthImage = (uint16_t *) dev.get_frame_data(rs::stream::depth);
Mat rgb_show;
Mat rgb(480, 640, CV_8UC3, pRgb);
cvtColor(rgb, rgb_show, CV_BGR2RGB);
imshow("RGBImage",rgb_show);
Mat depth(480,640,CV_16UC1, depthImage);
imshow("Depthimage",depth*15);
uchar* pFra = (uchar*)dev.get_frame_data(rs::stream::infrared);
Mat fra_img(480, 640, CV_8UC1, pFra);
imshow("FraImage",fra_img); waitKey(10);
}
}
其中CMakeLists是根据例子中的改的,所以需要把example.hpp和第三方头文件文件夹都要拷过来。