Split image by the label and drop to the subfolder

11 ビュー (過去 30 日間)
Farhad Abedinzadeh
Farhad Abedinzadeh 2021 年 11 月 26 日
コメント済み: yanqi liu 2021 年 12 月 10 日
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

採用された回答

yanqi liu
yanqi liu 2021 年 11 月 26 日
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 件のコメント
yanqi liu
yanqi liu 2021 年 11 月 29 日
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 2021 年 12 月 10 日
now filename is number vector, so just use
current_filename=strrep(num2str(filename(idx)), '''', '');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by