Ask Your Question
0

cvDecodeImage - after streaming fails

asked 2014-03-20 02:04:55 -0600

Aruna Vijay gravatar image

updated 2014-03-20 04:21:05 -0600

Camera capture --> Encode -> stream (unsigned char )

-> Recieve -> Decode

Encoding side

unsigned char Buffer[1024*20];
IplImage *image   image=cvQueryFrame(Capture); 
   encode=cvEncodeImage(".jpg",image,encodeparams);

    decode=cvCreateMat(encode->rows,encode->cols,CV_8UC1);

decode->data.ptr=encode->data.ptr;

  cvShowImage("Server",image);
   cvWaitKey(1);
    Frame->iSync=0x1;
    Frame->iLength=(16+(encode->rows*encode->cols));
    Frame->iSize=(encode->rows*encode->cols);
    Frame->iWidth=(encode->rows);
    Frame->iHeight=(encode->cols);
        Frame->iSync=0x1;
    Frame->iLength=(16+(encode->rows*encode->cols));
    Frame->iSize=(encode->rows*encode->cols);
    Frame->iWidth=(encode->rows);
    Frame->iHeight=(encode->cols);

    memcpy(&bImageData[0],&Frame->iSync,sizeof(int));
    memcpy(&bImageData[4],&Frame->iLength,sizeof(int));

    memcpy(&bImageData[8],&Frame->iSize,sizeof(int));

    memcpy(&bImageData[12],&Frame->iWidth,sizeof(int));

    memcpy(&bImageData[16],&Frame->iHeight,sizeof(int));


memcpy(&bImageData[20],encode->data.ptr,sizeof(unsigned char)*encode->rows*encode->cols);

// printf("%d",length );
length = Frame->iLength;

    while( count < length)
{
        offset=send(connect,&bImageData[count],(length-count),0);

            if(offset > 1)
            {
                    count +=offset;
                  //  printf("\n Count &  %d %d \n",offset,count); 
            }
            if(offset == (-1))
            {
                printf("closed connection");
                goto _error_video_client; 
            }           
}
    recv(connect,RecvBuff,sizeof(RecvBuff),0); 

}

decoding side

define MAKEDWORD(B0,B1,B2,B3) (((unsigned long)B0)+

(((unsigned long)B1)<<8)+ (((unsigned long)B2)<<16)+ (((unsigned long)B3)<<24))

if(offset)
{
      if(size > PACKET_HEADER_SIZE )
      {   // using QT so this... to get the header info.
          iSync   = MAKEDWORD(CopyData.at(0),CopyData.at(1),CopyData.at(2),CopyData.at(3));
          iLength = MAKEDWORD(CopyData.at(4),CopyData.at(5),CopyData.at(6),CopyData.at(7));
          iSize   = MAKEDWORD(CopyData.at(8),CopyData.at(9),CopyData.at(10),CopyData.at(11));
          iRows  = MAKEDWORD(CopyData.at(12),CopyData.at(13),CopyData.at(14),CopyData.at(15));
          iCols =  MAKEDWORD(CopyData.at(16),CopyData.at(17),CopyData.at(18),CopyData.at(19));
          std::cout << "iLength " << iLength;
          std::cout << "isize " << iSize << "iWidth " << iRows  << " iHeight" << iCols;
          offset=0;
          if((iLength < 0) && iLength > FRAME_LENGTH )
                  offset=1;
          count=size;
          memcpy(&FrameBuffer[0],&CopyData,size);
          std::cout << "Size" << size;
       }


}
else
{   
    memcpy(&FrameBuffer[count],&CopyData,size); // keep until u reach the count == length 

    count+=size;
    if(count == iLength)
    {
         decode =cvCreateMat(iRows,iSize,CV_8UC1);
         decode->data.ptr=&FrameBuffer[20];
         frame = cvDecodeImage(decode,CV_LOAD_IMAGE_COLOR);
        if(frame)
           std::cout << "Valid Frame";
      else
       std::cout << "Not a Valid Frame "; // I am not aware why this is going invalid frame after sending all the trasnmission are using unsigned char buffer.

    }
}
edit retag flag offensive close merge delete

Comments

please use the '10101' button to format your code properly

berak gravatar imageberak ( 2014-03-20 04:21:57 -0600 )edit

honestly, that unformatted 'wall of code' is anything but helpful now.

and i bet a fiver, that all your problems vanish, if you move over to the c++ api instead

berak gravatar imageberak ( 2014-03-20 05:39:54 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-04-02 09:35:01 -0600

Aruna Vijay gravatar image

Thanks berak.

I finally did encoding and decoding using mat my code below the data is streamed using tcp Let me know if anyquestions. Encoding

#define JPEG_HEADER_0     0xff
#define JPEG_HEADER_1     0xd8

#define JPEG_END_2        0xff
#define JPEG_END_1        0xd9

Mat drawing; unsigned char VideoBuffer[1024*10] ; //ofcourse keep u r req size vector<int> param = vector<int>(2);

param[0]=CV_IMWRITE_JPEG_QUALITY;

param[1]=60;//default(95) 0-100

vector<uchar> encodeBuff;//buffer for coding
        imencode(".jpg",drawing,encodeBuff,param);      
        pthread_mutex_lock(&buffer_sync);
if(PacketSize > VIDEO_BUFFER_SIZE)
            printf("ERROR Packet Size --------> %d",PacketSize );
        PacketSize=encodeBuff.size();
        memcpy(&VideoBuffer[0],(char*)&encodeBuff[0],PacketSize);

Decoding

imageptr=&SendBuffer[4]; // ok i receive in sendbuffer of unsigned char

 if(  SendBuffer[4]== JPEG_HEADER_0 && 
                  SendBuffer[5] == JPEG_HEADER_1 && 
                          SendBuffer[length+3]== JPEG_END_1 &&
              SendBuffer[length+2] ==JPEG_END_2 )                 
             {

    std::vector<uchar> decodebuff(imageptr,imageptr+(size-4));
    Mat decode =imdecode(Mat(decodebuff),CV_LOAD_IMAGE_COLOR);
   }
edit flag offensive delete link more

Comments

Kindly excuse me. I find its 0101....take time.. some part are not getting formatted. I cut and pasted from gedit and gvim.

Aruna Vijay gravatar imageAruna Vijay ( 2014-04-02 09:37:14 -0600 )edit

oh sorry for being so unfriendly, did not realize being unfair there.

berak gravatar imageberak ( 2014-04-02 11:10:43 -0600 )edit

Question Tools

Stats

Asked: 2014-03-20 02:04:55 -0600

Seen: 1,981 times

Last updated: Apr 02 '14