Bertie
Bertie

Reputation: 17697

How to create Mat from file bytes

I notice that Highgui.imread("h:/opencv.jpg") will automatically create the Mat instance with the correct type through auto-detection regardless of the file extensions.

Now what i have is a byte array uploaded from network.
Is it possible to create the correct Mat object without having to manually construct it with the row, col and type ?
I think this should be doable since imread can read the file bytes and detect the type, why not with the file bytes that i can pass ?

Upvotes: 0

Views: 337

Answers (1)

berak
berak

Reputation: 39806

if your bytes are actual pixels :

Mat m = new Mat(h,w,type);
m.put(0,0,bytes);

if it is an encoded image (like 'on-disk'), headers and all:

Mat m = Highgui.imdecode(new MatOfBytes(bytes), 1);

docs

Upvotes: 1

Related Questions