Items with no label
3335 Discussions

Most effecient way to get depth data & edit display image

DNone
Beginner
1,271 Views

I'm modifying the DF_RawStreams C# example project and I have two goals:

1. Get the depth data in the most efficient way possible

2. Edit the image displayed on the fly in the most efficient way possible.

I've hobbled together some working code for getting the depth in mm:

if (MainPanel != PXCMCapture.StreamType.STREAM_TYPE_ANY && render != null)

{

image = sample[MainPanel];

image.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out imageData);

var dwidth = image.info.width;

var dheight = image.info.height;

var dPixels = imageData.ToShortArray(0, dwidth * dheight);

for (int y = 0; y < dheight; y++)

{

for (int x = 0; x < dwidth; x++)

{

var depth = dPixels[y * dwidth + x];

if (depth < avgDepth) {

avgDepth--;

} else if (depth > avgDepth) {

avgDepth++;

}

}

}

image.ReleaseAccess(imageData);

render(this, new RenderFrameEventArgs(0, image));

SetStatus(avgDepth.ToString());

}

That is from about line 123 in raw_streams.cs. Given this as a starting point, how would I also draw on top of the image being rendered in the most memory and CPU efficient way possible? Ideally I'd love to be able to use System.Drawing.Graphics, like this where image is a Bitmap:

using (Graphics graphics = Graphics.FromImage(image))

{

graphics.FillRectangle(new SolidBrush(Color.Black),

Width / 2 - 150,

Height / 2 - 21,

300,

41);

}

How could I get from what I currently have, to also being able to efficiently draw on the rendered depth image?

0 Kudos
3 Replies
idata
Employee
173 Views

Hello David__

 

 

Thanks for contacting Intel customer support.

 

 

Unfortunately this topic is out of our scope of support, since you will need to use a 3rd party code to draw on the image.

 

 

As a best effort I can share with you an example of how to extract the bitmap data from the PXCMImage.

 

 

https://software.intel.com/sites/landingpage/realsense/camera-sdk/v1.1/documentation/html/index.html?doc_essential_image_data.html

 

 

Thanks for your understanding.

 

 

Best Regards,

 

Juan N.
0 Kudos
DNone
Beginner
173 Views

Actually I do still have questions that are directly about the SDK. It looks like the the example you linked to makes a copy of the image. I want to lock the image, get a pointer to it, edit it, unlock it and wait for the next frame. I can *almost* do that (see the example below) but image needs to be a System.Drawing.Bitmap instead of a PXCMImage.ImageData object. Is there a way to convert it to a System.Drawing.Bitmap without copying it? It exists in memory at some address somewhere. I just want a pointer to the start of the frame in memory. How would I get a pointer to the start of the PXCMImage.ImageData image, or convert it to a Bitmap?

imageData = image.LockBits(rect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);

short* currFrame = (short*)imageData.Scan0.ToPointer();

// do stuff

image.UnlockBits(imageData);

Another variation would the simplified code below. Would this be the most efficient way to get access to the image?

image.AcquireAccess(PXCMImage.Access.ACCESS_READ_WRITE, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out imageData);

IntPtr imagePtr = imageData.planes[0];

unsafe {

ushort* currFrame = (ushort*)imagePtr.ToPointer();

}

0 Kudos
idata
Employee
173 Views
0 Kudos
Reply