Search code examples
opencvcompressionjpegimage-compression

OpenCV - Possible Options for Image Compression


thanks for taking the time to read my post.

I'm working on a little project that is going well but as an optimisation exercise I would like to reduce the size of some of the JPEG's i'm outputting. At the moment, I have a .jpg that is 600 x 400 that is about 80kb.

I have read about 32-bit, 8-bit images being signed, unsigned and floats and char's, along with BGR and YUV colour spaces and am not sure which are the factors that most affect size.

Could anybody suggest where I might start to reduce this file size?

I will of course play about and conduct my own tests to find the right image quality to file size balance.

Many thanks in advance, Kay


Solution

  • sounds like you want to compress your images, when writing to disk.

    here's it for jpeg:

    std::vector<int> params;
    params.push_back(CV_IMWRITE_JPEG_QUALITY);
    params.push_back(100);   // that's percent, so 100 == no compression, 1 == full 
    cv::imwrite("my.jpg",image,params);
    

    and here for png:

    std::vector<int> params;
    params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    params.push_back(9);   // that's compression level, 9 == full , 0 == none
    cv::imwrite("my.png",image,params);