IMREAD error: Unable to determine the file format

167 views (last 30 days)
I am having trouble getting a code to finish without error. The error message is with IMREAD.
Here's an overview of my code & problem:
I have 41 images in 9 different folders. My code iterates through each folder, and thus each image. During the iteration IMREAD reads an image, alters it, then IMWRITE writes the altered file in the same directory under a different file name such that the original images and new written images are now in that directory.
The image directories look like this:
/Users/imagecomputer/Desktop/Folder/Test1/1
/Users/imagecomputer/Desktop/Folder/Test1/2
/Users/imagecomputer/Desktop/Folder/Test1/3
/Users/imagecomputer/Desktop/Folder/Test2/1
/Users/imagecomputer/Desktop/Folder/Test2/2
/Users/imagecomputer/Desktop/Folder/Test2/3
/Users/imagecomputer/Desktop/Folder/Test3/1
/Users/imagecomputer/Desktop/Folder/Test3/2
/Users/imagecomputer/Desktop/Folder/Test3/3
In each directory are 41 images. All .tiff files. Each set of images also have the same naming scheme: 0.tiff : 40.tiff
The code successfully iterates through /.../Test2/3
At /.../Test3/1 images 0:9 are ran through the code, then this error message comes up:
Error using imread (line 381)
Unable to determine the file format.
Error in pathwayVersion02 (line 36)
oldimage=imread(['',file_names(s).name]);
...
Here is my code:
R = {'3' '3' '3'};
r = length(R);
Images=41;
folderbase = {'Test1','Test2','Test3'};
filepath = '/Users/imgcompute/Desktop/Mock2/';
m = length(folderbase);
B = rdir('/Users/imgcompute/Desktop/Mock2/Test2/Half_Frames/');
lengthB = length(B);
for j = 1:m
filename1=sprintf('/Users/imgcompute/Desktop/Mock2/%s/',char(folderbase(j)));
Half_Frames = 'Half_Frames/';
filename1 = strcat(filename1, Half_Frames);
for rr = 1:r
rr = num2str(rr);
C = char(strcat(filename1,rr,'/'))
file_count=0;
file_names = rdir(C);
for s = 1:length(file_names)
if file_names(s).bytes >=10000;
oldimage=imread(['',file_names(s).name]);
newimage=uint16(oldimage.*8);
file_count = num2str(file_count);
imwrite(newimage,[C,file_count,'_8x.tiff']);
a=['This is pic num',num2str(file_count)];
file_count=str2double(file_count);
file_count=file_count+1;
end
end
end
% code
end
  2 Comments
Kurt
Kurt on 5 Jan 2024
This can also happen if there is no valid file info in the image. I downloaded a bunch of .png images and used Windows to copy them into .png files. I was able to view the images, and they seemed legit. However, when I tried to use imread on the images, I got this same error.
The solution I found was to read each image into Microsoft Paint, then save them specifically as .png images. After that, the imread function worked.
Apparently Windows doesn't care about the stored image info - it just uses the .png file extension. Matlab is a little more picky, and requires that the file format be stored internally in the image.
Walter Roberson
Walter Roberson on 5 Jan 2024
Looking back at this code...
We do not know what rdir() does. It appears to work in a manner similar to dir()
It looks to me to be plausible that rdir() is returning directory entries along with filename entries -- dir() would return directory entries. So it looks to me as if the code would be asking to imread() the '.' and '..' entries.
But as well the imread() is asking to pull the file out of the current folder; we can see that the files are generally in other folders.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 5 Jan 2013
Type
dbstop if error
at the command line, and re-run. When the program stops, determine the file name that is giving the problem. Try to imread() that file at the command line. Copy the filename, and close MATLAB and restart, and then try to imread() that same file. If both times the read fails, you can be sure that it is something in the file rather than a difficulty with running out of some kind of resource because of the loop.
There is a possibility that you might find an unexpected file in that directory that is not an image file.
If it does look like a real image file, does Windows Explorer have problems opening it?
Try using imfinfo() on the file. Try it including 'tiff' as the format, and try it with 'jpg' as well, in case the file is a real image but not a tiff image.
  2 Comments
Michael Martin
Michael Martin on 7 Jan 2013
Thanks for the advice! I ran the code, isolated the error and ran IMREAD on the image file I was getting an error on...It turns out that when my code runs through the directory list, I am getting the error because the iteration step is reading the hidden ".DSStore" file that's in the directory. So, the problem was with the images, but rather the file IMREAD was trying to read was hidden.
I thought I accounted for it by my IF statement (.DSStore files had low byte values), but apparently I was wrong. This file, as well as the "." and ".." files that are in every directory are difficult to deal with, but I think I found my solution.
Thank you!
Michael
Image Analyst
Image Analyst on 7 Jan 2013
I still don't know why you don't just set up the file pattern so that you get only files with the proper extension like I suggested.

Sign in to comment.


Image Analyst
Image Analyst on 5 Jan 2013
Replace this line:
file_names = rdir(C);
with this:
file_names = dir('*.tif');
and try again. And then this line - not sure what problems that might cause with the null prepended - maybe none, but try this more robust way.
folder = filename1;
fullFileName = fullfile(folder, file_names(s).name);
oldimage=imread(fullFileName );
Also, you might want to look at genpath() to generate all subfolders automatically.
  1 Comment
Walter Roberson
Walter Roberson on 5 Jan 2013
['',string] is the same as string, provided that string is char data type -- even if string is a strangely-sized empty string such as ones(1,1,0)

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!