Repository navigation
Expand file tree
/
Copy pathrs_stream.cpp
More file actions
45 lines (34 loc) · 1.71 KB
/
Copy pathrs_stream.cpp
File metadata and controls
45 lines (34 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp> // Include OpenCV API
int main()
{
// Create a RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
// Enable the color stream with resolution 640x480 at 30 frames per second in BGR format
cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);
// Start streaming with the chosen configuration
rs2::pipeline_profile profile = pipe.start(cfg);
// Create an OpenCV window to display the color stream
const std::string window_name = "RealSense Color Stream";
cv::namedWindow(window_name, cv::WINDOW_AUTOSIZE);
while (true)
{
// Wait for the next set of frames from the camera
rs2::frameset frames = pipe.wait_for_frames();
// Get a frame from the color stream
rs2::frame color_frame = frames.get_color_frame();
// Query frame size (width and height)
const int width = color_frame.as<rs2::video_frame>().get_width();
const int height = color_frame.as<rs2::video_frame>().get_height();
// Create an OpenCV matrix of size (width, height) with 8-bit unsigned chars and 3 channels (BGR)
cv::Mat image(cv::Size(width, height), CV_8UC3, (void*)color_frame.get_data(), cv::Mat::AUTO_STEP);
// Display the image in the created window
cv::imshow(window_name, image);
// Break the loop if any key is pressed (with a delay of 1 ms)
if (cv::waitKey(1) >= 0)
break;
}
return 0;
}