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 CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by