방금 로봇 컨트롤러에 대한 작업을 시작했지만 카메라 부품이 작동하지 않습니다. 나는 Mat [s]를 보내는 서버로부터 tcp를 통해 웹캠 스트림을 수신하고,이 정보를 클라이언트 측에 표시하려고합니다.네트워크를 통해 opencv Mat을 전송하는 방법은 무엇입니까?
서버가 잠재적으로 캐스트의 결과로 잘못된 정보를 보내거나 클라이언트가이 정보를 잘못된 것으로 해석하거나 잠재적으로 잠재적으로 캐스팅의 결과로 발생할 수 있습니다. 어느 쪽이든이 문제를 해결하는 방법을 모르겠습니다.
#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;
}
그리고 내 클라이언트는 다음과 같습니다 :
지금까지 내 server.cpp은 다음과 같습니다
#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("Bye\n");
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
원시 데이터에서 'strlen'을 호출하지 마십시오. 첫 번째 '\ 0'문자 앞의 바이트 수를 반환합니다 (물론 끝내지 않을 수도 있습니다). 그것은 확실히 당신의 배열의 길이가 아닙니다 ... – Lanting