JPG压缩后的原始BGR图像长度相同?
Posted
技术标签:
【中文标题】JPG压缩后的原始BGR图像长度相同?【英文标题】:Raw BGR image after JPG compression is the same length? 【发布时间】:2010-11-23 10:10:32 【问题描述】:好的。这有点奇怪。长话短说。我正在从相机中提取原始 BGR 图像,使用 OpenCV 将它们压缩为 JPG 并使用 UDP 协议发送到 PC。这就是我压缩图像的方式:
// 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);
这就是我使用 UDP 发送它们的方式:
n_sent = sendto(sk,&*buf.begin(),(int)size,0,(struct sockaddr*) &server,sizeof(server));
这是我在 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;
我得到这个输出:
Received 57600 bytes from the client
Received 57600 bytes from the client
...
如果我在从相机获取图像的程序中删除 JPG 压缩,输出是相同的:
Received 57600 bytes from the client
Received 57600 bytes from the client
...
但是,当我将接收到的图像保存在磁盘上时,它的大小约为 7.8KB,而保存到磁盘的未压缩原始图像大约需要 57KB 空间。
这是怎么回事?
【问题讨论】:
【参考方案1】:您传递给发送的“大小”是压缩缓冲区的大小,对吗?从您的代码 sn-ps 中,“size”的来源并不明显(正如 ypnos 所暗示的,我会预料到 buf.size() )。
【讨论】:
哦,对了。 size 包含原始图像的大小。顺便问一下,如何找到vector发送数据包时不要使用 buf.size()。因此,您发送的数据多于 buf 中实际包含的数据。在某些情况下,你会得到一个段错误。
【讨论】:
以上是关于JPG压缩后的原始BGR图像长度相同?的主要内容,如果未能解决你的问题,请参考以下文章
ComicEnhancerPro 系列教程十八:JPG文件长度与质量