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,275 조회수

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,710 조회수

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

0 포인트
jb455
소중한 기여자 II
4,710 조회수

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.

0 포인트
idata
직원
4,710 조회수

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

0 포인트
idata
직원
4,710 조회수

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.
0 포인트
jb455
소중한 기여자 II
4,710 조회수

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!

0 포인트
idata
직원
4,710 조회수

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

0 포인트
jb455
소중한 기여자 II
4,710 조회수

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?

0 포인트
idata
직원
4,710 조회수

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

0 포인트
jb455
소중한 기여자 II
4,710 조회수

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!

0 포인트
PBoos1
새로운 기여자 I
4,710 조회수

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

0 포인트
응답