Dynamically change Mat header values? [closed]

asked 2016-12-08 08:06:14 -0600

builer39393 gravatar image

updated 2016-12-08 08:24:16 -0600

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/tutori...

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

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

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2016-12-08 08:59:45.209910

Comments

you can send large packets in one go, but on the receiving side, you have to read small blocks, until you got the whole buffer. (packet fragmentation)

(also, remember, that udp is restricted to 64k per packet)

berak gravatar imageberak ( 2016-12-08 08:12:57 -0600 )edit

Thanks for the response, but I'm not using UDP and everything is being received as sent, and has been verified in a debugger. I just need to know how to change Mat header values.

builer39393 gravatar imagebuiler39393 ( 2016-12-08 08:16:40 -0600 )edit

ok, sorry, seems, i did not read it properly

you should use CV_8UC3, not CV_8UC1

berak gravatar imageberak ( 2016-12-08 08:21:06 -0600 )edit

Yes I just read that in the documentation right as you posted it, thank you for your help.

builer39393 gravatar imagebuiler39393 ( 2016-12-08 08:24:49 -0600 )edit