python jpg 压缩因子_OpenCV cvSaveImage Jpeg压缩因子

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.

What's cvSaveImage(...)'s Jpeg Compression factor

How can I pass the compression factor when using cvSaveImage(...)

解决方案

Currently cvSaveImage() is declared to take only two parameters:

int cvSaveImage( const char* filename, const CvArr* image );

#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 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:

#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.

Looks like you should be in good shape assuming you can use the latest code available from the Subversion repository.

BTW, the range for the quality parameter is 0-100, default is 95.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值