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

1 view (last 30 days)
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 Comment
DGM
DGM on 10 Apr 2024
Edited: DGM on 10 Apr 2024
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:

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!