Converting unit16 image to IEEF 32 bitdepth and saving as Tif.

조회 수: 1 (최근 30일)
Tevin
Tevin 2024년 4월 10일
편집: DGM 2024년 4월 10일
I want to convert some images from unint16 to 32 Bitdepth and IEEF single data type. I want to scale my original images to the max and min of the target images (IEEF). However, when i do this and save as Tif file, the image is purely black and white with no grey level differences. I want to know what I am doing wrong. Can someone help me?
I have the part of the code I use for scaling the max and min values and then saving to tif format. Is there a simpler way to do this that actually preserves the grey values of my images while scaling accordingly?
% Get a list of all TIFF files in the source folder
source_files = dir(fullfile(source_folder, '*.tiff')); % or '*.tiff' depending on the extension
% Loop through each source image
for i = 1:numel(source_files)
% Read the source image
source_image = imread(fullfile(source_folder, source_files(i).name));
% Convert the source image to IEEE floating point with a bit depth of 32
converted_image = im2single(source_image);
% Ensure the data type and bit depth are correct
converted_image = cast(converted_image, 'single');
% Save the converted source image with the same name and metadata as the target image
[~, name, ext] = fileparts(source_files(i).name);
output_filename = [name, ext];
output_fullpath = fullfile(source_folder, output_filename);
t = Tiff(output_fullpath, 'w');
tagstruct.ImageLength = size(converted_image, 1);
tagstruct.ImageWidth = size(converted_image, 2);
tagstruct.Photometric = Tiff.Photometric.MinIsBlack;
tagstruct.BitsPerSample = 32;
tagstruct.SampleFormat = Tiff.SampleFormat.IEEEFP;
tagstruct.Compression = Tiff.Compression.None;
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
t.setTag(tagstruct);
t.write(converted_image);
t.close();
end
  댓글 수: 1
DGM
DGM 2024년 4월 10일
편집: DGM 2024년 4월 10일
This puts your images in unit-scale, as expected for the numeric class.
converted_image = im2single(source_image);
This doesn't do anything, since they're already single.
converted_image = cast(converted_image, 'single');
I don't see anywhere else where you're scaling the images.
The images that are written by this code should still be readable by imread(), but if you're opening them in some other software, you can probably expect them to not work. There isn't broad support for floating-point TIFFs.
See also:

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by