MATLAB: I have a folder of .tif images that need to be converted to a different format for downstream analysis

convertformattingimage formatmat2graymatrix image

I have a folder of ~2400 .tif files, where these files were clipped from a georeferenced raster in QGIS. If I use the imread() they show up as an all white image, but if I use mat2gray() I can visualize them. So simply running this code below (found in a previous question answered by image analyst) doesn't help my issue that they stay in matrix style. I believe that is my issue, but not sure. I attached a .zip folder with 5 .tif images for reference. Any suggestions? Thanks!
% Specify the folder where the files live.
myFolder = '/Users/masonlien/Desktop/PhD/Drone/2020/test';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease select another folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles);
if numFiles == 0
warningMessage = sprintf('Warning: no TIFF images found in the folder:\n%s', myFolder);
uiwait(warnuser(warningMessage));
return;
end
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s (#%d of %d).\n', fullFileName, k, numFiles);
% such as reading it in as an image array with imread()
imageArray = imread(mat2gray(fullFileName));
imshow(imageArray); % Display image.
caption = sprintf('%s (#%d of %d)', fullFileName, k, numFiles);
title(caption, 'FontSize', 14, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Write it out as a PNG file.
baseFileName = strrep(lower(baseFileName), '.tif', '.jpg');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end

Best Answer

Hi,
I think your input tif files are in single format whereas your output file format which is jpeg doesn't support single format while using imwrite (you can find using help imwrite in the 'fmt' of the file output section)
You can do this instead. either save them in a different file format let us say 'bmp', 'jp2' or 'png'. Else you can do the following thing by converting your input image from single format to unsigned integer 8 (uint8).
% Specify the folder where the files live.
myFolder = '/Users/masonlien/Desktop/PhD/Drone/2020/test';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease select another folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = length(theFiles);
if numFiles == 0
warningMessage = sprintf('Warning: no TIFF images found in the folder:\n%s', myFolder);
uiwait(warnuser(warningMessage));
return;
end
for k = 1 : numFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf('Now reading %s (#%d of %d).\n', fullFileName, k, numFiles);
% such as reading it in as an image array with imread()
% Change made here
imageArray = uint8(imread((fullFileName)));
imshow(imageArray); % Display image.
caption = sprintf('%s (#%d of %d)', fullFileName, k, numFiles);
title(caption, 'FontSize', 14, 'Interpreter', 'none');
drawnow; % Force display to update immediately.
% Write it out as a PNG file.
baseFileName = strrep(lower(baseFileName), '.tif', '.jpg');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end