Ask Your Question

Revision history [back]

C: Sending IplImage over TCP

Hello everyone

I'm creating a simple client/server socket sample program that has the following structure: The client sends the server the image filepath, then the server loads the Image from the drive (so far so good) and is supposed to send the IplImage back to the client to be processed.

I'm new to openCV and socket programming (honestly, to all programming in general), so I am having an hard time doing this. I am not sure how to send the data, so I decided i would send it row by row.

I'll post my code (which doesn't work). Please bear with it and don't be mad :P

Server side:

int rows = Image->height, cols = Image->width;

  snd = write(sock, &cols, sizeof(&cols));
  if(snd < 0) myerror("ERROR writing to socket");

  snd = write(sock, &rows, sizeof(&rows));
  if(snd < 0) myerror("ERROR writing to socket");


  char  RowVals[cols];
  bzero(RowVals,sizeof(RowVals));
  for (y=0; y<rows; y++)
  {

    char *ptr = (char *) (Image->imageData + y * (Image->widthStep));

    for (x=0; x<cols;x++)
    {
      RowVals[x]=ptr[x];

    }

    snd = write(sock, RowVals, sizeof(RowVals));     //SEND IMAGEDATA ROW BY ROW. client has to decode the information
    if(snd < 0) myerror("ERROR writing to socket");
    bzero(RowVals,cols);
  }

In the client side, I'm hoping to receive this information and use it to create the IplImage, but I am out of ideas of how to do that. Here is what I have so far.

rcv = read(sockfd,cols,sizeof(int*));

    if (rcv < 0) myerror("ERROR reading from socket");

    rcv = read(sockfd,rows,sizeof(int*));

    if (rcv < 0) myerror("ERROR reading from socket");

    int buffer_img[cols[0]];

// printf("%d, %d\n",rows[0],cols[0]); This code prints out the correct size.

    for (k=0; k<rows[0]; k++)
    {

      rcv = read(sockfd,buffer_img,sizeof(buffer_img));
      if (rcv < 0) myerror("ERROR reading from socket");

     }

I really need help to make this work.

Appreciated!

Regards