OpenCV cvSaveImage Jpeg Compression Factor

后端 未结 4 676
无人及你
无人及你 2020-12-05 16:15

I am using OpenCV and saving as a jpeg using the cvSaveImage function, but I am unable to find the Jpeg compression factor used by this.

  1. What\'s cvSaveImage(.
相关标签:
4条回答
  • 2020-12-05 16:19
    imwrite("filename.jpeg",src,(vector<int>){CV_IMWRITE_JPEG_QUALITY, 20});
    
    • filename.jpeg will be output File name
    • src be source image read containing variable
    • (vector<int>) typecasting
    • {CV_IMWRITE_JPEG_QUALITY, 20} an array of elements to be passed as Param_ID - and Param_value in imwrite function
    0 讨论(0)
  • 2020-12-05 16:25

    Currently cvSaveImage() is declared to take only two parameters:

    int cvSaveImage( const char* filename, const CvArr* image );
    

    However, the "latest tested snapshot" has:

      #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 CV_DEFAULT(0) );
    

    I've been unable to find any documentation, but my impression from poking through this code is that you would build an array of int values to pass in the third parameter:

    int p[3];
    p[0] = CV_IMWRITE_JPEG_QUALITY;
    p[1] = desired_quality_value;
    p[2] = 0;
    

    I don't know how the quality value is encoded, and I've never tried this, so caveat emptor.

    Edit:

    Being a bit curious about this, I downloaded and built the latest trunk version of OpenCV, and was able to confirm the above via this bit of throwaway code:

    #include "cv.h"
    #include "highgui.h"
    int main(int argc, char **argv)
    {
        int p[3];
        IplImage *img = cvLoadImage("test.jpg");
    
        p[0] = CV_IMWRITE_JPEG_QUALITY;
        p[1] = 10;
        p[2] = 0;
    
        cvSaveImage("out1.jpg", img, p);
    
        p[0] = CV_IMWRITE_JPEG_QUALITY;
        p[1] = 100;
        p[2] = 0;
    
        cvSaveImage("out2.jpg", img, p);
    
        exit(0);
    }
    

    My "test.jpg" was 2,054 KB, the created "out1.jpg" was 182 KB and "out2.jpg" was 4,009 KB.

    Looks like you should be in good shape assuming you can use the latest code available from the Subversion repository.

    BTW, the range for the quality parameter is 0-100, default is 95.

    0 讨论(0)
  • 2020-12-05 16:36

    OpenCV now has a parameter to set jpeg quality. I'm not sure exactly when this was introduced, but presumably sometime after 2.0.

    const int JPEG_QUALITY = 80;
    
    Mat src;
    // put data in src
    
    vector<int> params;
    params.push_back(CV_IMWRITE_JPEG_QUALITY);
    params.push_back(JPEG_QUALITY);
    
    imwrite("filename.jpg", src, params);
    

    If you are using C++0x, you can use this shorter notation:

    imwrite("filename.jpg", src, vector<int>({CV_IMWRITE_JPEG_QUALITY, JPEG_QUALITY});
    
    0 讨论(0)
  • 2020-12-05 16:39
    1. You can probably find this by poking around in the source code here: http://opencvlibrary.svn.sourceforge.net/viewvc/opencvlibrary/
    2. You can't, as the function does not accept such a parameter. If you want to control the compression then the simplest method I can think of is first saving your image as a bitmap with cvSaveImage() (or another lossless format of your choice) and then use another image library to convert it to a JPEG of the desired compression factor.
    0 讨论(0)
提交回复
热议问题