C++ OpenCV image sending through socket

久未见 提交于 2019-11-27 07:01:31
Haris

Get Mat.data and directly send to the socket, the data order is BGR BGR BGR.... On the receiving side assumes you know the size of image. After receiving just assign the received buffer(BGR BGR... array) to a Mat.

Client:-

Mat frame;
frame = (frame.reshape(0,1)); // to make it continuous

int  imgSize = frame.total()*frame.elemSize();

// Send data here
bytes = send(clientSock, frame.data, imgSize, 0))

Server:-

Mat  img = Mat::zeros( height,width, CV_8UC3);
int  imgSize = img.total()*img.elemSize();
uchar sockData[imgSize];

//Receive data here

for (int i = 0; i < imgSize; i += bytes) {
    if ((bytes = recv(connectSock, sockData +i, imgSize  - i, 0)) == -1)  {
        quit("recv failed", 1);
    }
}

// Assign pixel value to img
int ptr=0;
for (int i = 0;  i < img.rows; i++) {
    for (int j = 0; j < img.cols; j++) {                                     
       img.at<cv::Vec3b>(i,j) = cv::Vec3b(sockData[ptr+ 0],sockData[ptr+1],sockData[ptr+2]);
       ptr=ptr+3;
    }
}
user3165224
bytes = send(clientSock, frame.data, imgSize, 0));

This statement is giving error in Visual Studio 12.It say that.. ERROR: argument of type "uchar *" is incompatible with parameter of type "const char *" at ""frame.data"".

Buffer overflow. And I am not sure that format and sizes of image are the same -> you should serialize your Mat honestly, something like here.

Haris's answer is right and worked for me. However, instead of that server code (constructing image from uchar data), you can also use the shorter form:

int  imgSize = img.total()*img.elemSize();
uchar sockData[imgSize];

for (int i = 0; i < imgSize; i += bytes) {
    if ((bytes = recv(connectSock, sockData +i, imgSize  - i, 0)) == -1)
        quit("recv failed", 1);
}

// change the last loop to below statement
Mat img(Size(HEIGHT, WIDTH), CV_8UC3, socketData);

In Microsoft Visual Studio 2012, I have a red line under imgSize in sockData[imgSize] saying expression must have a constant value. I then changed int imgSize to const int imgSize. It still shows me the same problem.

 Mat img = Mat::zeros(100,100, CV_8UC3);
 const int imgSize = img.total()*img.elemSize();
 uchar sockData[imgSize];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!