Unable to determine the file format error. How can i solve this in matlab?

11 views (last 30 days)
I've made code for face recognition, but i get this error. Anyone help me? Here is my code:
function histSLGS = FP_FaceRecognition_Neat
directory = uigetdir;
imageVolum = [];
for i = 1:23
%imageVolume = [];
%directoryFinal = cell(23,1);
garing = '\';
extension = '.txt';
strI = num2str(i);
directoryFinal = strcat(directory,garing,strI);
fileList = dir(directoryFinal);
%fl = fileList
%DF = directoryFinal
%a = numel(fileList)
for idx = 3:numel(fileList)
tempImage = imread(fullfile(directoryFinal, fileList(idx).name));
imageVolum(:,:,:,idx) = tempImage;
end
%sI = size(imageVolum)
for j = 3:33
strJ = num2str(j);
garis = '_';
fileNameFinal = cell(30,1);
fileNameFinal = strcat(directoryFinal,garing,strI,garis,strJ,extension);
imgManipulation3 = SLGS(imageVolum(:,:,:,j));
histSLGS = hist(imgManipulation3);
[rowhist, columnhist] = size(histSLGS);
%r = rowhist
%c = columnhist
%fn = fileNameFinal
fid = fopen(fileNameFinal, 'w');
for l = 1:rowhist
for k = 1:columnhist
fprintf(fid, '%.3f, ', histSLGS(l,k));
end
fprintf(fid, '\n');
end
fclose(fid);
histSLGS;
end
end
I got this error message:
Unable to determine the file format.
Error in ==> FP_FaceRecognition_Neat at 18 tempImage = imread(fullfile(directoryFinal, fileList(idx).name));
Your help is very hopeful.. :) Thanks..

Answers (1)

Guillaume
Guillaume on 4 Dec 2014
You're just doing a plain dir, listing all files and directories in directoryFinal and passing each of these to imread. Of course, it's going to choke if one of those is a directory or a non-image file.
Either wrap the content of the for loop into a try ... catch statement, or restrict your dir to image files.
----
Side note: I would also reorganise the code to move variable declaration to just before where it's needed (e.g. extension), use fullfile to build path rather than hardcode the path separator, use sprintf to build filenames, and get rid of the preinitialisations to cell arrays that get immediately overwritten by a string (e.g. filenamefinal).
  2 Comments
Petrus Gumarang
Petrus Gumarang on 4 Dec 2014
sorry, i don't understand your answer enough.. Can you explain me details? Sorry.
Guillaume
Guillaume on 4 Dec 2014
You're getting the content of directoryFinal with this line:
fileList = dir(directoryFinal);
where directoryFinal is
'somepath\1' %or 'somepath\2' or 'somepath\3', etc.
You then pass this content to imread. If, for example, in that directory you have a text file starting with one
1doesnotmatter.txt
or a directory starting with one
1doesnotmatter\
This is going to be passed to imread which won't know what do with it and tell you it can't determine the image format.
To fix it, either only list the file of the image format you're using, e.g. for png:
fileList = dir(fullfile(directory, sprintf('%d*.png', i)))
Or wrap the content of the for loop processing each file in a try catch statement:
for idx = 3:numel(fileList)
try
tempImage = imread(fullfile(directoryFinal, fileList(idx).name));
imageVolum(:,:,:,idx) = tempImage;
catch exception
if strcmp(exception.identifier, 'MATLAB:imagesci:imread:fileFormat')
warning('skipped reading %s. Not an image', fileList(idx).name);
else %some other unknown error. stop execution
rethrow(exception);
end
end
end

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!