Save Raw DepthFrame in c#
Hi,
I have a D415 camera. As I have seen in the examples I can get the Depth frames and save as jpg as Colorized frames using this code.
using (var frames = _irsPipeline.WaitForFrames(2000)) // Before 2000
{
var colorFrame = frames.ColorFrame.DisposeWith(frames);
var depthFrame = frames.DepthFrame.DisposeWith(frames);
var colorizedDepth = _colorizer.Process<VideoFrame>(depthFrame).DisposeWith(frames);
}
How can I save the raw data of the Depth frame? What is the best way and what type of format is accepted to use?
I think that I can save data using a matrix and saving in Csv file for example. But doing this is more difficult to rotate data for example.
Maybe something like this
public RawDepthFrameDto(DepthFrame frame)
{
_width = frame.Width;
_height = frame.Height;
_frameData = new float[_height, _width];
FillRawDepth(frame);
}
private void FillRawDepth(DepthFrame frame)
{
for(int x = 0; x < _height; x++)
{
for(int y = 0; y < _width; y++)
{
_frameData[x, y] = frame.GetDistance(x, y);
}
}
}
With regards,
-
Hi Juan Diezyanguas The link below discusses saving raw depth data with C#.
https://github.com/IntelRealSense/librealsense/issues/4201
The discussion in that link also provides a link to C# scripting for accessing the data without copying in an unsafe context.
Another approach would be to save the depth data to a bag file.
https://github.com/IntelRealSense/librealsense/issues/3142
The bag file could then be played back and have data retrieved from it.
An alternative way to obtain what you require from the bag file would be to use the RealSense SDK's rs-convert tool to extract the bag file's frames and convert them to a suitable format that preserves depth information such as csv, .raw or .bin.
https://github.com/IntelRealSense/librealsense/tree/master/tools/convert
A pre-made executable version of rs-convert can be found in the RealSense SDK's set of tools and examples.
-
Thanks for your reply!
I'm trying to compare the results using CopyTo and GetDistance method and it seems that they are not producing the same matrix.
I'm not sure if I'm obtaining the correct data from the copyed ushort array.
Thanks for your help.
private readonly int _width;
private readonly int _height;
private readonly float _depthScale;
private readonly ushort[] _depthData;
private float[,] _frameData;
private float[,] _frameData2;
public RawDepthFrameDto(DepthFrame frame)
{
_width = frame.Width;
_height = frame.Height;
_depthScale = frame.Sensor.DepthScale;
_depthData = new ushort[_width * _height];
frame.CopyTo(_depthData);
_frameData = new float[_height, _width];
_frameData2 = new float[_height, _width];
FillRawDepth(frame);
FillRawDepthFromCopy();
}
private void FillRawDepth(DepthFrame frame)
{
for(int row = 0; row < _height; row++)
{
for(int column = 0; column < _width; column++)
{
_frameData[row, column] = frame.GetDistance(column, row);
}
}
}
private void FillRawDepthFromCopy()
{
for (int row = 0; row < _height; row++)
{
for (int column = 0; column < _width; column++)
{
var idx = (row * _width) + column;
_frameData2[row, column] = _depthData[idx] * _depthScale;
}
}
} -
If GetDistance() is not used then the real-world depth value in meters is calculated by multiplying the depth value by 0.001 (the default 'depth unit scale' of the D415 camera. For example, if the raw 'pixel depth' uint16_t value is 6500 then 6500 x 0.001 = 6.5 (meters real world distance).
With the GetDistance() instruction there is no need to multiply the depth value by the depth unit scale value as it is already taken care of automatically by the instruction.
-
Yes,
I know that it's not neccesary to use both ways, but I have done the code in my previous post just to check that both matrix are equals using the multiply or the GetDistance() method.
And it seems that they are not equal. That is the reason because I have posted it to show you the code and check if you see correct o detect any error.
I think that it's better to use CopyTo() because it is much more faster to use the GeDistance() method arround all the image pixels.
With regards,
Please sign in to leave a comment.
Comments
9 comments