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

10 次查看(过去 30 天)
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
  1 个评论
Mason Lien
Mason Lien 2020-11-14
maybe I'm overthinking this as I found some more information in this post - https://www.mathworks.com/matlabcentral/answers/30784-how-to-convert-a-matrix-to-a-gray-scale-image
ALSO - there is an error I made in the code when I was fiddeling with original.
imageArray = imread(mat2gray(fullFileName)); %wrong
imageArray = imread(fullFileName)); %correct
As you can tell, I'm inexperienced with this type of work.

请先登录,再进行评论。

采纳的回答

ICR
ICR 2020-11-14
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
  1 个评论
Mason Lien
Mason Lien 2020-11-14
Thank you, Ragupathy! You're edit did the trick. I removed some of the code that wasn't relevant to what I needed shown below.
% Specify the folder where the files live.
myFolder = '/Users/masonlien/Desktop/PhD/Drone/2020/20200425_Havelock_1033_RGB';
% 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);
imageArray = uint8(imread((fullFileName)));
% Choose what format you want to change it to .tif to .png, .jpg, etc.
baseFileName = strrep(lower(baseFileName), '.tif', '.jpg');
outputFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now writing %s\n', outputFileName);
imwrite(imageArray, outputFileName);
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by