简体   繁体   中英

Quantization Error in Lossless JPEG2000 (Matlab)

I have the following matrix:

A = [0.01 0.02; 1.02 1.80];

I want to compress this using JPEG 2000 and then recover the data. I used imwrite and imread in MATLAB as follows:

imwrite(A,'newA.jpg','jp2','Mode','lossless');
Ahat = imread('newA.jpg');

MATLAB give me the result in uint8 . After converting data to double I get:

Ahat_double = im2double(Ahat)

Ahat_double = 

0.0118    0.0196
1.0000    1.0000

I know this is because of the quantization, but I don't know how to resolve it and get the exact input data, which is what lossless compression is supposed to do.

Converting data to uint8 at the beginning did not help.

The reason why you are not getting the correct results is because A is a double precision matrix. When you are writing images to file in double precision, it assumes that the values vary between [0,1] . In your matrix, you have 2 values that are > 1 . When you write this to file, these values will saturate to 1, and then they are saved to file. Actually, before even writing, the intensities will be scaled so that they are uint8 and vary between [0,255] . When you try re-reading the values, it will be read in as intensity 255, or double intensity of 1.0.

The other two values make sense when you read the values back in, as 0.01 in double form is actually 255*0.01 = 2.55 and thus rounded to 3 and 3 / 255 = 0.0118 . For 0.02 , this is 255*0.02 = 5.1 and thus rounded to 5 and 5 / 255 - 0.0196 .

The only way you can possibly get around this is to renormalize your data before you write the image so that it conforms to [0,1] . To get the original data back, you would have to know the minimum and maximum values you had before you normalized this. Even when you do this, there are only 256 possible double precision values that can be encoded in your image (assuming grayscale), and so you will not be able to capture all possible floating point values this way.

As such, there is basically no way around your problem, so you're SOL !

If you want to encode arbitrary data using the JPEG 2000 standard, perhaps you should download this library from MATLAB's File Exchange . I haven't taken a closer look at it, but it may be able to compress arbitrary data using the JPEG 2000 algorithm.

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