首页 > 解决方案 > 从 png 字节获取 EMGU 图像

问题描述

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

//using Emgu.CV;
//using Emgu.CV.Structure;
var path = @"C:\path\to\my.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

而 bytes 数组具有压缩 png 图像的大小。
所以,我的问题是 - 给定一个 png 字节数组,我怎样才能获得这些字节的图像实例?

标签: c#.net-corecross-platformemgucv

解决方案


Emgu github repo中浏览了一下之后,下面将从字节数组中获取图像。
不是那么 OOP-ish,但可以工作......

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

推荐阅读