Converting WriteableBitmap to Bitmap for use in EmguCV
Asked Answered
K

2

7

In my code, I'm receiving WriteableBitmaps from a byte array (in turn from a Kinect) and I'd like to turn them into bitmaps for use with EmguCV. Currently this is the code I have:

                // Copy the pixel data from the image to a temporary array
                colorFrame.CopyPixelDataTo(this.colorPixels);

                // Write the pixel data into our bitmap
                this.colorBitmap.WritePixels(
                    new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                    this.colorPixels,
                    this.colorBitmap.PixelWidth * colorFrame.BytesPerPixel,
                    0);

                    BitmapEncoder encoder = new BmpBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(colorBitmap));
                    MemoryStream ms = new MemoryStream();

                    encoder.Save(ms);
                    Bitmap b=new Bitmap(ms);

                    Image<Gray, Byte> img = new Image<Gray, Byte>(b);
                    img = img.ThresholdBinary(new Gray(200), new Gray(255));

I got the bottom half of the code from here.The code compiles and everything, but hangs when I'm trying to run the program (it's supposed to perform some operations on the image and then convert it back to a format that can be presented as an image.) Pausing my code and then using IntelliTrace in VS 2013, I get the following Exception at Image<Gray, Byte> img = new Image<Gray, Byte>(b); "A System.ArgumentException was thrown: URI formats are not supported." Using alternate code, from where I go directly from byte to bitmap gives me the same error. (Code can be found here.)

Anyone got tips on how to resolve this error, or alternate ways of casting to bitmap? I'm a newbie with C# & EmguCV and I'd greatly appreciate it.

Kosse answered 28/12, 2013 at 8:7 Comment(0)
K
0

Turns out all the code is fine. I'm not too sure on the technical details of the error, but the error is received when trying to write a Gray16 image in the WriteableBitmap (which is to be transformed into a Emgu Image.) Bgr565 or other formats are supported and I believe Gray16 wasn't fully implemented by MS. If doing a WinForms application, Format16bppGray will also give the same error.

I resolved to using a Gray Emgu image while writing the Bitmap as a Bgr555, which is a lot more noisy, but better than nothing.

Kosse answered 31/12, 2013 at 0:40 Comment(0)
I
0

I had the same issue. The exception with "URI formats are not supported" had nothing to do with the bitmap but with loading needed opencv dlls. I just copied the x86 and x64 folders including opencv_core290.dll and others to my executable directory.

Impassable answered 15/12, 2014 at 14:24 Comment(1)
It's actually a blanket error statement thrown for many different error. For my case it was the bitmap format, but it also occurs for incorrect dll loading, such as in your case.Kosse

© 2022 - 2024 — McMap. All rights reserved.