C++ OpenCV cvSaveImage Jpeg 压缩因子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/801054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV cvSaveImage Jpeg Compression Factor
提问by The Unknown
I am using OpenCV and saving as a jpeg using the cvSaveImage function, but I am unable to find the Jpeg compression factor used by this.
我正在使用 OpenCV 并使用 cvSaveImage 函数保存为 jpeg,但我无法找到由此使用的 Jpeg 压缩因子。
- What's cvSaveImage(...)'s Jpeg Compression factor
- How can I pass the compression factor when using cvSaveImage(...)
- 什么是 cvSaveImage(...) 的 Jpeg 压缩因子
- 使用 cvSaveImage(...) 时如何传递压缩因子
回答by Lance Richardson
Currently cvSaveImage() is declared to take only two parameters:
目前 cvSaveImage() 被声明为只接受两个参数:
int cvSaveImage( const char* filename, const CvArr* image );
However, the "latest tested snapshot" has:
但是,“最新测试快照”具有:
#define CV_IMWRITE_JPEG_QUALITY 1
#define CV_IMWRITE_PNG_COMPRESSION 16
#define CV_IMWRITE_PXM_BINARY 32
/* save image to file */
CVAPI(int) cvSaveImage( const char* filename, const CvArr* image,
const int* params CV_DEFAULT(0) );
I've been unable to find any documentation, but my impression from poking through this code is that you would build an array of int values to pass in the third parameter:
我一直无法找到任何文档,但我通过浏览这段代码的印象是,您将构建一个 int 值数组来传递第三个参数:
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = desired_quality_value;
p[2] = 0;
I don't know how the quality value is encoded, and I've never tried this, so caveat emptor.
我不知道质量值是如何编码的,我从未尝试过,所以请注意空客。
Edit:
编辑:
Being a bit curious about this, I downloaded and built the latest trunk version of OpenCV, and was able to confirm the above via this bit of throwaway code:
对此有点好奇,我下载并构建了 OpenCV 的最新主干版本,并且能够通过这一点一次性代码确认上述内容:
#include "cv.h"
#include "highgui.h"
int main(int argc, char **argv)
{
int p[3];
IplImage *img = cvLoadImage("test.jpg");
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 10;
p[2] = 0;
cvSaveImage("out1.jpg", img, p);
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 100;
p[2] = 0;
cvSaveImage("out2.jpg", img, p);
exit(0);
}
My "test.jpg" was 2,054 KB, the created "out1.jpg" was 182 KB and "out2.jpg" was 4,009 KB.
我的“test.jpg”为 2,054 KB,创建的“out1.jpg”为 182 KB,“out2.jpg”为 4,009 KB。
Looks like you should be in good shape assuming you can use the latest code available from the Subversion repository.
假设您可以使用 Subversion 存储库中提供的最新代码,看起来您应该处于良好状态。
BTW, the range for the quality parameter is 0-100, default is 95.
顺便说一句,质量参数的范围是 0-100,默认是 95。
回答by Rick Smith
OpenCV now has a parameterto set jpeg quality. I'm not sure exactly when this was introduced, but presumably sometime after 2.0.
OpenCV 现在有一个参数来设置 jpeg 质量。我不确定这是什么时候引入的,但大概是在 2.0 之后的某个时候。
const int JPEG_QUALITY = 80;
Mat src;
// put data in src
vector<int> params;
params.push_back(CV_IMWRITE_JPEG_QUALITY);
params.push_back(JPEG_QUALITY);
imwrite("filename.jpg", src, params);
If you are using C++0x, you can use this shorter notation:
如果您使用的是 C++0x,则可以使用以下较短的表示法:
imwrite("filename.jpg", src, vector<int>({CV_IMWRITE_JPEG_QUALITY, JPEG_QUALITY});
回答by N PRABHU RAM prabhuramn4186ece
imwrite("filename.jpeg",src,(vector<int>){CV_IMWRITE_JPEG_QUALITY, 20});
filename.jpeg
will be output File namesrc
be source image read containing variable(vector<int>)
typecasting{CV_IMWRITE_JPEG_QUALITY, 20}
an array of elements to be passed as Param_ID - and Param_value in imwrite function
filename.jpeg
将输出文件名src
是包含变量的源图像读取(vector<int>)
类型转换{CV_IMWRITE_JPEG_QUALITY, 20}
要作为 Param_ID 传递的元素数组 - 和 imwrite 函数中的 Param_value
回答by Hannes Ovrén
- You can probably find this by poking around in the source code here: http://opencvlibrary.svn.sourceforge.net/viewvc/opencvlibrary/
- You can't, as the function does not accept such a parameter. If you want to control the compression then the simplest method I can think of is first saving your image as a bitmap with cvSaveImage() (or another lossless format of your choice) and then use another image library to convert it to a JPEG of the desired compression factor.
- 您可以通过在此处查看源代码来找到它:http: //opencvlibrary.svn.sourceforge.net/viewvc/opencvlibrary/
- 你不能,因为函数不接受这样的参数。如果您想控制压缩,那么我能想到的最简单的方法是首先使用 cvSaveImage() (或您选择的其他无损格式)将您的图像保存为位图,然后使用另一个图像库将其转换为 JPEG所需的压缩因子。