gpt4 book ai didi

c++ - 使用套接字发送 Iplimage

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:52:30 25 4
gpt4 key购买 nike

我正在尝试从通过 USB 连接到 Raspberry Pi 的罗技高清摄像头捕获帧,RP 正在运行 arch linux,我正在使用 OpenCV C api 和 TCP 客户端。

TCP服务器在ubuntu下运行c++(QT)

我将 frame->imageData 存储在缓冲区中,然后将数据发送到缓冲区中。

这是一个概念证明,我可以将一个帧保存到缓冲区,然后从缓冲区重建一个新帧,它是 tmpbuffer,实际上这段代码运行完美:

CvCapture *capture = cvCaptureFromCAM(1);
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
int i =1;

while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
CvSize size;
size.height = 480;
size.width = 640;
IplImage* tmpframe = cvCreateImageHeader(size, IPL_DEPTH_8U, 3); //create a new frame
if ( !frame ) {
//fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}else
{
//std::cout<<i<<std::endl;
char buffer[frame->imageSize];
snprintf(buffer,frame->imageSize,"%s",frame->imageData);
//printf("frame->height= %s\n",buffer);

tmpframe->imageData = buffer;
printf("data %s\n",tmpframe->imageData);

//snprintf(IDbuffer,10,"%d",frame->ID);
//printf("frame->ID= %s\n",IDbuffer);
i++;
}
cvShowImage( "mywindow", tmpframe );

这是我的 client.c 文件:

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc,char *argv[])
{

int sockfd,portno,n;
struct sockaddr_in serv_addr;
struct hostent *server;


if(argc <3)
{
fprintf(stderr,"usage %s hostname portname port\n",argv[0]);
exit(0);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET , SOCK_STREAM,0);

if(sockfd < 0)
{
error("ERROR OPENING SOCKET");
}

server = gethostbyname(argv[1]);
if(server == NULL)
{
fprintf(stderr,"ERROR,NO SUCH HOST\n");
exit(0);
}

bzero((char*)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;

bcopy((char*)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);

if(connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR CONNECTING");
}
CvCapture *capture = cvCaptureFromCAM(1);
// Show the image captured from the camera in the window and repeat
int i =1;

while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
//fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}else
{
i++;
}

char *msg = frame->imageData;
char buffer[frame->imageSize];
bzero (buffer,frame->imageSize);
snprintf(buffer,frame->imageSize,"%s",msg);
n=write(sockfd,buffer,frame->imageSize);
if(n <0)
{
error("ERROR READING FROM SOCKET");
}
//printf("%s\n",buffer);


}
return 0;
}

这就是我在服务器上接收数据的方式:

void HostConnector::readyRead()
{
QByteArray Data = socket->readAll();

CvSize size;
size.height = 480;
size.width = 640;
IplImage *frame = cvCreateImageHeader(size, IPL_DEPTH_8U, 3);


frame->imageData = Data.data();
cvShowImage( "mywindow", frame );

}

但是服务器崩溃了!!段错误

cvShowImage( "mywindow", frame );

最佳答案

我在没有仔细检查代码的情况下看到的两个问题:

  • 您正在使用 printf(3) 风格的函数来格式化二进制数据。那是错误的TM。生成的字符串的长度将与图像的长度不同,二进制数据可能在中间有零字节,或者没有零终止符。
  • 您正在其范围之外使用临时缓冲区 (char buffer[frame->imageSize]) - 这可能是段错误的原因。

关于c++ - 使用套接字发送 Iplimage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com