Items with no label
3335 Discussions

Question) How can I get 'depth' image as opencv?

mgum1
Beginner
1,323 Views

Hello, thank you for help.

There is two source code.

First one is basic example supplied by Intel, the second source which i modify is combined by opencv.

The problem is, I can't use "render". because of pipe.start(cfg)

i should use pipe.start(cfg) because 'cfg enable_stream--' function need that for showing color image.

but, because of this pipe.start(cfg), below

" depth_image.render(depth, { 0, 0, app.width() / 2, app.height() });"

sentence didn't work. (if i modify pipe.start(cfg) to pipe.start(), it did work.)

so, how can i active this render without delete pipe.start(cfg)?

or, is there any solution to make color image and depth image like first code on each window?

/////////////////////////////////////////////////// first code ////////////////////////////////////////////////////////

# include

# include "example.hpp"

int main(int argc, char * argv[]) try

{

window app(1280, 720, "RealSense Capture Example");

texture depth_image, color_image;

rs2::colorizer color_map;

rs2::pipeline pipe;

pipe.start();

while(app) // Application still alive?

{

rs2::frameset data = pipe.wait_for_frames();

rs2::frame depth = color_map(data.get_depth_frame());

rs2::frame color = data.get_color_frame();

depth_image.render(depth, { 0, 0, app.width() / 2, app.height() });

color_image.render(color, { app.width() / 2, 0, app.width() / 2, app.height() });

}

return EXIT_SUCCESS;

}

/////////////////////////////////////////////////// second code ////////////////////////////////////////////////////////

# include

# include

# include "example.hpp"

using namespace std;

using namespace cv;

int main()

{

window app(1280, 720, "RealSense Capture");

rs2::pipeline pipe;

rs2::config cfg;

cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);

cfg.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);

cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);

pipe.start(cfg);

texture depth_image;

for (;;)

{

rs2::frameset frames = pipe.wait_for_frames();

rs2::frame color_frame = frames.get_color_frame();

rs2::frame depth_frame = frames.get_depth_frame();

depth_image.render(depth_frame, { 0 , 0 , app.width() / 2, app.height() });

// show the color image

Mat color(Size(640, 480), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP);

namedWindow("Display Image", WINDOW_AUTOSIZE);

imshow("Display Image", color);

// i want depth image by this code, but there's error.

/*Mat depth_show(Size(640, 480), CV_8UC1, (void*)depth_frame.get_data(), Mat::AUTO_STEP);

namedWindow("Display infrared", WINDOW_AUTOSIZE);

imshow("Display infrared", depth_show);*/

waitKey(10);

}

return 0;

}

0 Kudos
1 Reply
idata
Employee
378 Views

Hello P_D_C,

 

 

This code achieves what you are trying to do:

 

 

window app(1280, 720, "RealSense Capture");

 

 

rs2::pipeline pipe;

 

 

rs2::config cfg;

 

 

 

 

cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);

 

 

cfg.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8, 30);

 

 

cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);

 

 

 

 

pipe.start(cfg);

 

 

texture depth_image;

 

 

// Declare depth colorizer for pretty visualization of depth data

 

rs2::colorizer color_map; //Added this, needed later

 

 

for (;;)

 

 

{

 

 

rs2::frameset frames = pipe.wait_for_frames();

 

 

rs2::frame color_frame = frames.get_color_frame();

 

 

// rs2::frame depth_frame = frames.get_depth_frame();

 

rs2::frame depth_frame = color_map(frames.get_depth_frame()); // Find and colorize the depth data //Added color_map

 

 

depth_image.render(depth_frame, { 0 , 0 , app.width() / 2, app.height() });

 

 

// show the color image

 

 

Mat color(Size(640, 480), CV_8UC3, (void*)color_frame.get_data(), Mat::AUTO_STEP);

 

 

namedWindow("Display Image", WINDOW_AUTOSIZE);

 

 

imshow("Display Image", color);

 

 

Mat depth_show(Size(640, 480), CV_8UC3, (void*)depth_frame.get_data(), Mat::AUTO_STEP); //Changed CV_8UC1 -> CV_8UC3

 

 

namedWindow("Display infrared", WINDOW_AUTOSIZE);

 

 

imshow("Display infrared", depth_show);

 

 

waitKey(10);

 

 

}

 

return 0;

 

 

Regards,

 

Jesus G.

 

Intel Customer Support

 

0 Kudos
Reply