Search code examples
matlabimage-compression

Image Compression using imfinfo function in Matlab


I am trying to calculate the compression ratio of a given image. My matlab code is as follows:

temp = imfinfo('flowers.jpg'); comperssion_ratio = (temp.Width * temp.Height * temp.BitDepth) / temp.FileSize;

The imfinfo displays the following:

       FileSize: 11569
         Format: 'jpg'
  FormatVersion: ''
          Width: 430
         Height: 430
       BitDepth: 8
      ColorType: 'grayscale'
FormatSignature: ''
NumberOfSamples: 1
   CodingMethod: 'Huffman'
  CodingProcess: 'Sequential'
        Comment: {}

Running the above code gives me a compression ratio of about 120 which is huge and does not seem right. Is there something that I'm doing wrong? I went through a document from MIT and they showed that the Width and Height and BitDepth should be divided by 8 and then divided by the FileSize. Why divide by 8?


Solution

  • The division by factor of 8 is to convert bits to bytes.

    According to the Matlab documentation for imfinfo

    the FileSize parameter is the size of the compressed file, in bytes.

    The compression ratio is defined as:

    uncompressed size of image in bytes/compressed size of file in bytes

    imfinfo gives you the pixel width, height, and bits per pixel (bit depth). From that you can compute the uncompressed size in bits, and divide by 8 to get bytes.

    For the uncompressed image , you have 430*430*8/8 = 184,900 bytes.

    The size of the compressed image is 11569 bytes.

    So the compression ratio is actually 184,900/11569 or 15.98, not an unreasonable value for JPEG.