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

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

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

Dear Image Analyst,
Thank you for your immediate response. Yes it has to be 'k'. Sorry for my mistake. Your code has actually listed all the image file names in all folders and sub-folders in D:/PHOTOS. I just modified the last for loop to read each image and extract its features using below code.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
% fprintf(' Processing image file %s\n', fullFileName);
A{f}=imread(fullFileName);
B16{f} = rgbplaneavg(A{f});
end
Code is working perfectly fine as expected. I'm able to extract the features of all images in D:/PHOTOS. I compared the query image against the feature database and found few images which match with the query image. I now have to display the relevant retrieved images using subplot. How do i display those retrieved images? Request you to kindly guide me .
My code
for i = 1:length(matching_images)
filename = num2str(matching_images(i));
filename = ['' filename];
filename = ['%s\*.jpg' filename];
I3 = imread(filename);
subplot(6,5,i);
imshow(I3);
title(['path of folder ',num2str(matching_images(i))]);
end
I know i'm wrong in the way of pointing the path of matched images. But how do i do ? please guide me .
Regards, Malini
Other than creating the filename kind of weird, and not using fullfile() and exist(), what's wrong? What does "wrong in the way of pointing the path" mean?
Dear Image Analyst,
I've 5 sub folders in a folder each sub-folder containing 5 images with image label as a mix of names and numbers(strings) and 6 images in the main folder.
1. When i search for similar images which are relevant to query image based on distance measures, i get a list of images in the form of numbers.But i think that the search is done only to images in the main folder and the search is ignoring the sub folders. (for loop is operating only on main folders and not sub folders - i think the call to 'rgbplaneavg' is not placed in proper for loop ) as the distance is calculated only for 6 images instead of 31 images.
2. And also the retrieved images are in numbers and so i'm getting confused as the number refers to which images and in which subfolder. So i thought of displaying the images to solve this issue by giving its corresponding location path as title. I request your help here.
Eg:
Main Folder Name(6 images) :testdb (101.jpg,102.jpg,103.jpg,104.jpg,105.jpg, 106.jpg)
sub folders
people1 - A.jpg , B.jpg, C.jpg, D.jpg, E.jpg
people2 - A.jpg , B.jpg, C.jpg, D.jpg, E.jpg
building - 2.jpg, 3.jpg,4.jpg,5.jpg,6.jpg
vehicles - 344.jpg, 345.jpg, 346.jpg, 359.jpg, 360.jpg
motorcycle - 2.jpg, 3.jpg, 6.jpg, bus.jpg, van.jpg
My query - people1(A.jpg)
distance calculated = 11.961 1.416 22.152 29.500 4.199 14.503
retrieved images = 1 2 3 4 5 6
when the folder is sequentially number then , i dont have problem in identifying the image . But if the folder is of this type , then how do i identify which images are retrieved?Request your valuable suggestions and also Kindly request you to guide me.
Regards, Malini
Please reconcile this: "i dont have problem in identifying the image" with this contradictory statement: "how do i identify which images are retrieved". You have the full file name (folder + base file name) of all images so it should not be a problem to know which images you are dealing with.
Please reconcile this: "i dont have problem in identifying the image" with this contradictory statement: "how do i identify which images are retrieved". You have the full file name (folder + base file name) of all images so it should not be a problem to know which images you are dealing with.
Dear Image Analyst,
I meant, previously when i tested on a folder containing 1 to 100 images all numbered in sequential order and no subfolders in it,I was able to retrieve the relevant images.
But, now when i'm testing on a folder with subfolders and image labels coinciding between folders though the image differs, I donno how to identify which image is retrieved and from which folder. ( For eg: folder1 may have a bus image named as 1.jpg and folder2 may have an apple image named as 1.jpg) . If one of the images in the retrieved images list 1.jpg how do i identify which folder is this 1.jpg pointing to?
Regards, Malini
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)

Categories

Products

Community Treasure Hunt

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

Start Hunting!