This is different from opencv's API. I'm not familiar with it.
new Mat(byte[]) works
@saudet And Mat to byte[] ?
Mat.data().get(byte[]), pretty much the same as the C++ API.
@saudet But new Mat(byte[]) not available
@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);
}
You never mentioned anything about an image. You'll need to clarify what
you want to do if you expect someone to help you.
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));
@saudet thanks very much
@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?
There's currently no helper for arrays of arrays, so yes, we need to do it manually.
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?
We need to consider the "step" or "stride", but just use an indexer, it will do that for you.
Ok, got it