Items with no label
告知
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
3338 ディスカッション

Video frame to bitmap

idata
従業員
7,295件の閲覧回数

Hi,

I'm now starting to use the d435 camera.

I know how to convert a video frame to BitmapSource but is there any function to convert a video frame to bitmap on C# ?

If not how should I approach this? I tried by doing the follow but with no success.

var bytes = new byte[frame.Stride * frame.Height];

frame.CopyTo(bytes);

Bitmap bmpRGB = null;

unsafe {

fixed (byte* ptr = bytes)

{

using (Bitmap image = new Bitmap(frame.Width, frame.Height, frame.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(ptr)))

{

bmpRGB = image;

}

}

}

return bmpRGB;

Thanks,

Vinicius

0 件の賞賛
10 返答(返信)
MartyG
名誉コントリビューター III
4,730件の閲覧回数

Have you considered the 'Save-To-Disk' SDK 2.0 sample program that saves stream data as a PNG image file?

https://github.com/IntelRealSense/librealsense/tree/master/examples/save-to-disk librealsense/examples/save-to-disk at master · IntelRealSense/librealsense · GitHub

jb455
高評価コントリビューター II
4,730件の閲覧回数

What's the problem with what you have? Do you get an error or corrupted image?

Does it work if you use frame.Data instead of new IntPtr(ptr)?

Though I think if you do it that way, the bitmap you create will have the same lifespan as the original frame so if the frame is released the bitmap will corrupt. You could try doing "return new Bitmap(bmpRGB)" if this is an issue.

idata
従業員
4,730件の閲覧回数

Hi,

The problem that I have know is that it throws me an exception.

I tried your suggestions.

In the attachments I send the project. Is the cs-tutorial-2-capture sample. Is the sample from Intel but I added the code to convert to bitmap and to display it as a bitmapSource.

My intention is to use the image as bitmap to process it using OpenCVSharp.

Thanks!

Vinícius

idata
従業員
4,730件の閲覧回数

Hello VCS_5,

 

 

It would be recommended for you to use this function for saving bitmaps: "int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);".

 

 

Here is an example on when this function is used:

 

https://github.com/IntelRealSense/librealsense/blob/165ae36b350ca950e4180dd6ca03ca6347bc6367/third-party/stb_image_write.h

 

 

Hope this function helps to clarify your problem.

 

 

Best Regards,

 

Juan N.
jb455
高評価コントリビューター II
4,730件の閲覧回数

This works for me:

private System.Drawing.Bitmap FrameToBitmap(VideoFrame frame, System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format32bppArgb)

{

if (frame.Width == 0) return null;

return new System.Drawing.Bitmap(frame.Width, frame.Height, frame.Stride, format, frame.Data);

}

Edit:

Just realised what was probably wrong with yours: the bitmap object you create is in a using block so it gets disposed of almost as soon as it's created!

idata
従業員
4,730件の閲覧回数

Hi,

I can't convert it back to bitmapSource. And when I try to convert to Mat I got this exception:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

I can't even debug it because by only running the project it consumes 100% of my CPU. I'm using the D435 camera.

Any further ideas?

Thanks,

Vinícius

jb455
高評価コントリビューター II
4,730件の閲覧回数

Is that when using my code example?

That error suggests the bitmap data is no longer available. Make sure you're not disposing or releasing the bitmap before you use it again, and don't put a variable you'll want to use later as the subject of a using() block.

What method are you using to convert the image to a mat?

idata
従業員
4,730件の閲覧回数

Hi,

Yes, I'm using your code see the project in the attachment.

To convert Bitmap to Mat I'm using a OpenCVSharp function.

var matImage = BitmapConverter.ToMat(image);

Yo run the project in the attachment you have to install OpenCVSharp 3.xxx via nugget.

Thanks,

Vinícius

jb455
高評価コントリビューター II
4,730件の閲覧回数

Ah, it's a pixelformat issue.

Change the format on line 43 to Format.Bgr8, then in the call on line 59 add ", System.Drawing.Imaging.PixelFormat.Format24bppRgb". (there's either a bug or difference in endianness which causes bgr and rgb to swap)

Should work now!

PBoos1
新規コントリビューター I
4,730件の閲覧回数

the bug is that windows uses BGR despite calling it often RGB

返信