Strange / magic image visualization with Matlab
I have a double image, I want to display it with unsigned int 16 bit, so I do:
I = im2uint16(I); figure;imshow(I);title('Image being saved')
This shows it (with its normal noise):
Now I want to record this image using .png with 16 bit bit depth. I AM:
imwrite(I,'image.png','BitDepth',16);
And now the image opened with Photoshop CS5 or Windows Photo Viwer looks like this: (the noise has magically disappeared):
Can someone explain this strange behavior?
How to reproduce this error
Load in the C:\test\
image I used here :
Now run this script:
I = im2double(imread('C:\test\test_matlab.tif'));
% Add gaussian noise with variance = 0.0012
I = imnoise(I,'gaussian',0,0.0012);
figure,imshow(I);
imwrite(I,'C:\test\withNoise.tif');
And compare the figure in matlab with the saved file
source to share
Difficult to say because you didn’t give enough data to play back, but I would guess that the problem is with a display issue: the image is larger than your physical display window, so some downsampling must be applied to display it. Depending on how this resampling is performed, the result may be - in this scenario - very different, visually. Suppose Matlab uses nearest neighbor resampling to display it, which explains why the image looks very noisy; instead, if another image viewer applies bilinear interpolation or something similar, this will constitute a local average, which practically filters out the white noise.
To test this, try doing the same with a small image. Or try enlarging the visible clear image to see it in real size (100%: one pixel of the image = one pixel of the display)
Update: see also here
source to share
Here's what I did:
%# read the image (why is it so big?)
I = im2double(imread('https://p7o1zg.bay.livefilestore.com/y1pcQVsmssygbS4BLW24_X1E09BKt_Im-2yAxXBqWesC47gpv5bdFZf962T4it1roSaJkz5ChLBS0cxzQe6JfjDNrF7x-Cc12x8/test_matlab.tif?psid=1'));
%# add noise
I = imnoise(I,'gaussian',0,0.0012);
%# write tiff
imwrite(I,'withNoise.tif');
%# read the tiff again
I2 = imread('withNoise.tif');
class(I2) %# -- oopsie, it uint8 now!
%# convert to uint16 as in original post
I = im2uint16(I);
%# writ again
imwrite(I,'withNoise16.png','bitDepth',16);
%# read it
I2 = imread('withNoise16.png');
%# compare
all(all(I==I2)) %# everything is equal
So there is no funky thing about writing / reading an image (although you lose some information when converting bits), your original image is about a third of the dynamic range, so you will lose more information if you stretched the contrast before converting).
However, the image is 2k by 2k. When I only look at the top right corner of the image (taking 500 by 500 pixels), it displays the same in Matlab and other graphics programs. So I bet it's a matter of resampling your image, which Matlab does differently from other programs. As @leonbloy suggests, Matlab can do nearest neighbor resampling while other programs will do interpolation.
source to share