Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 440
Everaldo Aguiar Avatar asked Aug 29 '11 23:08

Everaldo Aguiar


People also ask

Does cv2 Imwrite compress?

This is because cv2. imwrite() has the ability to compress the image while writing it into the file.

What does CV Imwrite do?

imwrite() Saves an image to a specified file. The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see cv::imread for the list of extensions).

What format is the image read in using OpenCV in C++?

Reading and Displaying an image in OpenCV using C++ It loads the image in BGR format.


2 Answers

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

like image 100
Everaldo Aguiar Avatar answered Sep 25 '22 21:09

Everaldo Aguiar


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);
like image 39
Ryan Avatar answered Sep 25 '22 21:09

Ryan