How to call a directory so that the function is applied on images in all folders and sub folders.

18 views (last 30 days)
Dear Team ,
I thought of implementing my search algorithm in my own system for searching a particular image from a drive. My D:/PHOTOS contains many folders and sub-folders all JPEG images.
I go by this - I extract the features of query image and search for similar images in My D:/PHOTOS drive. I've calculated the features of query image by calling a function 'rgbfeature'. Before searching i need to create a feature database. I'm facing trouble here, How would i call my function 'rgbfeature' so that it could extract the image features of all images in D:/PHOTOS which contains many folders and subfolders.
My piece of code where i'm facing issues
cd(fileparts(which(mfilename)));
start_path = cd;
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
allSubFolders = genpath(topLevelFolder);
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder), break; end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
NF = length(listOfFolderNames);
images = cell(NF,1);
for k = 1 : NF
images{i} = imread(fullfile(listOfFolderNames{k}, topLevelFolder(i).name));
B16{i} = rgbplaneavg(images{i});
end
end
end
My error Message:
??? Improper index matrix reference.
Error in ==> searchsubfolder at 72
images{i} = imread(fullfile(listOfFolderNames{k}, topLevelFolder(i).name));
Kindly request you to guide me .
Regards, Malini

Accepted Answer

Image Analyst
Image Analyst on 22 Mar 2013
Edited: Image Analyst on 23 Mar 2013
Perhaps because you're using "i" in a loop over "k". What is i? It's the imaginary variable (in more ways than one!). Shouldn't it be k? Plus you're using two folder names to build file names of images instead of using the dir() function.
Try this code:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all PNG and TIF images in
% that folder and all of its subfolders.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Define a starting folder.
start_path = fullfile(matlabroot, '\toolbox\images\imdemos');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
fprintf(' Processing image file %s\n', fullFileName);
end
else
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
  8 Comments
Keta shah
Keta shah on 24 Mar 2013
dear Image Analyst i got the following output of your code,i have two images of type png in that folder still it gives output like below
numberOfFolders = 1 Processing folder /Users/mananmehta/Desktop/cones: Folder /Users/mananmehta/Desktop/cones: has no image files in it. where i have to do change in my code.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!