1

This is a follow up to my previous post here

I'm using the following lines of code to convert the data type(from uint16 to uint8) of z-stack images in MATLAB

%Multiple image tiff conversion%

File_Name = "Test_Image.tiff";
Image_Data = imfinfo(File_Name);
Number_Of_Images = length(Image_Data);


Tiff_Structure = struct('Image_File',[]);  

for Image_Index = 1: Number_Of_Images
    
      Image = imread(File_Name,Image_Index);
      Uint8_Image = im2uint8(Image);

      %For more information and plotting individual images%
      Tiff_Structure(Image_Index).Image_File = Uint8_Image;
      
      %Saving the converted images to one tiff file%
      imwrite(Uint8_Image,'Converted_Image.tiff','WriteMode','append');

end

In the documentation available here it is mentioned that

im2uint8(I) converts the grayscale, RGB, or binary image I to uint8, rescaling or offsetting the data as necessary

I would like to know if it is possible to rescale the data before converting the datatype to uint8 and how this rescaling can be done.

Test_Image.Tiff

Suggestions will be really helpful.

EDIT: Plotting the histogram of the image data gives the following

img_data = imfinfo(f);
n_img = length(img_data);

imgs = cell(1, numel(img_data));
for i = 1:numel(img_data)
    imgs{i} = imread(f, i);
end
imgs = cat(3, imgs{:});
figure(1)
imhist(imgs(:), 256)

enter image description here

Natasha
  • 1,111
  • 5
  • 28
  • 66
  • Yes, the function `im2uint8()` does automatically rescale the data and normalizes everything accordingly to fit a 255 (8-bit) range/scale. I don't see the reason for further rescaling. That histogram looks like an aggregate plot of all the images within the TIFF file. I'm not sure if you wanted a histogram as such. – MichaelTr7 Oct 21 '20 at 05:11

1 Answers1

1

Assuming Image has values in the range 0-65535 (16bit) to scale the values into an 8bit image:

  1. first divided the image by 65535 (2^16) - this will normalize value range to 0-1
  2. now you need to rescale to 8bit range of values, multiplying the image by 255

so basically:

scaled_image = uint8(double(Image)./65535*255)

Note: To preserve the dynamic range of the image it might be better to choose a different value to normalize with, e.g some max value across all stack

Dudas
  • 72
  • 7