将WriteableBitmap转换为位图以在EmguCV中使用

7

在我的代码中,我从字节数组(由Kinect提供)接收WriteableBitmap并希望将其转换为EmguCV可用的位图。目前这是我拥有的代码:

                // 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));

我从这里得到了代码的下半部分。代码编译一切正常,但在尝试运行程序时却会停顿(它应该对图像执行一些操作,然后将其转换回可呈现为图像的格式)。我暂停我的代码,然后在VS 2013中使用IntelliTrace,我得到了以下异常:Image<Gray, Byte> img = new Image<Gray, Byte>(b);“引发了System.ArgumentException:不支持URI格式。”使用备用代码,直接从字节到位图会给我带来同样的错误。(可以在这里找到代码。
有人有解决此错误或转换为位图的替代方法的提示吗?我是C#和EmguCV的新手,非常感激。
2个回答

0

我遇到了同样的问题。"URI 格式不受支持" 的异常与位图无关,而是与加载所需的 opencv dll 文件有关。我只需将包括 opencv_core290.dll 和其他文件的 x86 和 x64 文件夹复制到我的可执行目录即可。


实际上,这是一个针对许多不同错误抛出的通用错误语句。对于我的情况,它是位图格式,但它也会发生在不正确的 DLL 加载中,就像你的情况一样。 - Gaessaki

0
原来所有的代码都没问题。我对错误的技术细节不太确定,但是当尝试在WriteableBitmap中写入Gray16图像(该图像将被转换为Emgu图像)时会收到错误。Bgr565或其他格式受支持,我相信MS没有完全实现Gray16。如果进行WinForms应用程序,则Format16bppGray也会出现相同的错误。
我决定在将位图写为Bgr555时使用灰色Emgu图像,这样会更嘈杂,但总比没有好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接