简体   繁体   中英

Compressing using JPEG but no quality degradation (MATLAB)

Good day,

I am trying to show that compressing an image many-many times (extremely many, as in 500 times), will show quality degradation. And from what I understand, this can happen when an image is saved many-many times in JPEG. I tried writing a MATLAB code to do this:

    clc;close all;clear;

for i = 1:500
    if i==1
        a = imread('e:\ismoka_small.jpg');
        currFileName = 'e:\multipleJpegs\001.jpg';
    else
        a = imread(currFileName);
        if i <= 10
            zeross = '00';
        elseif i <= 100
            zeross = '0';
        elseif i <= 1000
            zeross = '';
        end
        currFileName = ['e:\multipleJpegs\' zeross num2str(i-1) '.jpg'];
    end

    imwrite(a, currFileName, 'jpeg');
end

The end result however shows that no degradation occurs, and all the 500 images have the same file size. I was wondering if anyone can help me and explain why this is the case? Or do I have it wrong regarding the JPEG algorithm? Thanks in advance :)

The loss caused by JPEG compression is due to quantization, which is essentially rounding or truncation. If you're always saving with the same quality setting, it's quite possible that the quantization process produces the same results each time, especially if you do this many times - the pixels will degrade until they reach a point where they survive the round trip, then they won't change any more.

If you make any changes to the image before resaving, those changes will cause degradation in the 8x8 or 16x16 region where the changes were made.

If you save at different quality settings every time, you'll get different quantization each time and the image will definitely degrade, even sometimes if you use a higher setting.

Here I've repeated the test with my own image, using Python's PIL to open and save the image 100 times. I also opened and saved it one more time, to see if there was any additional degradation - there was no difference. I've resaved the JPEGs as PNG to prevent any further losses from StackOverflow's image engine.

Although there are measureable differences between the first and 100th saves, they are insignificant in comparison to the difference between the original and the first.

Original:
原版的

First save:
第一次保存

100th save:
第100次保存

You're simply reading and saving the file as is. You're not reprocessing it in any way. I don't know how you do that in matlab, but you must open it, convert to image, and then save that image as JPEG. That's whre the degradation will occur. And yes, you'll see the degradation because JPEG is a lossy compression algorithm. However, the quality loss can't be measured by the file size.

You'll see the greates degradation on sharp edges: for example the borders of a black figure on a white background. The degradation is greater when you choose the higher compression rates or lower quality settings.

NOTE: thanks to denver's comment, let's make it clear that there are also lossless JPEG formats: JPEG-LS and JPEG2000, but they're really unusual. If you use one of this lossless formats no matter how many times you compress and decompress and image, it will keep exactly like it was originally

Not sure. The quality should default to 75, causing a little degradation. You could try the following tests.

1) Save the original image with different qualities to see if you can tell the difference.

imwrite(a, "75.jpg", 'jpeg', 'Mode', 'lossy', 'Quality', 75);
imwrite(a, "50.jpg", 'jpeg', 'Mode', 'lossy', 'Quality', 50);

2) Set the quality setting lower in your loop to see if it makes the effect more pronounced.

3) You can confirm the quality is decreasing each iteration by loading the image from the previous iteration and subtracting. If the result is zero you are not changing the image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM