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;endfor 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