Uigetdir error when reading images from different directory

3 次查看(过去 30 天)
I have written the below program to display 1000 images in sequence from a directory. Since I am storing the images in different folders I used the uigetdir command at the beginning of my program to select the folders from which the stored images are to be read. However, I get a "File img_00001.pgm does not exist" error when I run the code. The code without the uigetdir works fine without the uigetdir command but the code has to reside in the same folder as that of the image. Below is my code, kindly suggest what might be the mistake that I am committing:
% Read images image_00001.pgm through image_00999.pgm.
uigetdir
for k = 1:1000
% Create an image filename, and read it in to a variable called imageData.
if k<1001 %for images 0 to 9
pgmFileName = sprintf('img_%05d.pgm',k)
if exist(pgmFileName, 'file')
imageData = imread(pgmFileName);
imshow(imageData);
fname = sprintf('FrameNo.%d', k);
title(fname);
pause(0.1);
else
fprintf('File %s does not exist.\n', pgmFileName);
end
plot(imageData);
end
end

回答(2 个)

Geoff Hayes
Geoff Hayes 2014-8-28
Abhishek - while your code calls the function uigetdir, you don't make use of what is returned by it. At uigetdir doc, this function returns a string. You need to capture this string and use it with your file names. Something like
folderName = uigetdir;
% if the user presses cancel, then folderName is zero, so only
% continue if it is a string
if ischar(folderName)
for k = 1:1000
% create the filename with its path (folderName)
pgmFileName = fullfile(folderName, sprintf('img_%05d.pgm',k));
if exist(pgmFileName, 'file')
% etc.
end
end
end
Note that I removed your if k<1001 statement as it is unnecessary since your for loop iterates from 1 to 1000, so k will always be less than 1001.
Try the above and see what happens!
  2 个评论
Abhishek
Abhishek 2014-8-29
Dear Goeff,
Thank you for your answer and your interest in my problem. I tried your method but am still getting the same error ("File img_00000.pgm does not exist" and so on). I checked the value stored in the variable folderName and it is storing the correct folderName which I select. Can you please point what I might be doing wrong?
Below is the code which again runs fine without the uigetdir command:
folderName = uigetdir;
% if the user presses cancel, then folderName is zero, so only
% continue if it is a string
if ischar(folderName)
%for displaying images img_00000.pgm to img_00999.pgm
for k = 0:1000
if k<1000
pgmFileName = sprintf('img_%05d.pgm',k);
if exist(pgmFileName, 'file')
% Create an image filename, and read it in to a variable called imageData.
imageData = imread(pgmFileName);
imshow(imageData);
fname = sprintf('FrameNo.%d', k);
title(fname);
pause(0.1);
else
fprintf('File %s does not exist.\n', pgmFileName);
end
plot(imageData);
end
end
end
Geoff Hayes
Geoff Hayes 2014-8-29
Abhishek - you did not add the line of code that "concatenates" the folder name with the file name. Just replace
pgmFileName = sprintf('img_%05d.pgm',k);
with
pgmFileName = fullfile(folderName, sprintf('img_%05d.pgm',k));
Note that you do not need the if k<1000 condition. If you only want to evaluate the code (i.e. read the files) for k less than 1000, then just change the for loop to
for k=0:999

请先登录,再进行评论。


Image Analyst
Image Analyst 2014-8-29
Why not just use the second option in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F which uses dir() to process only those image files that are actually there ?

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by