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

7 ビュー (過去 30 日間)
ASHWIN JOSHY
ASHWIN JOSHY 2022 年 5 月 1 日
回答済み: yanqi liu 2022 年 5 月 7 日
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
imshow(grayImg,[])
Not by
imshow(grayImg)
but if I store the image with
imwrite(grayImg,outputFileName);
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?
  3 件のコメント
ASHWIN JOSHY
ASHWIN JOSHY 2022 年 5 月 1 日
The encrypted values are double, i want to save as .tiff
Image Analyst
Image Analyst 2022 年 5 月 1 日
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;
tiffObject.setTag(tagstruct);
% Write the array to disk.
tiffObject.write(rgbImage);
tiffObject.close;
% Recall image.
m2 = imread(fileName)
% Check that it's the same as what we wrote out.
maxDiff = max(max(m2-rgbImage))

サインインしてコメントする。

回答 (1 件)

yanqi liu
yanqi liu 2022 年 5 月 7 日
yes,sir,may be use
imwrite(mat2gray(grayImg),outputFileName);
and, then to check the file

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by