- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Tags:
- Memory
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
the bug is that windows uses BGR despite calling it often RGB
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page