Is depth.get_distance(x,y) a time consuming function on C++?
| Required Info | |
|---|---|
| Camera Model | D400 |
| Firmware Version | 05.09.02.00 |
| Operating System & Version | Win 10 |
| Kernel Version (Linux Only) | |
| Platform | PC |
| SDK Version | 2.16.1 |
| Language | C++ |
| Segment | others |
Edit: Test Results
Using .get_distance(x, y), the time it takes to execute is as follows:
1 coordinate: ~0.032 seconds
100 coordinates: ~3.2 seconds
40,000 coordinates: ~21.5 minutes
921,600 coordinates: ~8.2 hours
Introduction
Hello all again! I have asked a similar question here three weeks ago, but there wasn't any satisfactory answers.
Goal
The x and y columns are (x, y) coordinates respectively, while the last column is the depth column for each corresponding (x, y) coordinate. There are 921,600 data which represents 921,600 pixels from a photo resolution of 1280x720.

Issue
To achieve this, I wrote a for loop that loops for 921,600 times just writing to a CSV format file.
CSV Loop - V1
for (int y = 0; y < 921600; y++) {
myfile << "x,y,c\n";
}
In my own testing, this loop took ~512ms to complete.
CSV Loop - V2
for (int y = 0; y < 720; y++) {
for (int x = 0; x < 1280; x++) {
myfile << x << "," << y << "," << "c\n";
}
}
In my own testing, this loop took ~8709ms to complete.
CSV Loop - V3
do {
for (int x = 0; x < 1280; x++) {
myfile << x << "," << y << "," << "c\n";
}
y++;
} while (y < 720);
In my own testing, this loop took ~8317ms to complete.
Implementation Idea (Need Advice)
I am thinking of capturing a single frame using the rosbag rs-record-playback CPP example, then using the depth.get_distance(x,y); CPP function to loop 921,600 times for every pixel. Something like this:
for (int y = 0; y < 720; y++) {
for (int x = 0; x < 1280; x++) {
c = depth.get_distance(x,y);
myfile << x << "," << y << "," << c << "\n";
}
}
Do you think the function is fast enough to loop? Or will it take very long and is impossible? I have tried taking a look at the rs-pointcloud example.. where Dorodnic has mentioned "the output of pointcloud object is a collection of {x,y,z} coordinates in the real world". Here is a snippet of the pointcloud code from example.hpp:
/* this segment actually prints the pointcloud */
auto vertices = points.get_vertices(); // get vertices
auto tex_coords = points.get_texture_coordinates(); // and texture coordinate
for (int i = 0; i < points.size(); i++)
{
if (vertices[i].z)
{
// upload the point and texture coordinates only for points we have depth data for
glVertex3fv(vertices[i]);
glTexCoord2fv(tex_coords[i]);
}
}
I know from this segment, points.size() is actually 921,600, so this function does loop 921,600 times in order to draw the pointcloud. However, do you think this can be applied to the function of getting depth for each individual pixel without the function taking too long (ie more than a few minutes)?
Thank you very much.. looking forward to the suggestions or solutions. 😄
Hi @HippoEug
Using get_distance in a loop is not recommended. It is more suitable when you need to check the distance of couple of specific interest points. The main reason for the slowdown is that the compiler has no way to inline the call (since you are passing library boundaries) so instead of just going to the depth value in memory, the compiler is forced to generate a bunch of function calls and exception handling blocks. A better way is to multiply the raw data by the value of RS2_OPTION_DEPTH_UNITS option.
Example: cv-helpers.hpp:43-56
Hello Dorodnic! Thanks for the wonderful insight, I think I have a rough idea of what you are talking about.
I will take a look at the example you sent, if there are any further questions on this issue I will further clarify again. Thank you!
Hi @HippoEug Using
get_distancein a loop is not recommended. It is more suitable when you need to check the distance of couple of specific interest points. The main reason for the slowdown is that the compiler has no way to inline the call (since you are passing library boundaries) so instead of just going to the depth value in memory, the compiler is forced to generate a bunch of function calls and exception handling blocks. A better way is to multiply the raw data by the value ofRS2_OPTION_DEPTH_UNITSoption.
Hello @dorodnic and everyone, I read the above and the other thread, Error compiling librealsense/wrappers/opencv/dnn. Having a little difficulty understanding the solutions you mentioned. Anyways, was wondering of alternative solutions while I figure out the solutions you mentioned.
Alternative Solution:
I would like to bring up part of the original question again.. since the rs-pointcloud.cpp or pointclouds in general are made up of (x, y, z) coordinates, why can't we just extract the (x, y, z) coordinates from the function which draws the pointcloud in rs-pointcloud.cpp?
Here is a snippet of the pointcloud code from example.hpp:
/* this segment actually prints the pointcloud */
auto vertices = points.get_vertices(); // get vertices
auto tex_coords = points.get_texture_coordinates(); // and texture coordinate
for (int i = 0; i < points.size(); i++)
{
if (vertices[i].z)
{
// upload the point and texture coordinates only for points we have depth data for
glVertex3fv(vertices[i]);
glTexCoord2fv(tex_coords[i]);
}
}
The problem I have currently is that I am unable to get any (x, y, z) coodinates from the above function, just do not know where the code is "hiding"!
Thank you for reading! 😃 😄
[Realsense Customer Engineering Team Comment] Ticket being closed due to inactivity for 30+ days