How to save rescale image ?
9 views (last 30 days)
Show older comments
Hi, i just want to know did matlab can save a rescale image because when i save the data and open it for check that value in matrix show scale range [0 255] not [0 1] like before i save it. If it can, can you please tell me how to do.
my code for rescale and save as below :
ct1 = imread('ct1.png');
ct_re = rescale(ct1);
ct_save = sprintf('ct_rescale.png');
imwrite(ct_re,ct_save);
and this is the result:
0 Comments
Accepted Answer
DGM
on 24 Jul 2023
Edited: DGM
on 24 Jul 2023
The only normal image format that's supported by MATLAB for writing float data is TIFF.
% assuming inputs are either double or single precision float arrays
% with either 1 or 3 channels
inpict = imread('peppers.png'); % uint8
inpict = im2double(inpict); % unit-scale double
sz = size(inpict,1:3);
filename = 'testtifffp.tiff';
t = Tiff(filename, 'w');
tagstruct.ImageLength = sz(1);
tagstruct.ImageWidth = sz(2);
tagstruct.Compression = Tiff.Compression.None;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
if sz(3) == 1
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
elseif sz(3) == 3
tagstruct.Photometric = Tiff.Photometric.RGB;
end
if strcmp(class(inpict),'single')
tagstruct.BitsPerSample = 32;
elseif strcmp(class(inpict),'double')
tagstruct.BitsPerSample = 64;
end
tagstruct.SamplesPerPixel = sz(3);
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
t.setTag(tagstruct);
t.write(inpict);
t.close();
% read the image back
newpict = imread(filename);
isequal(inpict,newpict) % test that it's the same
Don't expect any other application to be able to read it though. Given that float TIFFs aren't widely supported, you might as well just save it to a .mat file if you really needed to save it in floating point.
Also, if you're just trying to scale a uint8 image to unit-scale (0-1), then using rescale() is probably the wrong thing to do. When used in the given manner, rescale() does not map [0 255] to [0 1]. It maps whatever the image extrema are to [0 1], which will usually change the image contrast. Tools like im2double() and im2single() are what you use to change numeric class and scale without altering the image data relative to black and white.
That said, it's not clear why you can't store the image as uint8. It's already been quantized, so storing it in unit-scale doesn't accomplish anything but increase the file size and make it incompatible with most other software. In the given example with peppers.png, the TIFF file is over 16x as large, but it contains no more color information than the original PNG.
2 Comments
Image Analyst
on 24 Jul 2023
Well said. A good explanation. If they're going to use it again/later in MATLAB and want it as floating point, then saving it as a .mat file would be the best option in my opinion.
fullFileName = fullfile(pwd, sprintf('ct_rescaled.mat'));
save(fullFileName, 'ct_re');
Then to recall it, use load
More Answers (0)
See Also
Categories
Find more on Image Data in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!