Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

IMREAD error: Unable to determine the file format

Asked by Michael Martin on 5 Jan 2013

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

0 Comments

Michael Martin

2 Answers

Answer by 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 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 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.

Walter Roberson
Answer by 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 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)

Image Analyst

Contact us