2014-02-07 2 views
1

여러 개의 폴더가있는 디렉터리가 있습니다. 각 폴더에는 여러 이미지가 있습니다.하위 디렉터리에서 이미지 읽기

이미지가 포함 된 디렉토리가 있다고 가정하면이 이미지를 읽는 방법을 알고 있습니다 (아래 코드 참조). 이러한 이미지가이 디렉토리의 하위 디렉토리에 있다고 가정하면 코드를 편집하는 방법을 모르겠습니다.

directory = 'my_direct'; 
dnames1={directory}; 
c{1} = dir(dnames1{1}); % struct array with 5 fileds (name, isdir,...) 

if length(c{1}>0) 
    if c{1}(1).name =='.' 
     c{1} = c{1}(3:end); %ignore the '.' and '..' 
    end 
end 

for k = 1: length(c{1}) 
    image= double(imread([dnames{1} '/' h{1}(L).name]))./255; 
end 

답변

2

당신은 여러 이미지를 읽기 위해이 코드를 사용할 수있는 각 하위 디렉토리 내에서 이동 한 이미지

Root_directory='The directory location'; 
sub_directories=dir(Root_directory); 
sub_directories(1,2)=[]; % to remove . and .. 
for sub_dir_index=1:length(sub_directories) 
    images=dir(fullfile(Root_directory,sub_directories(sub_dir_index).name)); 

    the rest of your code 

end 
1

을 읽을 수 있다고 가정, 당신이 알아야 할 모든 디렉토리의 경로입니다. 그냥 경로를 작성하면 코드가 저장 한 패턴에 따라 이미지를 읽습니다. 당신이 유용하다고 생각하기를 바랍니다.

imgSets = imageSet('my_direct','recursive') 

imgSets가 포함되어 각각의 imageSet 객체의 배열이 될 것입니다 : 당신이 컴퓨터 비전 시스템 도구 상자와 MATLAB의 R2014b 자료가있는 경우

myFolder = 'path'; 
if ~isdir(myFolder) 
    errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder); 
    uiwait(warndlg(errorMessage)); 
    return; 
end 
filePattern = fullfile(myFolder, '*.jpg'); 
jpegFiles = dir(filePattern); 
for i = 1:length(jpegFiles) 
    baseFileName = jpegFiles(i).name; 
    fullFileName = fullfile(myFolder, baseFileName); 
    fprintf(1, 'Now reading %s\n', fullFileName); 
Input_image = imread(fullFileName); 
end 
+0

감사합니다. :) – Christina

1

, 당신은 imageSet 객체를 사용할 수 있습니다 하위 디렉토리에있는 모든 이미지 파일의 경로는 my_direct입니다. 그런 다음 i 번째 하위 디렉토리에서 다음과 같이 j 번째 이미지를 읽을 수 있습니다.

im = read(imgSets(i), j); 
관련 문제