从png字节获取EMGU图像



我正在尝试从png字节中获取Image
在以下示例中,我可以从文件加载png,但不能从文件的字节加载:

//using Emgu.CV;
//using Emgu.CV.Structure;
var path = @"C:pathtomy.png";    
// Using the constructor with the file path works great!
var imageFromFile = new Image<Rgb, byte>(path); 

var bytes = File.ReadAllBytes(path);
var size = imageFromFile.Size;
var imageFromBytes = new Image<Rgb, byte>(size);
// Assigning the bytes ails with an 'ArgumentOutOfRangeException' exception
imageFromBytes.Bytes = bytes; 

这实际上是有道理的,因为imageFromBytes.Bytes期望的数组大小为:

size.Width * size.Height * 3

而字节数组具有压缩的png图像的大小
所以,我的问题是-给定一个png字节数组,如何获得这些字节的Image实例?

在Emgu github repo中翻一翻后,下面将从字节数组中获取图像
不太OOP,但有效。。。

var imageFromBytes = new Image<Rgb, byte>(imageFromFile.Size);
CvInvoke.Imdecode(bytes, Emgu.CV.CvEnum.ImreadModes.AnyColor, imageFromBytes.Mat);
// Now imageFromBytes holds the uncompressed bytes.

最新更新