How to save an image which only display properly using imshow(grayImg,[])?
4 次查看(过去 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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!