MATLAB: How to select only those image folders containing images more than 100. From a set of 193 image folders.

Computer Vision Toolboxfetch only required folders from dataset.

Here i have 193 folders, where in each folder contains a set of images. In each folder, total number of images are not the same. It is like 15, 28, 64, 96, 125, 265, 456, 564, and so on. Now i need to take only those folders where my images count is more than 100.(out of these 193 folders). Then splitting it into training and testing. How i can do it? I tried this code. but its not working properly. Please help.
SetDir = fullfile('D:\MATLAB\Proj_Work\seperated');
Imds = imageDatastore(SetDir,'IncludeSubfolders',true,'LabelSource','foldernames');
T = countEachLabel(Imds);
minSetCount = 100;%min([T.Count]);
Imds_New = splitEachLabel(Imds,minSetCount,'randomize');
[trainingSet,testSet]=splitEachLabel(Imds_New,0.7,'randomize');

Best Answer

  • karthik, try this:
    SetDir = fullfile('D:\MATLAB\Proj_Work\seperated'); % My Main folder path.
    imds = imageDatastore(SetDir,'IncludeSubfolders',true,'LabelSource','foldernames'); % All Sub folders with its labels. (193 folders, images in each folder along with its label)

    % Get the total number of files in each folder.
    T = countEachLabel(imds); % Total count in each folder
    % See which folders have at least 100 files in them.
    foldersWithEnoughFiles = T.Count >= 100;
    % Build a list (cell array) of all the folders.
    allFolderList = cell(size(T, 1), 1);
    for k = 1 : size(T, 1)
    [thisFolder, ~, ~] = fileparts(imds.Files{k});
    allFolderList{k} = thisFolder;
    end
    % Extract only those folders with 100 files or more.
    goodFolders = unique(allFolderList(foldersWithEnoughFiles));
    % Get a new datastore with only those folders with 100 or more files in them.
    imds_good = imageDatastore(goodFolders,'IncludeSubfolders',false,'LabelSource','foldernames'); % All Sub folders with its labels. (193 folders, images in each folder along with its label)
    % Split all files into a test set and a training set.
    splitPercentage = 0.8;
    [imds_train, imds_test] = splitEachLabel(imds_good, splitPercentage, 'randomize');