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.

16 views (last 30 days)
THUNAKALA BALA KRISHNA on 23 Sep 2021 at 3:59
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 on 23 Sep 2021 at 4:33
T = readtable('')
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('');
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);

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.
inputFolder = pwd;
outputFolder = uigetdir(pwd);
if strcmp(outputFolder, inputFolder)
errorMessage = sprintf('Error: the output folder must be different than the input folder');
% Check to see that both folders exist.
if ~isdir(inputFolder)
errorMessage = sprintf('Error: The following input folder does not exist:\n%s', inputFolder);
if ~isdir(outputFolder)
errorMessage = sprintf('Error: The following output folder does not exist:\n%s', outputFolder);
% 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);
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!