フィルターのクリア

How to save rescale image ?

7 ビュー (過去 30 日間)
patthanee
patthanee 2023 年 7 月 24 日
コメント済み: patthanee 2023 年 7 月 25 日
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:

採用された回答

DGM
DGM 2023 年 7 月 24 日
編集済み: DGM 2023 年 7 月 24 日
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
ans = logical
1
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 件のコメント
Image Analyst
Image Analyst 2023 年 7 月 24 日
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
patthanee
patthanee 2023 年 7 月 25 日
Thank you for your code and all explanation. it's help me so much.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by