How to save an image which only display properly using imshow(grayImg,[])?

6 views (last 30 days)
I am working on an image encryption project, so I first converts color image to gray and do the rest, after encryption I can see the image only using
Not by
but if I store the image with
Then it isn't saving as expected or not as I saw when using imshow(grayImg,[]) Does anyone know what kind of error am I making?
Image Analyst
Image Analyst on 1 May 2022
If you use single or double in imshow() it expects the values to be in the range 0-1. If they're below 0 the pixels show up as black. If more than 1 the pixels show up as white. If you use [] then it scales the min of your data (whatever value it may have) to black and the max to white.
To save a double as a tiff, this should do it:
% Create floating point image.
rgbImage = rand (10, 20, 3);
% Image must be single precision.
rgbImage = single(rgbImage);
% Display it.
imshow(rgbImage, 'InitialMagnification', 1000)
axis('on', 'image');
% Create tiff object.
fileName = '_floatingPointImage.tif';
tiffObject = Tiff(fileName, 'w')
% Set tags.
tagstruct.ImageLength = size(rgbImage,1);
tagstruct.ImageWidth = size(rgbImage,2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SamplesPerPixel = size(rgbImage,3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
% Write the array to disk.
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage))

Sign in to comment.

Answers (1)

yanqi liu
yanqi liu on 7 May 2022
yes,sir,may be use
and, then to check the file


Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!