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
Comments
new Mat(byte[]) works
|
@saudet And Mat to byte[] ? |
Mat.data().get(byte[]), pretty much the same as the C++ API.
|
@saudet But |
@saudet I mean, the image of 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 ? |
There's currently no helper for arrays of arrays, so yes, we need to do it manually. |
Btw, the 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 |
This is different from opencv's API. I'm not familiar with it.
The text was updated successfully, but these errors were encountered: