Reputation: 5235
Using blockproc
and a Cosinus discrete transform (DCT
) and a threshold, I just compress an image in Matlab.
I need to find the compression ratio, which is probably not hard to find.
It should be the number of bit in the compressed image / number of bits in the original image..
Using BitDepth
, I am not able to have a valid compression ratio..
f1 = @(block_struct) dct2(block_struct.data);
f2 = @(block_struct) idct2(block_struct.data);
I=imread('autumn.tif','tiff');
Im=rgb2gray(I);
J = blockproc(Im, [8 8], f1);
seuil = find(abs(J) < 15);
J(seuil) = zeros(size(seuil));
K = blockproc(J, [8 8], f2) / 255;
subplot(2,2,2)
imshow(K);
How can I find the compression ratio using Matlab?
Upvotes: 1
Views: 7434
Reputation: 32920
First of all, you need to define compression ratio.
Let's agree that compression ratio is the amount of bits in the original image divided by the amount of bits in the compressed image. Since all values (DCT coefficients) are represented by the same fixed amount of bits, you can deduce the compression ratio from counting the number of coefficients instead of bits.
In the original image the number of coefficients is numel(J)
, whereas it is numel(seuil)
in the compressed image. The compression ratio for this image is therefore:
compression_ratio = numel(J) / numel(seuil)
For example, let's assume that your image dimensions are 640×480, and seuil
contains 100000 indices of values below the threshold. Hence, your compression ratio is 640*480 / 100000 = 3.072
Upvotes: 1