Ask Your Question
0

Error sending Iplimage using TCP

asked 2013-01-05 17:20:02 -0500

Ahmed Kato gravatar image

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.

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

here is my client.c code

#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;
    char buffer[999999];

    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);
       // capture from cam
        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++;
         }
         bzero (buffer,999999);
         strcpy(buffer,frame->imageData);
    n=write(sockfd,buffer,strlen(buffer));
    if(n <0)
    {
        error("ERROR READING FROM SOCKET");
    }
    //printf("%s\n",buffer);


    }
return 0;
}

This is how I receive data at the server:

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

    IplImage* frame = new IplImage();
    frame->imageData = Data.data();

    cvShowImage( "mywindow", frame ); //show the frame in a window

}

But I am receiving this error:

OpenCV Error: Bad flag (parameter or structure field) (unrecognized or unusupported array type) in cvGetMat, file /home/kato/GP/src/OpenCV-2.4.2/modules/core/src/array.cpp,line 2482

Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt.You must reimplement QApplication::notify() and catch all exceptions there

terminate called after throwing an instance of 'cv::Exception',error:(-206) Unrecognized or unsupported array type in function cvGetMat.

Does anyone knows how to solve this issue ??

Thanks in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-02-06 04:30:30 -0500

berak gravatar image

updated 2013-02-06 04:48:32 -0500

  1. you can't use strcpy (or strlen), when writing the bytes, it will stop at the 1st zero byte. use memcpy instead, img.width * img.height * img.nChannels() for the counting

  2. when reading back in, be prepared, that your packets will be fragmented into small pieces, so you can't read it all in one go. check how many bytes you read, and repeat till done.

  3. the iplimage to hold the receiving data must be constucted with correct width,height,type. just assigning the data is not enough

edit flag offensive delete link more
-1

answered 2013-01-05 23:54:50 -0500

Haris gravatar image

You can also try packet streaming of image data using client-server model using socket programming. For this save your image to hard-disc then you can read image data packet by packet using file operator fread in C/C++. Send each packet to the server where you can save your image using fwrite with the same extension as you read. I refered this site for implementing the client and server.

Hope this is helpful.

edit flag offensive delete link more

Comments

I don't want to use the hardisk , this will cost me a very high time complexity.

Ahmed Kato gravatar imageAhmed Kato ( 2013-01-06 02:20:12 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-01-05 17:20:02 -0500

Seen: 1,692 times

Last updated: Feb 06 '13