Everaldo Aguiar
Everaldo Aguiar

Reputation: 4136

Compressing images on opencv (imwrite). How to explicitly set the compression factor?

I was wondering if there is a way to easily specify the compression factor when compressing images on opencv without having to declare a dummy vector. If I declare a vector p (similar to this discussion), but containing only 2 items, which is what imwrite takes, I can make the call:

vector<int> p(2);
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 50; // compression factor

imwrite("compressed.jpg", img, p);

The above works fine. However, I want to compress the same image with several compression factors in a loop. Is there a way to explicitly pass the parameter to imwrite? Something like:

imwrite("compressed.jpg", img, {CV_IMWRITE_JPEG_QUALITY, factor}); // this doesn't work

Just as a side note, the function header is:

bool imwrite(const string& filename, const Mat& img, const vector<int>& params=vector<int>());

Thanks!

Update: After activating C++0x, I can pass a vector explicitly defined inline to the function.

Upvotes: 8

Views: 14321

Answers (2)

Ryan
Ryan

Reputation: 11

vector<int> compression_params;
compression_params.push_back(IMWRITE_JPEG_QUALITY);
compression_params.push_back(30);
compression_params.push_back(IMWRITE_JPEG_PROGRESSIVE);
compression_params.push_back(1);
compression_params.push_back(IMWRITE_JPEG_OPTIMIZE);
compression_params.push_back(1);
compression_params.push_back(IMWRITE_JPEG_LUMA_QUALITY);
compression_params.push_back(30);
imwrite('sample.jpg', img, compression_params);

Upvotes: 1

Everaldo Aguiar
Everaldo Aguiar

Reputation: 4136

As suggested, activating C++0x allows me to pass a vector explicitly defined inline to the function. This solved the issue.

Upvotes: 3

Related Questions