MATLAB Answers

How to save resized image with original filename to a folder.

7 views (last 30 days)
Darren Brown
Darren Brown on 23 Oct 2020
Commented: Darren Brown on 29 Oct 2020 at 16:39
Hi there,
I am trying to save a resized (smaller) image to a folder. I am doing image classification and having the resized images would be helpful.
I would like to store the resized images into a folder called "resized" or something like that.
Any thoughts? Thank you for any help you can offer.
Code below. Comments are from some of my trying to work it out myself.
clc
clear all
close all
load('Train_AlexNet_Feature')
load('Train_AlexNet_Label')
Test_AlexNet_Feature=[];
BT50=[];
mkdir('New Folder')
ds = imageDatastore('/Users/kimpitman/Documents/MATLAB/Vehicle/Test');
a=ds.Files;
for i=1:length(a)
[I,info] = readimage( ds , i);
g=imresize(I,[227,227]);
imshow(g)
net=alexnet;
net.Layers;
layer='fc7';
F=activations(net,g,layer,'outputAs','rows');
Test_AlexNet_Feature=[Test_AlexNet_Feature;F];
SVMModel=fitcsvm(Train_AlexNet_Feature,Train_AlexNet_Label,'KernelFunction','Linear');
[label,score]=predict(SVMModel,Test_AlexNet_Feature);
label=label;
if label(i,:)=='1'
display('This is BT-50')
BT=info.Filename
BT=string(BT);
BT50=[BT50;BT];
imwrite(g,fName(i),'.png');
%srcFiles = dir('/Users/kimpitman/Documents/MATLAB/Vehicle/Test'); % the folder in which ur images exists
%for i = 1 : length(srcFiles)
%filename = strcat('/Users/kimpitman/Documents/MATLAB/Vehicle/Testcopiedimages',srcFiles(i).name);
%im = imread(filename);
%newfilename=strcat('/Users/kimpitman/Documents/MATLAB/Vehicle/Testcopiedimages',srcFiles(i).name);
%imwrite(k,newfilename,'jpg');
%end
%end
%newfilename=ds.Files('/Users/kimpitman/Documents/MATLAB/Vehicle/Testcopiedimages',srcFiles(i).name);
%imwrite(g,newfilename,'jpg');
%imwrite(g,ds);
else
display('This is not BT-50')
BT=info.Filename
BT=string(BT);
BT50=[BT50;BT];
end
end
save('BT50')
T=table(BT50,label);
fname=sprintf('LinearTestMix159%s.xlsx',datestr(now, 'yyyymmddHHMM'));
writetable(T,fname,'Sheet',1)

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2020
Try this:
% Define input folder location.
folder = '/Users/kimpitman/Documents/MATLAB/Vehicle/Test';
% Create output folder name.
outputFolder = fullfile(folder, '/resized');
% Make output folder if it exists.
if ~isfolder(outputFolder)
mkdir(outputFolder)
end
ds = imageDatastore(folder)
% Loop over all files.
for k =length(ds.Files)
% Get this filename.
thisFileName = ds.Files{k};
fprintf('Processing %s...\n', thisFileName);
% Read in the input image.
originalImage = imread(thisFileName);
% Display it.
imshow(originalImage);
drawnow;
% Find out just the base filename without the folder.
[thisFolder, baseFileNameNoExt, ext] = fileparts(thisFileName);
% Create output image by resizing....
fprintf(' Resizing %s...\n', baseFileNameNoExt);
resizedImage = imresize(originalImage, [227, 227]);
% Create the output filename which will be in the output folder.
outputFileName = fullfile(outputFolder, [baseFileNameNoExt, ext]);
% Write the image out to the output folder.
fprintf(' Saving %s...\n', outputFileName);
imwrite(resizedImage, outputFileName);
end
message = sprintf('Done processing %d images', length(ds.Files))
uiwait(helpdlg(message));

  3 Comments

Darren Brown
Darren Brown on 23 Oct 2020
WOW "Image Analyst"! I will try this out tomorrow. It is 3:07am here in Brisbane, Australia. I don't trust myself... I'd probably wake up in the morning wondering why the heck my toaster is wondering what a filename is.
Thank you in advance! I'll let you know how I go.
Darren Brown
Darren Brown on 29 Oct 2020 at 16:35
Magic! Thank you Image analyst.
Now I just need to work out how to integrate it into the original code (I am new to coding).
Thank you again for your help! I am grateful how quickly you were able to respond and with a solution!
Darren Brown
Darren Brown on 29 Oct 2020 at 16:39
Oh, I just needed to put a "1:" to get it to work.
Thank you again for your help. Sorry for my late update.
%original code:
for k =length(ds.Files)
end
%changed to:
for k =1:length(ds.Files)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!