How do I save multiple images after converting them?

2 vues (au cours des 30 derniers jours)
Vinay Chawla
Vinay Chawla le 15 Déc 2020
Commenté : Vinay Chawla le 15 Déc 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,

Réponse acceptée

Image Analyst
Image Analyst le 15 Déc 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
  1 commentaire
Vinay Chawla
Vinay Chawla le 15 Déc 2020
Thank you so much. It worked.
I really appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by