使用套接字发送Iplimage [英] sending Iplimage using sockets

查看:108
本文介绍了使用套接字发送Iplimage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从通过USB连接到Raspberry Pi的Logitech HD凸轮捕获帧,RP正在运行arch linux,并且我正在使用OpenCV C api和TCP客户端.

I am trying to capture frames from a Logitech HD cam connected to a Raspberry Pi through usb, the RP is running arch linux and I am using OpenCV C api and a TCP client.

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

The TCP server is running c++(QT) under ubuntu.

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

I am storing the frame->imageData in a buffer and then send the data in the buffer.

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

this is a proof of concept that I can save a frame to a buffer then reconstruct a new frame which is tmpbuffer from the buffer,actually this code is running perfectly:

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文件:

Here is my client.c file:

#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;
}

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

and this is how I am receiving data on my server:

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 );

}

但是服务器崩溃了! 一个segmentation fault at

But the server is crashing !! a segmentation fault at

cvShowImage( "mywindow", frame );

推荐答案

我没有真正仔细检查代码就看到了两个问题:

Two issues I see without real close examination of the code:

  • 您正在使用printf(3)样式的函数来格式化二进制数据.这是错误的 TM .结果字符串的长度将与图像的长度不同,并且二进制数据的中间可能有零字节,或者没有零终止符.
  • 您正在使用超出其范围的临时缓冲区(char buffer[frame->imageSize])-这可能是分段错误的原因.
  • You are using printf(3)-style functions to format binary data. That is wrongTM. The length of the resulting string would not be the same as the length of the image, and binary data might have zero bytes in the middle, or not have zero terminator.
  • You are using temporary buffer (char buffer[frame->imageSize]) outside of its scope - that is probably the cause of the segmentation fault.

这篇关于使用套接字发送Iplimage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆