【发布时间】:2015-12-23 07:57:07
【问题描述】:
我有一个 EmguCV.Image<Brg, Byte>() 实例和另一个第 3 方 API,它需要代表图像数据的 Stream。
我能够将 EmguCV 图像转换为流并使用 System.Drawing.Bitmap.Save 将其传递给第 3 方 API,但这不是很有效。
如何尽可能高效地获取流?
此代码有效:
var image = new Image<Rgb, byte>("photo.jpg");
var bitmap = image.Bitmap// System.Drawing - this takes 108ms!
using (var ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Bmp); //not very efficient neither
ms.Position = 0;
return ImageUtils.load(ms); //the 3rd party API
}
我尝试直接从图片创建UnmanagedMemoryStream:
byte* pointer = (byte*)image.Ptr.ToPointer();
int length = image.Height*image.Width*3;
var unmanagedMemoryStream = new UnmanagedMemoryStream(pointer, length);
但是当我尝试从中读取时,它会抛出 AccessViolationException: Attempted to read or write protected memory。
for (int i = 0; i < length; i++)
{
//throw AccessViolationException at random interation, e.g i==82240, 79936, etc
unmanagedMemoryStream.ReadByte();
}
在这种情况下长度为 90419328,应该是正确的,因为它与 image.ManagedArray.Length 具有相同的值;
如何在不复制数据的情况下获取流?
【问题讨论】:
标签: opencv pointers stream unmanaged emgucv