Filter löschen
Filter löschen

How do I save multiple images after converting them?

4 Ansichten (letzte 30 Tage)
Vinay Chawla
Vinay Chawla am 15 Dez. 2020
Kommentiert: Vinay Chawla am 15 Dez. 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,

Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Dez. 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

Weitere Antworten (0)

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by