Sunday, December 24, 2017

How to read and write multiple TIFF images (i.e. folder access) having negative values using MATLAB?

% Select input image folder
image_folder = uigetdir('F:\','Select input image folder');
filenames=dir(fullfile(image_folder,'*.tiff'));
display(filenames(1));
total_images=numel(filenames);

% Select output image folder
op_im_folder = uigetdir('F:\','Select output image folder');

% Iterate through all the images
for n = 1:1:total_images
      full_name = fullfile(image_folder,filenames(n).name);
      input_image = imread(full_name);
     
      % do processing here, Output_image = result of processing
      % (typecast int32 or appropriate datatype for negative values and use the same information in            % 'BitsPerSample' tag)

      % Create name of output image file
      file_name=strcat('OutputImage_',num2str(n));
      file_name = strcat(op_im_folder,'\',file_name,'.tif');
     
      % Here, I'm writing data as TIFF images as it can save negative pixel values.
      % This can be handy if we need subtract two images to eliminate background information.
      % In other cases, the function 'imwrite' is easy to use.
      tiffObj = Tiff(file_name,'w');
      setTag(tiffObj,'ImageLength',size(Output_image,1))
      setTag(tiffObj,'ImageWidth',size(Output_image,2))
      setTag(tiffObj,'Photometric',Tiff.Photometric.MinIsBlack)
      setTag(tiffObj,'BitsPerSample',32)
      setTag(tiffObj,'SamplesPerPixel',size(Output_image,3))
      setTag(tiffObj,'SampleFormat', Tiff.SampleFormat.Int)
      setTag(tiffObj,'Compression',Tiff.Compression.None)
      setTag(tiffObj,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky)
      setTag(tiffObj,'Software','MATLAB')
      write(tiffObj,Output_image);
end

% Close TIFF object
close(tiffObj);

disp('Operation completed');

No comments:

Post a Comment

Total Magnification

If a microscope has objective magnification (Mo) of 10x and eyepiece magnification (Me) of 10x, then total magnification (Mt) is given as: M...