Ask Your Question

Revision history [back]

Dynamically change Mat header values?

I'm trying to send a Mat over a network socket, something like this

Mat Img;
VideoCapure << Img;
send (socket, Img.data, img.total() * img.elemSize() );

Then on the receiving end

Mat img;
img = Mat::zeros(480 , 640, CV_8UC1);    
int imgSize = img.total() * img.elemSize();
recv (socket, img.data, imgSize, 0);

I can view the frames on the receiving end but they're distorted, and I understand that I am only sending Mat.data() without sending the header value for the Mat object. For this reason, on the sending end

img.elemSize() is 3
img.channels() is 3

however, on the recieving end

img.elemSize() is 1
img.channels() is 1

So my receiving end thinks this is a 1 channel image with an element size of 1, when in reality it is a 3 channel image with an elementSize of 3. Hence the distortion. However, being unfamiliar with opencv I cannot find a way to dynamically change these values, or initialize the constructors with these values.

Would you be able to show me the code to manually set the values of Mat.elemSize() & Mat.channels()?

Thanks.

Dynamically change Mat header values?

I'm trying to send a Mat over a network socket, something like this

Mat Img;
VideoCapure << Img;
send (socket, Img.data, img.total() * img.elemSize() );

Then on the receiving end

Mat img;
img = Mat::zeros(480 , 640, CV_8UC1);    
int imgSize = img.total() * img.elemSize();
recv (socket, img.data, imgSize, 0);

I can view the frames on the receiving end but they're distorted, and I understand that I am only sending Mat.data() without sending the header value for the Mat object. For this reason, on the sending end

img.elemSize() is 3
img.channels() is 3

however, on the recieving end

img.elemSize() is 1
img.channels() is 1

So my receiving end thinks this is a 1 channel image with an element size of 1, when in reality it is a 3 channel image with an elementSize of 3. Hence the distortion. However, being unfamiliar with opencv I cannot find a way to dynamically change these values, or initialize the constructors with these values.

Would you be able to show me the code to manually set the values of Mat.elemSize() & Mat.channels()?

Thanks.

EDIT - I figured it out, the channel/element size can be initialized by the last value in the following constructor, as shown in http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html

img = Mat::zeros(480 , 640, CV_8UC1);

All I had to do was change CV_8UC1 to CV_8UC3 : )