Use OpenCV to save pictures and generate JPG format to adjust the compression quality.

Source: Internet
Author: User


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.