我刚刚开始在机器人控制器上工作,但是我还没有得到相机部分的工作。我想通过tcp从服务器接收网络摄像头流,该服务器发送Mat[s],然后在客户端显示此信息。
服务器发送错误的信息(可能是强制转换的结果),或者客户端错误地解释了该信息(也可能是强制转换的结果)。不管怎样,我不知道如何解决这个问题。
到目前为止,我的server.cpp看起来像这样:#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
using namespace cv;
int main(int argc, char** argv)
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 1111 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
VideoCapture cap;
// open the default camera, use something different from 0 otherwise;
// Check VideoCapture documentation.
if(!cap.open(0))
return 0;
for(;;)
{
Mat frame;
cap >> frame;
if( frame.empty() ) break; // end of video stream
//imshow("Input :)", frame);
write(client_sock,(char *)frame.data,strlen((char *)frame.data));
if( waitKey(1) == 27 ) break; // stop capturing by pressing ESC
}
// the camera will be closed automatically upon exit
// cap.close();
return 0;
}
我的客户是这样的:
#include "opencv2/opencv.hpp"
#include <SFML/Network.hpp>
using namespace cv;
int main() {
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("127.0.0.1", 1111);
if (status != sf::Socket::Done)
{
puts("Byen");
return 0;
}
while(true) {
Mat frame;
char data[1000000]; //strlen gave me around 930000 when I was debugging.
std::size_t received;
// TCP socket:
if (socket.receive(data, 1000000, received) != sf::Socket::Done)
{
continue;
}else {
frame.data = (unsigned char *)data;
imshow("Hello",frame); //Error occurs here, not sure what causes it
}
}
}
误差OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp, line 269
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow
Aborted
我猜这是因为你只发送Mat
的数据。Mat
对象包含header,其中包括Mat
的大小。
我建议至少也传输Mat的Size
和Type
,然后用它来创建一个新的Mat
Mat frame(receivedSize, receivedType)
我不确定这是否足够,但这是一个开始。
另外,是否有理由只发送data
而不是(char *)&frame
,然后将其转换回Mat
?