How do I save multiple images after converting them?
1 view (last 30 days)
Show older comments
Vinay Chawla
on 15 Dec 2020
Commented: Vinay Chawla
on 15 Dec 2020
Hello,
I am trying to apply Median filter on a set of image data (50 Images) and I was wondering if someone can help me with the code to save each image after coonversion. I am using the following code and tool 'imsave' to save the images, but it is giving me an error
D = 'C:\Users\ChawlaV\Desktop\MATLAB\CNN Input 2\Alligator Cracks';
S = dir (fullfile(D,'*.png'));
for k = 1: numel(S);
F = fullfile (D, S (k).name);
I = imread (F);
grayImage = rgb2gray(I);
MFImage = medfilt2(grayImage);
path = ('C:\Users\ChawlaV\Desktop\MATLAB\CNN MF\Alligator Cracks'\'*.png*');
imsave(MFImage);
figure, imshow (MFImage);
end
%% ERROR
Thank you,
0 Comments
Accepted Answer
Image Analyst
on 15 Dec 2020
Don't use imsave(). And don't use path as the name of the variable - it's a very important built in global variable.
Try it this way, which has tons of improvements (like better variable names, using imwrite(), etc.).
inputFolder = 'C:\Users\ChawlaV\Desktop\MATLAB\CNN Input 2\Alligator Cracks';
outputFolder = 'C:\Users\ChawlaV\Desktop\MATLAB\CNN MF\Alligator Cracks';
fileListing = dir (fullfile(inputFolder,'*.png'));
for k = 1: numel(fileListing)
% Get the input filename.
inputFullFileName = fullfile (inputFolder, fileListing(k).name);
% Read in the original image from disk.
grayImage = imread (inputFullFileName);
% Display the image
subplot(1, 2, 1);
imshow(grayImage);
% Check to see if it's grayscale, like it should be.
if ndims(grayImage) == 3
% It's color. Need to convert it to gray scale.
grayImage = rgb2gray(grayImage);
end
% Median filter the image.
MFImage = medfilt2(grayImage);
% Display the image
subplot(1, 2, 2);
imshow(MFImage);
drawnow; % Force update.
% Save the image to the output folder.
fullFileName = fullfile(outputFolder, fileListing(k).name);
imwrite(MFImage, fullFileName);
end
More Answers (0)
See Also
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!