How can I make the code work for any format, not just .jpg?

Hello - I found this code from http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F and although it says that any format should work, it will not read any .tif files. Instead it results in the following errors -
  • Error using imread>get_format_info (line 491)Unable to determine the file format.
  • Error in imread (line 354) fmt_s = get_format_info(fullname);
  • Error in Line (line 16) imageArray = imread(fullFileName);
There are no errors when the code is used with .jpg files. Here is the code
% function Line(~,~)
% Specify the folder where the files live.
myFolder = 'C:\Users\Vicki\Documents';
% 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', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
end
end
end
Any help is much appreciated.

3 Commenti

That error arises when you attempt to open a non-image file. Are you able to open that tif image file via another image-viewing software? Are you able to open a specific tif file via this command?
Im = imread('C:\Users\Vicki\Documents\image.tif')
Yes, I can open all the tif images on Photoshop or just plain photo viewer. I also tried your command and it read the tif file without a problem.
For debug, do this:
try
imageArray = imread(fullFileName);
catch
warning('Could not open: %s', fullFileName);
end
Which files are failing to open?

Accedi per commentare.

 Risposta accettata

Victoria
Victoria il 27 Giu 2018
Modificato: Image Analyst il 28 Giu 2018
I have fixed this problem myself. For whatever reason, it was only that specific folder that wasn't opening the .tif files. All the other folders worked. Thanks for the help.

6 Commenti

This is not a complete "Answer". Can you tell us what you did to fix the folder or the images?
Victoria
Victoria il 29 Giu 2018
Modificato: Victoria il 29 Giu 2018
I didn't do anything to the code. Only one folder didn't work, for reasons unknown. All I did was move the images to another folder and it worked just fine.
That is a contradictory statement... So you did do something, but not with the code. Were those .tif files corrupt? Perhaps the image files that were generated were corrupt. In that case, using a try/catch statement to bypass your corrupt images will make your code more resistant to error.
Imagine if randomly, matlab stops and starts working, and you didn't do anything different... It'll drive a lot of people crazy.
What language do you use? Did the folder have any unicode or non-English characters in it? I think I've seen problems with that. But it must be something like that because the image itself seems to be fine.
These are the exact steps I took: I created a new folder, copied and then pasted my .tif files into the new folder. I use English. There were no non-English characters in it. I wish I knew why that old folder didn't work. Maybe it's because the old folder was a mix of .tif and .jpg files, but then I wouldn't understand why .jpg files would work.
No, mixing file types would not cause the problem.
Some things to try if you want,or are curious and want to avoid the problem in the future:
  1. If you move the files back into the folder of the same name (if you still have it), does the problem reoccur?
  2. If you make a folder with that name on a different drive, or as a subfolder of another folder, do you see the problem with that image file?
  3. I could try to reproduce with the same image file (if you attach it) and a folder of the same name on my computer.
Otherwise, I guess it will have to remain a mystery.

Accedi per commentare.

Più risposte (1)

My guess is that you really did . or * instead of *.tif and then you tried to read a non-image file. Or else the file has a .tif extension but is really NOT a tiff file. See if you can open the file in Photoshop or GIMP.

2 Commenti

Unfortunetly, I did do *.tif. I just rechecked. Additionally, I opened all the tiff files I'd like to use in this code on Photoshop and they open just fine.
Attach one or more of the tiff files that imread() cannot open.

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by