2

I am accessing images from following folders:

"D:/images/c1"
"D:/images/c2"
"D:/images/c3"

each folder has 10 images. I have used 3 for loops for accessing images from each respective folder. How do i do it in single loop?

My Code:
srcFiles = dir('D:\Images\c1\*.ppm');  
B = zeros(num_bins, 30); 
ptr=1;
for i = 1 : length(srcFiles)
    filename = strcat('D:\Images\c1\',srcFiles(i).name);
    I = imread(filename);
    B(:,ptr) = imcolourhist(I, num_red_bins, num_green_bins, num_blue_bins); 
    ptr=ptr+1;                                                   
end
for i = 1 : length(srcFiles)
    filename = strcat('D:\Images\c2\',srcFiles(i).name);
    I = imread(filename);
    B(:,ptr) = imcolourhist(I, num_red_bins, num_green_bins, num_blue_bins); 
    ptr=ptr+1;                                           
end
for i = 1 : length(srcFiles)
    filename = strcat('D:\Images\c3\',srcFiles(i).name);
    I = imread(filename);
    B(:,ptr) =imcolourhist(I, num_red_bins, num_green_bins, num_blue_bins); 
    ptr=ptr+1;                                                     
end

2 Answers 2

1

Code

%// List all paths
path1 = 'D:/images/c1'
path2 = 'D:/images/c2'
path3 = 'D:/images/c3'

%// Get all paths into one cell array
paths = cellstr(cat(1,path1,path2,path3))

%// Get all filenames (with their full paths) into one cell array
filename1 = cell(numel(paths),1);
for k1=1:numel(paths)
    filename1{k1} = fullfile(paths(k1),ls(char(fullfile(paths(k1),'*.ppm'))));
end
filenames = vertcat(filename1{:});

%// Your code modified according to the new path creating setup
B = zeros(num_bins, 30); 
for k2 = 1 : numel(filenames)
    I = imread(char(filenames(k2)));
    B(:,k2) = imcolourhist(I, num_red_bins, num_green_bins, num_blue_bins);
end

Improvements:

  1. No need to carry ptr as a count of filenames being processed.
  2. The first loop to get the full paths of all files to be processed is minimal and thus essentially you would be working with one loop.
Sign up to request clarification or add additional context in comments.

2 Comments

try porting code from linux to windows or vice-versa: you'll like this tool too.
@Shai I do! It takes care of the file-separator pretty nicely I think.
0

As far as I can tell, the only difference between each of your loops is filename. In that case, you can simply create an array containing 3 items (each containing the first half of your strcat function). You're then able to use nested for loops to achieve your desired result.

srcFiles = dir('D:\Images\c1\*.ppm');  
B = zeros(num_bins, 30); 
ptr=1;
sourceDir = ['D:\Images\c1\'; 'D:\Images\c2\'; 'D:\Images\c3\'];

for n = 1: length(sourceDir) 
    for i = 1 : length(srcFiles)
        filename = strcat(sourceDir,srcFiles(i).name);
        I = imread(filename);
        B(:,ptr) = imcolourhist(I, num_red_bins, num_green_bins, num_blue_bins); 
        ptr=ptr+1;                                                   
    end
end

It's been roughly 4 years since I've used MATLAB; so, I'm not 100% sure I have the syntax correct. Still, that's the method to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.