How to save an image which only display properly using imshow(grayImg,[])?
10 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
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
2022 年 5 月 7 日
yes,sir,may be use
imwrite(mat2gray(grayImg),outputFileName);
and, then to check the file
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!