Avi Turner
Avi Turner

Reputation: 10456

Getting EMGU Image from png bytes

I'm trying get an Image from png bytes.
In the following example I am able to load the png from the file, but not from the file's bytes:

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

This actually makes sense, since imageFromBytes.Bytes expects an array with size of:

size.Width * size.Height * 3

while the bytes array has the size of the compressed png image.
So, my question is - Given a png byte array, how can I get an instance of an Image of those bytes?

Upvotes: 0

Views: 1248

Answers (1)

Avi Turner
Avi Turner

Reputation: 10456

After poking around a little in the Emgu github repo, the following will get the image from the byte array.
Not so OOP-ish, but works...

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

Upvotes: 3

Related Questions