For example, Photoshop software saves jpg images with low, medium, high, and best save option settings, and the corresponding size of the generated image is different. The image generated by OpenCV is relatively large and may be similar to the one saved by Photoshop, but I want a relatively small image, how can I use OPENCV to set the compression quality of JPG images.
The function Cvsaveimage (filename,img) in OpenCV is usually used to save the image, and it is rarely noted that it has a third parameter. The third parameter is used to adjust the quality of JPG image compression, it is a 3 elements of an array, the specific use of the following methods:
#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 (0));
int p[3];
P[0] = cv_imwrite_jpeg_quality;
P[1] = Desired_quality_value;
P[2] = 0;
#include <opencv2\opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace CV;
int main (int argc, char **argv)
{
int p[3];
Iplimage *img = cvloadimage ("test.jpg");
P[0] = cv_imwrite_jpeg_quality;
P[1] = ten;
P[2] = 0;
Cvsaveimage ("Out1.jpg", IMG, p);
P[0] = cv_imwrite_jpeg_quality;
P[1] = m;
P[2] = 0;
Cvsaveimage ("Out2.jpg", IMG, p);
Exit (0);
}
There is also a way to use the Imwrite () function to save the picture, while adjusting the compression of the image quality.
Save Image Imwrite ()
BOOL Imwrite (const string& filename,inputarray img,const vector& params=vector ())
Each parameter in the params appears in pairs, i.e. Paramid_1, Paramvalue_1, Paramid_2, paramvalue_2, ..., currently supports the following parameters:
JPEG: Compression quality (cv_imwrite_jpeg_quality), from 0 to 100 (the higher the value the better the quality), the default is 95.
Png:compression level (Cv_imwrite_png_compression) from 0 to 9. The higher the number, the smaller the file size, and the longer the compression time. The default value is 3.
PPM, PGM, or PBM: Binary flag (Cv_imwrite_pxm_binary), 0 or 1. The default value is 1.