how to access images from sub folders?

7 ビュー (過去 30 日間)
sidra Rafique
sidra Rafique 2018 年 9 月 14 日
編集済み: Stephen23 2018 年 10 月 1 日
hi. i have a folder F1 and then in this folder i have S1 and S2 afterwards in each S1 and S2 i have E1 and E2 finally in these folders i have images named as frame_0000.jpeg till frame_2000.jpeg. for better understanding i am attaching a pic of my hierarchy.
after accessing each image i have to convert them from rgb2gray, apply my required preprocessing and then make excel file of each images in 'E' folders. Please help me accessing these images. waiting for your kind response.

採用された回答

Stephen23
Stephen23 2018 年 9 月 29 日
編集済み: Stephen23 2018 年 9 月 29 日
Using dir:
D = 'path to directory F1';
S = dir(fullfile(D,'S*'));
for ii = 1:numel(S)
E = dir(fullfile(D,S(ii).name,'E*'));
for jj = 1:numel(E)
F = dir(fullfile(D,S(ii).name,E(jj).name,'frame*.jpg'));
for kk = 1:numel(F)
N = fullfile(D,S(ii).name,E(jj).name,F(kk).name);
im = imread(N);
end
end
end
Using cell arrays and sprintf:
F = 'path to directory F1';
S = {'S1','S2'};
E = {'E1','E2'};
V = 0:2000;
for ii = 1:numel(S)
for jj = 1:numel(E)
for kk = 1:numel(F)
N = fullfile(D,S{ii},E{jj},sprintf('frame_%04d.jpg',V(kk)));
im = imread(N);
end
end
end
See also:
  10 件のコメント
sidra Rafique
sidra Rafique 2018 年 9 月 30 日
yes. i mean workbook and worksheets. you are right.
Stephen23
Stephen23 2018 年 10 月 1 日
編集済み: Stephen23 2018 年 10 月 1 日

@sidra Rafique: it seems that you want to use the S loop to define the filename, and the E loop to define the sheet name, something like this:

D = 'path to directory F1';
S = dir(fullfile(D,'S*'));
for ii = 1:numel(S)
    fnm = sprintf('%s.xlsx',matrix,S(ii).name);
    E = dir(fullfile(D,S(ii).name,'E*'));
    for jj = 1:numel(E)
        sht = E(jj).name;
        F = dir(fullfile(D,S(ii).name,E(jj).name,'frame*.jpg'));
        out = cell(1,numel(F))
        for kk = 1:numel(F)
            N = fullfile(D,S{ii},E{jj},sprintf('frame_%04d.jpg',V(kk)));
            im = imread(N);
            out{kk} = ... whatever data you want to save
        end
        mat = vertcat(out{:});
        xlswrite(fnm,mat,sht,...)
    end
end

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2018 年 9 月 14 日
A simple google will give you lot of links to this question. This question is discussed and asked lot many times.
  4 件のコメント
sidra Rafique
sidra Rafique 2018 年 9 月 29 日
By using KSSV's approach i can get count of the images and that count cannot be used as argument in 'imread' command.(i am hoping you have read KSSV's prescribed code). i have to read each image using 'imread' and later i have to make a excel sheet of name of the each folder and files and their corresponding values. but if you have any other suggestion then please do share it with me or make changes in my switch statements. So that i can work further as i am stuck in this for last 4 weeks. thankyou
Image Analyst
Image Analyst 2018 年 9 月 30 日
Looks like this doesn't apply anymore since you accepted the other answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by