Mat to byte array
I am trying to convert a Mat object to a byte array to transfer over a network. How do I achieve this? I am trying to do this in Java.
I am trying to convert a Mat object to a byte array to transfer over a network. How do I achieve this? I am trying to do this in Java.
If your need is to convert the data of the image, you don't need to do much. If function image.isContinuous() returns true (which is usually the case) then it is already byte array. You can access it by calling image.data. If image.isContinuous() returns false (for example you worked with ROI of image), you will need to move the data. Easiest way will be by calling image.clone().
As for the header of image, you don't need to send all of its fields. Value of reference counter for example will have no meaning for someone who received this file from the network. Send only the fields that will allow recipient to create Mat by himself. Those are width, height and type of the image.
Hi! I am trying to do the same conversion in java desktop with opencv 2.4.4. I want to transform the Mat data in byte array to create ImageIcon to display in JLabel. I tried this, but it doesn't work..
Mat image = new Mat(); VideoCapture camCapture = new VideoCapture(0); //then I read the capture from the camera camCapture.read(frame)
//I know that the type of mat is CV_8UC3 continuous so, I try to use that functions
public ImageIcon getImageIconFromMatTo(Mat frame){
ImageIcon retImage = null;
byte[] imageInBytes = new byte[(safeLongToInt(frame.total())) * frame.channels()];
frame.get(0, 0, imageInBytes);
retImage = new ImageIcon(imageInBytes);
return retImage;
}
public int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALU
help please!
sorry, I couldn't write all
public int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new IllegalArgumentException
(l + " cannot be cast to int without changing its value.");
}
return (int) l;
}
I didn't used Java for a very long time, and never used OpenCV for Java, so I can't give you a reliable answer. I recommend you to post new question that will be visible by other people (not just me). Don't forget to mention that you are talking about OpenCV for Java in body of your question and in tags of question. Otherwise people will think you are talking about C++.
This is how I did it: byte[] to Mat:
byte[] raw_data = ...;
Mat mat = new Mat();
mat.put(0, 0, raw_data);
Mat to byte[]:
byte[] return_buff = new byte[(int) (result_mat.total() *
result_mat.channels())];
result_mat.get(0, 0, return_buff);
This method is throwing an error: OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pranith/devops/code/opencv.git/opencv/modules/imgproc/src/color.cpp, line 3326 329809 [pool-2-thread-2] WARN org.apache.avro.ipc.Responder - user error
I get the byte value from mat object with this way, but while convert the byte value to bufferedimage an error occured. Then I get byte value from FileInputStream object and the mat to byte and file to byte results were different. I need to convert Mat to byte[] and then byte[] to BufferedImage. How can I solve the problem?
I solved my problem from http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
Not sure if there's a single function to do it but you have access to the following variables in cv::Mat
So rows * cols * elem_size should give you to the total bytes to send.
I use
Mat src; src.create(cv::Size(cols,rows),CV_8UC1); for (int i=0;i<rows;i++) {="" memcpy(src.ptr(i),srcdata+i*cols,cols);="" }<="" p="">
Althougth,it's not very smart.But it can work
Asked: 2012-11-30 14:28:13 -0600
Seen: 76,101 times
Last updated: Dec 05 '12
What is the most effective way to access cv::Mat elements in a loop?
Area of a single pixel object in OpenCV
build problems for android_binary_package - Eclipse Indigo, Ubuntu 12.04
OpenCV DescriptorMatcher matches
Can't compile .cu file when including opencv.hpp
Using OpenCV's stitching module, strange error when compositing images
compile error in opencv2/flann/lsh_table.h when compiling bgslibrary