2

My C++ server sends a video stream from a computer's usb to a Java client.
In order to send an image(Mat object) from C++ I'm using FileStorage class to store the image in a string:

cv::FileStorage fs(".xml", cv::FileStorage::WRITE + cv::FileStorage::MEMORY);
fs << "mymatrix" << frame;
string buff = fs.releaseAndGetString(); //buff has all the Mat object info.

The client receives buff ,creates XML file and reads it using : Java: How to read and write xml files?

The image datatype is "3u" (=CV_8UC3), so I created a byte array to store the matrix data:

//data is a string that contains the matrix data

byte[] Bdata = data.getBytes();

and used this code to create the Mat object ( taken from https://groups.google.com/forum/#!topic/android-opencv/QEwhgO88ZwM ):

//Irows = number of rows
//Icols = number of columns
Mat mat = new Mat(Irows,Icols,CvType.CV_8UC3);
mat.put(0,0,Bdata);

but "mat.put.." is generating this error:

Exception in thread "main" java.lang.UnsupportedOperationException: Provided data element number (3742097) should be multiple of the Mat channels count (3)

I don't understand what is the problem with the byte array?
Any idea would be appreciated.

Edit : Though I'm not near my computer, so I can't confirm this, it appears that the large number in the error is the length of the array and it doesn't divide by 3 - the number of channels. I've no idea why.

Community
  • 1
  • 1
Eliran Koren
  • 187
  • 2
  • 10

1 Answers1

3

honestly, streaming uncompressed bytes (in xml even) over a network sounds like a horrible idea to me.

here's an alternative:

on the server side, encode to jpeg:

std::vector<uchar>outbuf;
std::vector<int> params;
params.push_back(cv::IMWRITE_JPEG_QUALITY);
params.push_back(100);
cv::imencode(".jpg", frame, outbuf, params);

int outlen = outbuf.size();
// send outlen bytes to client

on the client side, unpack:

// read into a MatOfBytes, until the jpeg end sequence ff d9 is discovered.
Mat img = Highgui.imdecode(bytes_buf, IMREAD_UNCHANGED) 
berak
  • 39,159
  • 9
  • 91
  • 89