Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I convert the .raw pixel values from the Depth Map to meters? #3286

Closed
amaanda opened this issue Feb 19, 2019 · 15 comments
Closed

How can I convert the .raw pixel values from the Depth Map to meters? #3286

amaanda opened this issue Feb 19, 2019 · 15 comments

Comments

@amaanda
Copy link

amaanda commented Feb 19, 2019

I want to generate a .ply from the Depth Map information to compare the results I get from using the Left and Right Infrared images. I opened the .raw Depth file as unsigned 16 bits, and I am having a hard time to convert the Depth pixel values to meters (which will be the input to my function).
So basically what I need to know is the following scale:

depth_meters = depth_raw * scale;

depth_raw contains values from 0 to 65,535. Any help would be appreciated. Thanks!

@amaanda amaanda changed the title How can I convert the .raw pixel values from the Depth Map to Disparity? How can I convert the .raw pixel values from the Depth Map to meters? Feb 19, 2019
@MartyG-RealSense
Copy link
Collaborator

Using the depth_scale instruction may be what you need.

#2348

Below is another example of use of depth_scale, where the user converts X-Y pixels to X-Y positions in meters.

#2481 (comment)

@amaanda
Copy link
Author

amaanda commented Feb 19, 2019

I ran the instruction in the .bag file, but when I get the .raw depth (not png image), multiplying the values by the scale provided by get_depth_scale() gives me values of around 49 to 65, which are not in meters. So I am not sure if this depth_scale works for using .raw images.

@amaanda
Copy link
Author

amaanda commented Feb 19, 2019

What I have so far:

cv::Mat depth16(720, 1280, CV_16U, (uchar *)buffer.data());
cv::Mat depthfloat;
depthfloat = depth16.clone();
depth16.convertTo(depthfloat, cv::DataType<float>::type); // values from 0 to 65535
depthfloat = depthfloat * 0.0010000000474974513; // the scale I get from the instruction

@MartyG-RealSense
Copy link
Collaborator

Would this script be of any help? It came from a discussion where a person was trying to get the depth data from a .raw file.

#2200 (comment)

@amaanda
Copy link
Author

amaanda commented Feb 19, 2019

This example seems to work with .png format and not the .raw images. I am already able to get disparity from the .png left and right images. You don't think I would lose information by using the .png instead of the .raw format for the Depth frames?

@MartyG-RealSense
Copy link
Collaborator

I apologize, RealSense programming is not one of my specialties, so I have to rely on what I can research. One of the Intel staff on this forum may be able to give you a better answer about conversion (I do not work for Intel).

As @ev-mp says in the link below, you may indeed get better results from a .raw.

#2231 (comment)

@amaanda
Copy link
Author

amaanda commented Feb 19, 2019

I understand. I will wait for the staff's opinion then. Thanks a lot for your help!

@ev-mp
Copy link
Collaborator

ev-mp commented Feb 20, 2019

Hello @amaanda ,
Can you look into the following lines:

depthfloat = depth16.clone();
depth16.convertTo(depthfloat, cv::DataType<float>::type); // values from 0 to 65535

The type of depthfloat is the same as origin (UINT16). Is it possible that since the target matrix is already defined then convertTo call wouldn't resize it?
Can you try to replace the line with explicit float matrix
depthfloat = depth16.clone() with cv::Mat depthfloat(720, 1280, CV_32F) ?

You may also convert to metric units with a single call by passing the scale factor as the third param to convertTo
depth16.convertTo(depthfloat, cv::DataType<float>::type, 0.0010000000474974513); // values from 0 to 65535

@amaanda
Copy link
Author

amaanda commented Feb 20, 2019

Hello @ev-mp
I implemented the changes you asked. I agree with what you said about using depth.clone(), thank you for the tip.
This is the ply I am currently getting:
PS.: The function I am using to generate the pointcloud already works fine for .png images, so I am sure the problem I am having is with some kind of wrong conversion of the pixels on the .raw file.
snapshot00

@amaanda
Copy link
Author

amaanda commented Feb 20, 2019

This is the code that is generating the .ply above:

std::ifstream file("_Depth_37140.raw", std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.clear();
file.seekg(0, std::ios::beg);
std::vector buffer(size);

for (int i = 0; i < size-1; i+=2) {
file.get(buffer[i + 1]);
file.get(buffer[i]);
}
file.close();

cv::Mat depth16(720, 1280, CV_16U, (uchar *)buffer.data());
cv::Mat depthfloat(720, 1280, CV_32F);
depth16.convertTo(depthfloat, cv::DataType::type, 0.0010000000474974513);

stereo_camera_config camera_config;
camera_config.baseline = 0.0499061;
camera_config.fx = 0.50101;

cv::Mat disparityFile = StereoMatching::depthToDisparity(depthfloat, camera_config);

@ev-mp
Copy link
Collaborator

ev-mp commented Feb 20, 2019

@amaanda , one thing I notice is that the origin frame data is row-major:
cv::Mat depth16(720, 1280, 1280,720,CV_16U, (uchar *)buffer.data());

@amaanda
Copy link
Author

amaanda commented Feb 21, 2019

But 720 is the number of rows, and 1280 the number of columns, isn't it?

@amaanda
Copy link
Author

amaanda commented Feb 21, 2019

I was able to solve my problem. I used the depth sensor scale and changed the for loop.

@amaanda amaanda closed this as completed Feb 21, 2019
@ghost
Copy link

ghost commented Jun 17, 2019

Hi @amaanda ,
Can you please share the code for getting depth value in metres from .raw file .
Thanks in advance .

@Resays
Copy link

Resays commented Jul 1, 2019

That's the problem ... people want help, but they don't help others ...
I'm trying to get depth and xyz from raw file as well ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants