3

I have been working on a problem where I need to save a matrix as an Image and for it to be a truly lossless example of the matrix in MATLAB.

I have tried writing the image as a png and tiff both in lossless form:

Name              Size                 Bytes  Class     Attributes

  diff            237x354               671184  double              
  imPng           237x354               167796  uint16              
  imPngD          237x354               671184  double              
  imTiff          237x354                83898  uint8               
  imTiffD         237x354               671184  double              
  padded         2042x2170            35449120  double              
  testImage       237x354               671184  double              

testImage is the matrix I would like to save losslessly.

I used the following lines to write the matrix as a tif and png:

EDU>> imwrite(testImage,'imTiff.tif','tiff','compression','none');
EDU>> imwrite(testImage,'imTiff.png','PNG','bitdepth',16);

I then loaded the images back to the work space (imTiff and inPng) and converted them to double.

EDU>> imPngD = im2double(imPng);
EDU>> imTiffD = im2double(imTiff);

but when I subtract either imPngD or imTiffD from testImage there are residual values left.

My questions are:
Is there something wrong in my processing?
If not, does this mean that tif and png are not truly completely lossless?

I can supply images if you think it would help.

I thought these images might help: testImage
testImage - original
Difference between imTiffD and testImage
Difference between imTiffD and testImage
Difference between imPngD and testImage
Difference between imPngD and testImage

Please note: the values of the grey pixels in images 2-3 are zero i.e. the difference between testImage and the reloaded images are zero.

Gavin
  • 31
  • 4
  • 1
    How different are they? Could it just be a side effect of floating point error? – im so confused Oct 08 '12 at 14:26
  • Will the pixel values fit in 16 bits for PNG? If the values have fractions, converting to 16 bit integer might cause the precision loss. Look at the Tiff object if you want to write true double values pixels. – Ashish Uthama Oct 08 '12 at 14:47
  • Sorry I am not familiar with the floating point error. The differences are small, if I take the max and the min difference between testImage - imTiffD the answer is +- 0.001960784313725 and for testImage - imPngD the answer is +- 7.629510948348184e-06 – Gavin Oct 08 '12 at 18:04

1 Answers1

1

PNG and TIFF are both truly lossless (ignoring the TIFF option to package JPEG, which you are not using). However they are truly lossless given their input, which is either 8 or 16 bits per pixel per color value for PNG, or 8 bits per pixel per color value for TIFF.

If you are looking beyond the precision that can be carried by 8 or 16 bit values, then you will see a difference.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • So would I be right to conclude that in the areas of images 2-3 above (just added) where there is a difference from testImage, the precision of the matrix values of testImage are not carried over when I save the matrix as either a png or tiff? – Gavin Oct 08 '12 at 18:24
  • 1
    You are probably overflowing in that region. Since your source image is double, the range 0 to 1 is scaled to 0 to 255. If the data is less than 0 or more than 1, then you'll wrap around, and when it comes back all of the values will be between 0 and 1. Check your data. – Mark Adler Oct 09 '12 at 02:06