Split image by the label and drop to the subfolder

4 views (last 30 days)
Hi. I'm working on a project and I need to split my dataset (images) by the labels in an Excel file and drop them to subfolders( eg. normal, diabetes, cataract,...). How can I split them by the labels?
I attach a screenshot to explain what I need.
Also I wrote following script for that, but faced an error which is: "Brace indexing is not supported for variables of this type."
My script:
datapath='C:\Users\FarHad\Downloads\Compressed\ODIRK\preprocessed_images';
% Two-class Data path
multi_class_datapath='C:\Users\FarHad\Downloads\Compressed\ODIRK\preprocessed_images\right';
% Class Names
class_names={'Normal','Diabetes','Glaucoma','Cataract','AMD','Hypertension','Myopia','OtherDiseases'};
mkdir(sprintf('%s%s',multi_class_datapath,class_names{1}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{2}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{3}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{4}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{5}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{6}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{7}))
mkdir(sprintf('%s%s',multi_class_datapath,class_names{8}))
% Read the Excel Sheet with Labels
[num_data]=xlsread('full_df.xlsx');
% Determine the Labels
train_labels=num_data(:,end);
% Filename
filename=num_data(1:end,2);
% Determine the Files put them in separate folder
for idx=1:length(filename)
% You could uncomment if you would like to see live progress
% fprintf('Processing %d among %d files:%s \n',idx,length(filename),filename{idx})[/%]
% Read the image
current_filename=strrep(filename{idx}, char(39), '');
img=imread(sprintf('%s%s.jpg',datapath,current_filename));
% Write the image in the respective folder
imwrite(img,sprintf('%s%s%s%s.jpg',multi_class_datapath,class_names{train_labels(idx)},'\',current_filename));
clear img;
end

Accepted Answer

yanqi liu
yanqi liu on 26 Nov 2021
clc; clear all; close all;
datapath='C:\Users\FarHad\Downloads\Compressed\ODIRK\preprocessed_images';
% Two-class Data path
multi_class_datapath='C:\Users\FarHad\Downloads\Compressed\ODIRK\preprocessed_images\right\';
% Class Names
class_names={'Normal','Diabetes','Glaucoma','Cataract','AMD','Hypertension','Myopia','OtherDiseases'};
for i = 1 : length(class_names)
fdi = fullfile(multi_class_datapath, class_names{i});
if ~exist(fdi, 'dir')
mkdir(fdi);
end
end
% Read the Excel Sheet with Labels
[num_data]=xlsread('full_df.xlsx');
% Determine the Labels
train_labels=num_data(:,end);
% Filename
filename=num_data(1:end,2);
% Determine the Files put them in separate folder
for idx=1:length(filename)
% You could uncomment if you would like to see live progress
% fprintf('Processing %d among %d files:%s \n',idx,length(filename),filename{idx})[/%]
% Read the image
current_filename=strrep(filename{idx}, char(39), '');
fi = fullfile(datapath, [current_filename '.jpg']);
if ~exist(fi, 'fi')
disp(fi);
error('can not find the file');
end
img=imread(fi);
% Write the image in the respective folder
fi2 = fullfile(multi_class_datapath, class_names{train_labels(idx)}, [current_filename '.jpg']);
imwrite(img,fi2);
clear img;
end
  6 Comments
yanqi liu
yanqi liu on 29 Nov 2021
current_filename=strrep(filename{idx}, char(39), '');
it used to replace '''
so,may be use
current_filename=strrep(filename{idx}, '''', '');
current_filename=strrep(filename{idx}, '\'', '');
yanqi liu
yanqi liu on 10 Dec 2021
now filename is number vector, so just use
current_filename=strrep(num2str(filename(idx)), '''', '');

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!