Items with no label
3338 Diskussionen

Depth of a Pixel

MAlde3
Einsteiger
4.489Aufrufe

Hello,

What is the correct way of reading the depth of individual pixels in C++?

I want to copy depth matrix (without colorizing) to an OpenCV Mat and then make calculations with it.

0 Kudos
9 Antworten
MartyG
Geehrter Beitragender III
2.627Aufrufe

Are you using RealSense SDK 2.0 or the older '2016 R2' or '2016 R3' RealSense SDK, please?

MAlde3
Einsteiger
2.627Aufrufe

I am using RealSense SDK 2.0 on a RS300, which will be replaced with D435

MartyG
Geehrter Beitragender III
2.627Aufrufe

Here's some C++ code for getting pixel depth in meters in SDK 2.0.

rs2::depth_frame dpt_frame = frame.as();

float pixel_distance_in_meters = dpt_frame.get_distance(x,y);

MAlde3
Einsteiger
2.627Aufrufe

Thanks.

is there a way of getting distance values for all pixels at once?

MartyG
Geehrter Beitragender III
2.627Aufrufe

I wonder if a Point Cloud would be a suitable way for you to get all depth points at the same time.

https://github.com/IntelRealSense/librealsense/tree/master/examples/pointcloud librealsense/examples/pointcloud at master · IntelRealSense/librealsense · GitHub

You can also get a point cloud in the RealSense Viewer software by clicking on the '3D' option next to '2D' in the top corner of the Viewer program when a stream is playing.

MAlde3
Einsteiger
2.627Aufrufe

I need the depth of every pixel for calculations in OpenCV. It will be running on an embedded device with Ubuntu, so Realsense Viewer is not an option.

MartyG
Geehrter Beitragender III
2.627Aufrufe

rs2_deproject_pixel_to_point may do the job. Deprojection takes a 2D pixel location on a stream's images, as well as a depth, specified in meters, and maps it to a 3D point location within the stream's associated 3D coordinate space.

https://github.com/IntelRealSense/librealsense/issues/1413 deprojection pixels to 3d point · Issue # 1413 · IntelRealSense/librealsense · GitHub

You may also be interested in a pre-made depth program for OpenCV by developer UnaNancyOwen.

https://github.com/UnaNancyOwen/RealSense2Sample/tree/master/sample/Depth RealSense2Sample/sample/Depth at master · UnaNancyOwen/RealSense2Sample · GitHub

JRomu
Anfänger
2.627Aufrufe

Hi,

I have done it this way;

Quote

float darray [480 * 640];

int main()

 

{

 

 

//Contruct a pipeline which abstracts the device

 

rs2::pipeline pipe;

 

 

//Create a configuration for configuring the pipeline with a non default profile

 

rs2::config cfg;

 

 

//Add desired streams to configuration

 

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

 

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

 

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

 

//Instruct pipeline to start streaming with the requested configuration

 

pipe.start(cfg);

 

 

namedWindow("Colour", WINDOW_AUTOSIZE); namedWindow("IR", WINDOW_AUTOSIZE);

 

while (1) {

rs2::frameset frames;

 

 

//Wait for all configured streams to produce a frame

 

frames = pipe.wait_for_frames();

 

 

// Try to get a frame of a depth image

 

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

// darray contains distance meters of all the x,y

 

for (int j = 0; j < 480; j++)

 

{

for (int i = 0; i< 640; i++)

 

{

 

darray[i + (j * 640)]= d.get_distance(i, j);

 

 

}

 

 

}

 

 

//Get each frame

 

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

 

rs2::frame ir_frame = frames.first(RS2_STREAM_INFRARED);

 

 

// Creating OpenCV Matrix from a color image

 

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

 

Mat ir(Size(640, 480), CV_8UC1, (void*)ir_frame.get_data(), Mat::AUTO_STEP);

 

// Display in a GUI

 

 

imshow("Colour", color); imshow("IR", ir);

char key = waitKey(30);

 

if (key == 27) {

 

cout << "key pressed" << endl;

color.release(); ir.release();

break;

 

}

}

destroyAllWindows();

return 0;

 

}

Unquote

MartyG
Geehrter Beitragender III
2.627Aufrufe

Awesome, thanks so much for sharing your script with the community, JJR

Antworten