Raw BGR image after JPG compression is the same length?
Ok. This is a bit odd. Long story short. I am fething raw BGR images from a camera, compressing them to JPG with OpenCV and sending with UDP protocol to a PC. This is how I compress images:
// memblock contains raw image
IplImage* fIplImageHeader;
fIplImageHeader = cvCreateImageHeader(cvSize(160, 120), 8, 3);
fIplImageHeader->imageData = (char*) memblock;
// compress to JPG
vector<int> p;
p.push_back(CV_IMWRITE_JPEG_QUALITY);
p.push_back(75);
vector<开发者_如何学运维;unsigned char> buf;
cv::imencode(".jpg", fIplImageHeader, buf, p);
This is how I send them with UDP:
n_sent = sendto(sk,&*buf.begin(),(int)size,0,(struct sockaddr*) &server,sizeof(server));
This is how I receive them in a PC:
int iRcvdBytes=recvfrom(iSockFd,buff,bufferSize,0,
(struct sockaddr*)&cliAddr,(socklen_t*)&cliAddrLen);
// print how many bytes we have received
cout<<"Received "<<iRcvdBytes<<" bytes from the client"<<endl;
I am getting this output:
Received 57600 bytes from the client
Received 57600 bytes from the client
...
If I remove the JPG compression at the program fetching images from camera, the output is the same:
Received 57600 bytes from the client
Received 57600 bytes from the client
...
However, when I save the received image on a disk, it's size is around 7.8KB while uncompressed raw image saved to disk takes about 57KB space.
What's going on here?
The "size" you pass to send is the size of the compressed buffer, right? It's not obvious from your code snippets where "size" comes from (as ypnos suggests, I would have expected buf.size() ).
You don't use buf.size() when sending the packet. So you send more data than is actually contained in buf. In some cases you will get a segfault for that.
精彩评论