在 OpenCV 中使用 libjpeg 将 IplImage 压缩为 JPEG
所以我有这个问题。 我有一个 IplImage,我想将其压缩为 JPEG 并用它做一些事情。我使用 libjpeg。 我找到了很多答案“阅读示例和文档”等等,然后就这样做了。并成功为此编写了一个函数。
FILE* convert2jpeg(IplImage* frame)
{
FILE* outstream = NULL;
outstream=malloc(frame->imageSize*frame->nChannels*sizeof(char))
unsigned char *outdata = (uchar *) frame->imageData;
struct jpeg_error_mgr jerr;
struct jpeg_compress_struct cinfo;
int row_stride;
JSAMPROW row_ptr[1];
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outstream);
cinfo.image_width = frame->width;
cinfo.image_height = frame->height;
cinfo.input_components = frame->nChannels;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
row_stride = frame->width * frame->nChannels;
while (cinfo.next_scanline < cinfo.image_height) {
row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return outstream;
}
现在这个函数直接来自示例(除了分配内存的部分,但我需要它,因为我没有写入文件),但它仍然不起作用。 它在 jpeg_start_compress(&cinfo, TRUE); 部分死亡?
有人可以帮忙吗?
So I have this problem.
I have an IplImage that i want to compress to JPEG and do something with it. I use libjpeg.
I found a lot of answers "read through examples and docs" and such and did that. And successfully written a function for that.
FILE* convert2jpeg(IplImage* frame)
{
FILE* outstream = NULL;
outstream=malloc(frame->imageSize*frame->nChannels*sizeof(char))
unsigned char *outdata = (uchar *) frame->imageData;
struct jpeg_error_mgr jerr;
struct jpeg_compress_struct cinfo;
int row_stride;
JSAMPROW row_ptr[1];
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outstream);
cinfo.image_width = frame->width;
cinfo.image_height = frame->height;
cinfo.input_components = frame->nChannels;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
row_stride = frame->width * frame->nChannels;
while (cinfo.next_scanline < cinfo.image_height) {
row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return outstream;
}
Now this function is straight from the examples (except the part of allocating memory, but i need that since i'm not writnig to a file), but it still doesn't work.
It dies on jpeg_start_compress(&cinfo, TRUE); part?
Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经能够使用他们网站上提供的最新 jpeglib 找到解决方案。
新方法:jpeg_mem_dest(&cinfo, outbuffer, outlen);
I've been able to found a solution using the latest jpeglib available on their website.
New methods in : jpeg_mem_dest(&cinfo, outbuffer, outlen);
我使用以下代码进行内存到内存压缩使其工作。
在你的主压缩函数中,将 jpeg_stdio_dest 替换为
150000 是一个静态大小的缓冲区,你可能会有超过它的图像,因此进行相应的分配。
I got it to work using the following code for memory to memory compression.
And in your main compression function replace the jpeg_stdio_dest with
The 150000 is a static size buffer, you probably will have images that will exceed it, so allocate accordingly.
我让内存压缩发挥作用。请参阅以下内容
要使用此功能,请在 main 中执行类似的操作
I got in-memory compression to work. See the following
To use this do something like this in the main
在与 libJpeg 斗争了 2 天(指针、内存步进和拉扯头发)之后,我放弃了并使用了最喜欢的保存到磁盘加载到内存的方法,所以如果有人感兴趣的话,这里是方法:
我希望有人发现这很有用。我希望 OpenCV 开发人员中的某个人看到这个线程并在 OpenCV 中实现直接到缓冲区 JPEG 转换,从而避免我们的痛苦和 1 次保存/加载到磁盘操作。
After fighting with libJpeg for 2 days (pointers, memory stepping, and pulling hair) I gave up and used the all favourite save-to-disk-load-to-memory approach, so if anybody is interested here is the method:
I hope somebody finds this usefull. I wish somebody from OpenCV developers sees this thread and implements direct to buffer JPEG conversion in OpenCV and spares us the misery and 1 save/load to disk operation.