How to save multiple outputs of segmented in a different folder

Hello!
I have segmented 104 images, I need those segemted images to be saved in separate folder. How do I do that. Could you please help me with that.
The code used as follows
clc;
image_folder='C:\Users\Abinaya Giri\OneDrive\Desktop\Segmentation\Test_C1';
filenames = dir(fullfile(image_folder,'*.png'));
total_images = numel(filenames);
for n = 1:total_images
f=fullfile(image_folder,filenames(n).name);
I = imread(f);
s = rgb2gray(I);
imshow(s>115 & s<210);
figure(n)
imshow(s)
path = strcat('C:\Users\Abinaya Giri\OneDrive\Desktop\Segmentation\Segmented',image_folder.name);
imwrite(I,path);
end
Thanks in advance.

 Accepted Answer

Hi Srinidhi,
For saving a particular file in different folder you may use this:
save(['C:\Users\Abinaya Giri\OneDrive\Desktop\Segmentation\Segmented' filenames(1).name])
By this you will save a file of name filenames(1).name to the existing folder 'C:\Users\Abinaya Giri\OneDrive\Desktop\Segmentation\Segmented'
Note: Folder should already exist
Hope this helps!

7 Comments

I have taken 5 images instead of 104 images. The 5 images are in the folder name Original and I want the 5 segmented image to be saved in the folder named Apple. I have replace the code you sent like but still it not working. I am not sure if I have done a mistake. Could you help me out.
clc;
image_folder='C:\Users\Abinaya Giri\OneDrive\Desktop\Example\Original';
filenames = dir(fullfile(image_folder,'*.png'));
total_images = numel(filenames);
for n = 1:total_images
f=fullfile(image_folder,filenames(n).name);
I = imread(f);
s = rgb2gray(I);
imshow(s>115 & s<210);
figure(n)
imshow(s)
save(['C:\Users\Abinaya Giri\OneDrive\Desktop\Example\Apple' filenames(1).name])
end
Hi Srinidhi,
Can you share some snippets of error.
Thank you
I am not getting an error in command window. But the code runs and the images are getting segmented anD the last image doesn't. One original imge from set of 5 images is getting saved in the main folder ie the EXAMPLE FOLDER but that image is saved in unsupported format. I want the sgmented images to be stored in the APPLE SUBFOLDER. I have the matlab window snip where in the CURRENT FOLDER the saved image is shown as APPLECOVID(1) and aslo the snippet of the image when its opened.
Hi Srinidhi,
You may try this, This will produce files correctly:
image_folder='C:\Users\Abinaya Giri\OneDrive\Desktop\Example\Original\';
filenames = dir(fullfile(image_folder,'*.jpg'));
total_images = numel(filenames);
for n = 1:total_images
f=fullfile(image_folder,filenames(n).name);
I = imread(f);
s = rgb2gray(I);
figure(n)
imshow(s>115 & s<210);
baseFilename = filenames(n).name;
fullFilename = ['C:\Users\Abinaya Giri\OneDrive\Desktop\Example\Apple\' baseFilename];
saveas(figure(n), fullFilename);
end
Hope this Helps!
It worked perfectly saving the images in desired folder but the segmented images weren't saved instead the original images were saved. So now in the both Original and the Apple subfolders has the same original image
Hi Srinidhi,
Updated my comment above
Previously you had overwrited the imshow(s) with imshow(s>115 & s<210). So, whatever file in the figure will be saved.
Thanks
It was perfect.....Thanks for your time

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type 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!