How to compress a grey level image in different compression ratio

2 vues (au cours des 30 derniers jours)
qing li
qing li le 5 Août 2022
Commenté : qing li le 5 Août 2022
Dear all, I am extracting the watermark W from watermarked image R now, and in order to see whether my algorithm is robust enough to compression, I need to compress the watermarked image R multiple times...
As for how to compress the watermarked image R , My tutor said that I can save R as JPEG format, save it one time means compress once , and save it two times means compress twice. I did what he told me to do in matlab , and the following code is as the bottom.
My question is why 'R c1.jpeg' is totally same with 'R c2.jpeg' and 'R c3.jpeg' when R.png is R(2).png in the attachment ,and they are different when R.png is R.png in the attachment ...if they are the same,does that means compression does not work..? or maybe is there sth wrong in my code..?
Could anyone help please,thx a lot !
i
mwrite(imread('R.png'), 'R c1.jpeg'); % compress once
imwrite(imread('R c1.jpeg'),'R c2.jpeg');% compress twice
imwrite(imread('R c2.jpeg'),'R c3.jpeg'); % compress three times
( 'R.png' is watermarked image R, 'R c1.jpeg' is the result for compressing R once, 'R c2.jpeg' is the result for compressing R twice, 'R c3.jpeg' is the result for compressing R three times)

Réponse acceptée

Walter Roberson
Walter Roberson le 5 Août 2022
The png file has full data.
When you imwrite an image as .jpeg, the compression algorithm first transforms the image as a series of coefficients that together have enough detail to recreate the image exactly. The algorithm then throws away details from the representation, such as replacing entries with small absolute value with 0. This is done in a way that permits the remaining coefficients to be written out with less space than it would take to write out the full details of the coefficients, or to represent the original image.
When you then imread() the .jpeg, the decompression algorith reads in the lower-quality coefficients and recreates the image, having lost some details relative to the original image.
When you then go to imwrite() the reconstructed image, again the algorithm transforms the information from the array. However, because details were already thrown away, the new representation is the same as what was stored in the .jpeg file, and there is nothing new to be lost in writing out to the file.
Imagine, for example, that you had an image that was all integers in the range 0 to 255, which is something that takes 8 bits each. Now imagine that you took all of the entries less than 128 and replaced them with 0, so the remaining entries were all 128 to 255 or else were 0. Now suppose that you take each entry, and if it is (now) 0 you emit a single bit that is 0, and otherwise you emit the 8 bits of the entry, which is something that (in this construction) will always start with a 1 bit. Each value that was originally 0 to 127 has been replaced with a single bit 0, and each value that was originally 128 to 255 is still 8 bits. Pack all of these bits into a file, beside each other; if the original data was uniformly random distributed then 1/2 of the values were replaced by a single bit and half of the values were left alone at 8 bits, for an average of 1/2 * 1 + 1/2 * 8 = 4 1/2 average bits per entry instead of averaging 8 bits per entry.
Now imagine the reconstruction process. It reads a bit from the file, and if the bit is 0, it stores a full 8 bits of 0. If the bit read was 1, then it reads the next 7 bits and builds an 8 bit entry from the 1 that was read in together with the 7 bits. In this way, you get back an array that is 8 bits each. The result will have lost information, but it is the size of the original image.
Now... take that array and put it through the same process again. It has already lost all of the entries less than 128, replacing them all with 0, so the compressed version of this would be exactly the same as what was stored to the file before: all the details to be lost have already been lost. As long as the compress and decompression are consistent, once the details have been lost there is nothing more to lose by more compression steps that are done with the same settings.
But not all is lost: you can imwrite() the .jpeg with different 'quality' settings. The 'quality' option controls how much detail is to be lost. If you throw away more detail each time, that would result in more compression each time.
  1 commentaire
qing li
qing li le 5 Août 2022
That is really helpful ! Thank you sooo much for your attention and time for this issue !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Denoising and Compression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by