MATLAB Answers

How to split a certain class of images in folder(image dataset contains more than 10,000 images) having different classes into separate folder or location.

10 views (last 30 days)
THUNAKALA BALA KRISHNA
THUNAKALA BALA KRISHNA on 23 Sep 2021
Answered: Image Analyst on 29 Sep 2021 at 13:03
i am having an image dataset having 12,400 images belongs to 6 different classes in a single folder.
i am having the 6 execl sheets with image_name their class.
i need to split the total image dataset into six folder of their repective class.

Answers (5)

KSSV
KSSV on 23 Sep 2021
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/747179/FETAL_PLANES_DB_data.xlsx')
Now you can extract what ever you want from T.


Image Analyst
Image Analyst on 26 Sep 2021 at 15:13
Use the FAQ to process a sequence of files:
Inside the loop get the class and determine the proper output folder from it. Call mkdir() to create the folder if it does not exist. Then create original and new full file names with fullfile() and other functions (like maybe fileparts or sprintf). Then use movefile() or copyfile() to move or copy the original file to the new class-specific folder. Write back if you can't figure it out.

yanqi liu
yanqi liu on 29 Sep 2021 at 7:23
sir,please check the follow code to get some information
clc; clear all; close all;
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/747179/FETAL_PLANES_DB_data.xlsx');
for i = 1 : size(T.Image_name, 1)
% Patient00001_Plane1_1_of_15
ni = T.Image_name{i};
nis = strsplit(ni, '_');
nit = str2num(strrep(nis{2}, 'Plane', ''));
fprintf('\nimage %s belong to class %d, please copy or move to the %d subfolder!\n', ni, nit, nit);
end

Image Analyst
Image Analyst on 29 Sep 2021 at 13:03
Turns out I already had a demo for this. See attached.
% Copies all the files from one folder to another folder.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format compact;
% Define input and output folders.
% CHANGE THESE FOLDER NAMES!!!!!!
inputFolder = pwd;
outputFolder = uigetdir(pwd);
if strcmp(outputFolder, inputFolder)
errorMessage = sprintf('Error: the output folder must be different than the input folder');
uiwait(warndlg(errorMessage));
return;
end
% Check to see that both folders exist.
if ~isdir(inputFolder)
errorMessage = sprintf('Error: The following input folder does not exist:\n%s', inputFolder);
uiwait(warndlg(errorMessage));
return;
end
if ~isdir(outputFolder)
errorMessage = sprintf('Error: The following output folder does not exist:\n%s', outputFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of files to copy.
filePattern = fullfile(inputFolder, '*.*'); % All files.
% filePattern = fullfile(inputFolder, '*.m'); % m-files.
fileNamesToTransfer = dir(filePattern);
numFiles = length(fileNamesToTransfer);
% Do the copying.
for k = 1 : numFiles
% Get the base file name.
baseFileName = fileNamesToTransfer(k).name;
% Create the full input and output filenames.
fullInputFileName = fullfile(inputFolder, baseFileName);
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf(1, 'Now copying file #%d of %d: %s to %s\n', ...
k, numFiles, fullInputFileName, fullOutputFileName);
copyfile(fullInputFileName, fullOutputFileName);
end
uiwait(msgbox('Done copying files!', 'modal'));

Community Treasure Hunt

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

Start Hunting!