Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
Getting Started

Displaying Color Frame

In this demo, you will acquire color frame from the RealSense camera and display it using OpenCV. Before you start, make sure you have librealsense and OpenCV installed and working properly on your system. Using the editor of your choise create BGR_sample.cpp and copy-paste the following code-snippet:

// include the librealsense C++ header file
// include OpenCV header file
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
// Create a context object. This object owns the handles to all connected realsense devices
// Access the first available RealSense device
rs::device * dev = ctx.get_device(0);
// Configure Infrared stream to run at VGA resolution at 30 frames per second
// Start streaming
dev->start();
// Camera warmup - Dropped several first frames to let auto-exposure stabilize
for(int i = 0; i < 30; i++)
// Creating OpenCV Matrix from a color image
Mat color(Size(640, 480), CV_8UC3, (void*)dev->get_frame_data(rs::stream::color), Mat::AUTO_STEP);
// Display in a GUI
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", color);
waitKey(0);
return 0;
}

Compile and run the application from terminal using the following command line:

g++ -std=c++11 BGR_sample.cpp -lrealsense -lopencv_core -lopencv_highgui -o BGR && ./BGR

Result:

BGR_Image

Displaying Infrared Frame

Displaying Infrared and Depth data is not very different from Color data. Infrared frame is a single channel, 8 bits-per-pixel image. Copy the following code snippet into IR_sample.cpp:

// include the librealsense C++ header file
// include OpenCV header file
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
rs::device * dev = ctx.get_device(0);
// Configure Infrared stream to run at VGA resolution at 30 frames per second
// We must also configure depth stream in order to IR stream run properly
// Start streaming
dev->start();
// Camera warmup - Dropped frames to allow stabilization
for(int i = 0; i < 40; i++)
// Creating OpenCV matrix from IR image
Mat ir(Size(640, 480), CV_8UC1, (void*)dev->get_frame_data(rs::stream::infrared), Mat::AUTO_STEP);
// Apply Histogram Equalization
equalizeHist( ir, ir );
applyColorMap(ir, ir, COLORMAP_JET);
// Display the image in GUI
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", ir);
waitKey(0);
return 0;
}

Compile and run the program from the terminal, with the following command:

g++ -std=c++11 IR_sample.cpp -lrealsense -lopencv_core -lopencv_imgproc -lopencv_highgui -o ir && ./ir

Result :

IR_Image2

rs::device::start
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
rs::device::wait_for_frames
void wait_for_frames()
Blocks until new frames are available.
Definition: rs.hpp:985
rs::stream::color
rs::stream::depth
rs::context
Context.
Definition: rs.hpp:319
rs::device::get_frame_data
const void * get_frame_data(stream stream) const
Retrieves contents of latest frame on a stream.
Definition: rs.hpp:1050
rs.hpp
Exposes librealsense functionality for C++ compilers.
rs::context::get_device
device * get_device(int index)
Definition: rs.hpp:354
rs::stream::infrared
rs::device::enable_stream
void enable_stream(stream stream, int width, int height, format format, int framerate, output_buffer_format output_buffer_type=output_buffer_format::continous)
Enables specific stream and requests specific properties.
Definition: rs.hpp:704
rs::format::bgr8
rs::format::y8
rs::device
Provides convenience methods relating to devices.
Definition: rs.hpp:567
rs::format::z16