Search code examples
opencvjpegencoderos

Why cv::imencode gives similar size of output buffer for bgr8 and mono8 images?


I notice 4.1 MB/s @50fps 1280x720 bgr8.

.....and 3.7 MB/s @50fps 1280x720 mono8.

I was expecting around 3 times less bandwidth consumption for grayscale.

I completely edited my question to simplify and to make it more direct. Below is my simple C++ program and its output, it can reproduce my findings. lenna.png

#include <iostream>
#include <vector>
#include <opencv2/imgproc/imgproc.hpp>    // gray
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat im, imGray;
    im = imread( "lenna.png", IMREAD_COLOR ); // Read the file
    imGray = imread( "lenna.png", IMREAD_GRAYSCALE ); // Read the file
    if( im.empty() )                      // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    if(true){
        std::vector<int> params;
        params.resize(9, 0);
        params[0] = cv::IMWRITE_JPEG_QUALITY;
        params[1] = 80;
        params[2] = cv::IMWRITE_JPEG_PROGRESSIVE;
        params[3] = 0;
        params[4] = cv::IMWRITE_JPEG_OPTIMIZE;
        params[5] = 0;
        params[6] = cv::IMWRITE_JPEG_RST_INTERVAL;
        params[7] = 0;

        std::vector< uchar > buffer, bufferGray;

        if (cv::imencode(".jpeg", im, buffer, params))
          {

            float cRatio = (float)(im.rows * im.cols * im.elemSize())
                / (float)buffer.size();
            printf("BGR8 Jpeg image size: %lu KB,  Compression Ratio: 1:%.2f\n", buffer.size()/1024, cRatio);
          }
          else
          {
            printf("cv::imencode (jpeg) failed on bgr8 image\n");
          }


        if (cv::imencode(".jpeg", imGray, bufferGray, params))
          {

            float cRatio = (float)(imGray.rows * imGray.cols * imGray.elemSize())
                / (float)bufferGray.size();
            printf("MONO8 Jpeg image size: %lu KB,  Compression Ratio: 1:%.2f\n\n", bufferGray.size()/1024, cRatio);
        
          }
          else
          {
            printf("cv::imencode (jpeg) failed on mono8 image\n");
          }
      }
  
    imshow( "BGR8", im );
    imshow( "MONO8", imGray );
    waitKey(0);
   return 0;
}

g++ -g -O3 main.cpp -o main pkg-config --libs --cflags opencv4

Output: enter image description here My concern is low compression ratio for gray-scale.

Below are some of my system info:

jai@jai:~$ opencv_version
4.2.0
jai@jai:~$ opencv_version -v | grep jpeg
JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
jai@jai:~$ ldconfig -p | grep jpeg
libmjpegutils-2.1.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libmjpegutils-2.1.so.0
libjpegxr.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libjpegxr.so.0
libjpeg.so.8 (libc6,x86-64) => /lib/x86_64-linux-gnu/libjpeg.so.8
libjpeg.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libjpeg.so
libgdcmjpeg16.so.3.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgdcmjpeg16.so.3.0
libgdcmjpeg16.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libgdcmjpeg16.so
libgdcmjpeg12.so.3.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgdcmjpeg12.so.3.0
libgdcmjpeg12.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libgdcmjpeg12.so
libgdcmjpeg8.so.3.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libgdcmjpeg8.so.3.0
libgdcmjpeg8.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libgdcmjpeg8.so
jai@jai:~$

I checked on opencv4, opencv3.4.0 and opencv3.4.9.


Solution

  • JPEG in its default encoder setting does 4:2:0 chroma subsampling. So the assumption that a color image takes three times the grayscale image in bandwidth is wrong. Instead, it should be factor 1,5. But additionally:

    1. The two chroma channels are stored relative to the intensity/green channel (YCbCr color model), which means they can be compressed more effectively.
    2. You always need to store headers, quantization matrix, etc. so you have a baseline to consider when comparing buffer sizes

    In the end, I would say 43kb vs 33kb looks pretty reasonable.