How do I save multiple images after converting them?

4 次查看(过去 30 天)
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,

采纳的回答

Image Analyst
Image Analyst 2020-12-15
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by