Uigetdir error when reading images from different directory

조회 수: 3 (최근 30일)
Abhishek
Abhishek 2014년 8월 28일
답변: Image Analyst 2014년 8월 29일
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