Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert between Mat and byte[] #888

Closed
blackredscarf opened this issue Jan 24, 2018 · 14 comments
Closed

How to convert between Mat and byte[] #888

blackredscarf opened this issue Jan 24, 2018 · 14 comments
Labels

Comments

@blackredscarf
Copy link

This is different from opencv's API. I'm not familiar with it.

@saudet
Copy link
Member

saudet commented Jan 24, 2018 via email

@blackredscarf
Copy link
Author

@saudet And Mat to byte[] ?

@saudet
Copy link
Member

saudet commented Jan 24, 2018 via email

@blackredscarf
Copy link
Author

@saudet But new Mat(byte[]) not available

@saudet
Copy link
Member

saudet commented Jan 24, 2018

@blackredscarf
Copy link
Author

blackredscarf commented Jan 24, 2018

@saudet I mean, the image of new Mat(byte[]) is not the same as the original image. It's a long gray picture.

this is my test code:

    public static byte[] Mat2Bytes(Mat mat){
        byte[] b = new byte[mat.channels() * mat.cols() * mat.rows()];
        mat.data().get(b);
        return b;
    }

    public static Mat Bytes2Mat(byte[] b){
        return new Mat(b);
    }
    public static void main(String[] args) throws IOException {
        Mat img = imread(getPath("test4.jpg"));

        byte[] b = Mat2Bytes(img);
        Mat img2 = Bytes2Mat(b);
        opencv_highgui.imshow("FacesOfImage", img2);
        opencv_highgui.waitKey(0);
    }

@saudet
Copy link
Member

saudet commented Jan 24, 2018 via email

@saudet
Copy link
Member

saudet commented Jan 29, 2018

An image has width, height, depth, and type so you'll also need to specify that, for example:

Mat img2 = new Mat(rows, cols, CV_8UC(channels), new BytePointer(b));

@blackredscarf
Copy link
Author

@saudet thanks very much

@tzolov
Copy link

tzolov commented Jul 31, 2018

@saudet, can i use a similar approach to initialize a Mat from a long[][] array ?
Or do i need to use the indexer and put it pixel by pixel?

@saudet
Copy link
Member

saudet commented Jul 31, 2018

There's currently no helper for arrays of arrays, so yes, we need to do it manually.

@tzolov
Copy link

tzolov commented Jul 31, 2018

Btw, the Mat.data().get(byte[]) seems to be skewing the image! So for the moment i'm using this workaround:

byte[] data = ((DataBufferByte) Java2DFrameUtils.toBufferedImage(mat).getRaster().getDataBuffer()).getData();

Maybe there are some additional image parameters i need to consider before transforming to bytes?

@saudet
Copy link
Member

saudet commented Jul 31, 2018

We need to consider the "step" or "stride", but just use an indexer, it will do that for you.

@tzolov
Copy link

tzolov commented Jul 31, 2018

Ok, got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants