File does not go into right folder
古いコメントを表示
Hello ,
I am using this code which was written by another user for diabetic retinopathy detection. I am trying to split my dataset to either Yes or No folders, which is in a folder that is created by the code called 'Train Dataset Two Classes\'. The issue that I am facing is that it only writes the image to either only YES or NO. I have no idea why it does this.
Here's the code:
% Two-class Data path
two_class_datapath='Train Dataset Two Classes\';
myFolder = 'C:\Users\adamy\Desktop\MATLAB\train_images\';
% Class Names
class_names={'No','Yes'};
mkdir(sprintf('%s%s',two_class_datapath,class_names{1}))
mkdir(sprintf('%s%s',two_class_datapath,class_names{2}))
% Read the Excel Sheet with Labels
[num_data,text_data]=xlsread('train.xlsx');
% Determine the Labels
train_labels=num_data(:,1);
% Merge all labels marked into Mild, Medium, Severe and Proliferative DR
% into a single category 'Yes'
train_labels(train_labels~=0)=2;
% Rest of the dataset belongs to 'No' category
train_labels(train_labels==0)=1;
% Filename
filename=text_data(2:end,1);
% Now, write these images 2-folders 'Yes' or 'No' for us to develop a deep
% learning architecture utilizing Deep learning toolbox
% 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.png',myFolder,current_filename));
% Write the image in the respective folder
imwrite(img,sprintf('%s%s%s%s.png',two_class_datapath,class_names{train_labels(idx)},'\',current_filename));
clear img;
end
採用された回答
その他の回答 (1 件)
Image Analyst
2022 年 4 月 3 日
Exactly what does "split" mean to you? Do you have image files in one input folder, myFolder, and you want to copy (with copyfile) or create (with imwrite) into either a "Yes" subfolder or a "No" folder or subfolder?
I don't like making really long complicated filenames the way you did it:
imwrite(img,sprintf('%s%s%s%s.png',two_class_datapath,class_names{train_labels(idx)},'\',current_filename));
It's easy to make mistakes that way. Plus you didn't use fullfile() which nicely takes care of whether there is a slash between filename components so you don't have to worry about that. I'd do:
% Create input file name.
baseFileName = sprintf('%s.png', current_filename);
inputFullFileName = fullfile(myFolder, baseFileName);
fprintf('Reading in image : "%s".\n', inputFullFileName);
% Read in input image.
img = imread(inputFullFileName);
% Display input image (optional).
image(img);
axis('on', 'image');
impixelinfo;
drawnow;
% Save to output folder.
outputFolder = fullfile(two_class_datapath, class_names{train_labels(idx)});
outputFullFileName = fullfile(outputFolder, baseFileName);
fprintf('Saving image to : "%s".\n', outputFullFileName);
imwrite(img, outputFullFileName);
% Or alternatively you can use copyfile() instead of imwrite().
% copyfile(inputFullFileName, outputFullFileName)
3 件のコメント
adam yousry
2022 年 4 月 3 日
編集済み: adam yousry
2022 年 4 月 3 日
Image Analyst
2022 年 4 月 4 日
You have to look at your original train_labels and see why they are all non-zero. If you still need help, attach your original train_labels (before you to the reassignments you did above) in a .mat file.
save('answers.mat', 'train_labels')
or (probably better) attach 'train.xlsx'
adam yousry
2022 年 4 月 4 日
カテゴリ
ヘルプ センター および File Exchange で Get Started with Statistics and Machine Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!