看到网上很多的文章都是用的库是librealsense,现在用的库是librealsense2,感觉差别较大。
#include
#include
using namespace std;
using namespace cv;
int main()
{
//构建一个抽象设备的管道
rs2::pipeline pipe;
//使用非默认配置文件创建配置以配置管道
rs2::config cfg;
//将所需的流添加到配置
cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);
//指示管道使用请求的配置开始流式传输
pipe.start(cfg);
while(1)
{
// 放下几个第一帧以使自动曝光稳定
rs2::frameset frames;
for(int i = 0; i < 30; i++)
{
//等待所有配置的流生成一个帧
frames = pipe.wait_for_frames();
}
//获取每个帧
rs2::frame color_frame = frames.get_color_frame();
//从彩色图像创建OpenCV矩阵
Mat color(Size(640, 480), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP);
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", color);
waitKey(0);
}
return 0;
}
因为用的是Qt的.pro文件
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2\
/usr/local/include/librealsense2
LIBS += /usr/local/lib/libopencv_calib3d.so \
/usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_features2d.so \
/usr/local/lib/libopencv_flann.so \
/usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_imgcodecs.so \
/usr/local/lib/libopencv_imgproc.so \
/usr/local/lib/libopencv_ml.so \
/usr/local/lib/libopencv_objdetect.so \
/usr/local/lib/libopencv_photo.so \
/usr/local/lib/libopencv_shape.so \
/usr/local/lib/libopencv_stitching.so \
/usr/local/lib/libopencv_superres.so \
/usr/local/lib/libopencv_videoio.so \
/usr/local/lib/libopencv_video.so \
/usr/local/lib/libopencv_videostab.so\
/usr/local/lib/librealsense2.so
运行结果: