Items with no label
3335 Discussions

Depth of a Pixel

MAlde3
Beginner
2,908 Views

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 Replies
MartyG
Honored Contributor III
1,046 Views

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

0 Kudos
MAlde3
Beginner
1,046 Views

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

0 Kudos
MartyG
Honored Contributor III
1,046 Views

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);

0 Kudos
MAlde3
Beginner
1,046 Views

Thanks.

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

0 Kudos
MartyG
Honored Contributor III
1,046 Views

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.

0 Kudos
MAlde3
Beginner
1,046 Views

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.

0 Kudos
MartyG
Honored Contributor III
1,046 Views

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

0 Kudos
JRomu
Novice
1,046 Views

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

0 Kudos
MartyG
Honored Contributor III
1,046 Views

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

0 Kudos
Reply