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.
This is because cv2. imwrite() has the ability to compress the image while writing it into the file.
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).
Reading and Displaying an image in OpenCV using C++ It loads the image in BGR format.
As suggested, activating C++0x allows me to pass a vector explicitly defined inline to the function. This solved the issue.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With