How to calculate image compression ratio?

79 views (last 30 days)
After doing some processing on the image, say DCT and Quantization, how can I calculate the compression ratio between the compressed image and the original one ?
Thanks in advance

Accepted Answer

Walter Roberson
Walter Roberson on 29 Aug 2015
Let A be the storage size needed to represent the original image. For example use whos() to fetch it.
Let B be total storage size of all the arrays together that are needed to hold the compression information that would be needed to recover the image. For dct type arrangements be sure to include the storage needed to indicate which coefficients have been zeroed.
Now A/B is your compression ratio. If you get a result less than 1.0 then that means that your compression representation takes more space than the original, which does happen for some data and some compression algorithms.
  5 Comments
Dibyalekha Nayak
Dibyalekha Nayak on 11 Jan 2023
How to calculate the compression ratio for the dwt compressed images.Please help in this problem.
Walter Roberson
Walter Roberson on 12 Jan 2023
The ratio is what I outlined in my Answer.
The one thing I would change is to mention that when you are calculating the array sizes, be sure to include any storage you need to indicate the array sizes (explicit size) or any end-of-row/column markers (implicit size).
Mentally plan out: if you were to write the data to a file in a form that allowed you to exactly reconstruct the data in the same size and shape, then how large would the file be ? For example if you have uint8 data then for A, storage size needed to represent the image, include (say) 16 bits for a uint16 for the number of rows, another 16 bits for uint16 for number of columns, and then write out a stream of uint8 data. The total length of the data divided by (rows times columns) tells you how many color planes existed, so you can reconstruct whether you had grayscale or RGB without needing to explicitly write the number of color planes.
For B, the total storage size of all of the arrays together that need to hold the information needed to reconstruct the image, storing the sizes becomes even more important. (In some schemes you can imply sizes -- the size of each layer of detail coefficients is half the rows and columns of the previous level.)

Sign in to comment.

More Answers (1)

Sazzad Hosen
Sazzad Hosen on 17 Jul 2023
original_image = imread('babu.jpg');
compression_quality = 80;
imwrite(original_image, 'compressed_image.jpg', 'Quality', compression_quality);
original_info = dir('babu.jpg');
original_size = original_info.bytes;
compressed_info = dir('compressed_image.jpg');
compressed_size = compressed_info.bytes;
compression_ratio = original_size / compressed_size;
disp(['Compression Ratio: ', num2str(compression_ratio)]);
Compression Ratio: 4.2251
  1 Comment
Walter Roberson
Walter Roberson on 17 Jul 2023
No, this is not a correct calculation. jpg files can include header information such as size information and comments and resolution information. Your source file might include several kilobytes of header information. You are not copying that header information to the output file, so potentially the portion of the output file that is used to hold the header information might get larger but because of the omitted header information the output file might be smaller. You are not comparing the size of the image portion only.
Furthermore the original jpg file is already compressing the image. The real task for calculation of compression ratio is to compare the size of the image portion of the uncompressed image to the size of the image portion of the compressed image.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!