Adding Noise To A Folder OF Images in A Loop
7 views (last 30 days)
Show older comments
Good day all,
I am trying to understand a FOR LOOP to read all of the images in a specific folder, ADD NOISE TO The Folder OF Images at once and then Write the noise images to another specific folder,
I read the matlab documentation to help my own self but I am stuck at the sprint f statement and cannot move onward
question:
- the sprintf s%glow%d.jpg should output glow1.jpg?
- some how the common g is being taken for %g function and output low1.jpg??!!!!
- required some understanding why this outputs 1 noise file rather than 53 different noise images?
my code: Please Help!
D = '/Users/mmgp/Documents/MATLAB/FTruth53/FTruth53Gnoise5/'
S = dir(fullfile(D,'*.jpg'));
for k = 1: numel(S)
F = fullfile(D,S(k).name);
I = imread(F);
imshow(I);
S(k).data = I;
end
imname = dir(fullfile(D, sprintf('*/*',k)));
numel(imname)
% Assume only the first image is relevant (just for example purpose).
imname = imname(1).name;
for j = 1:length(F)
%apply the noise
J = imnoise(I,'salt & pepper',noise(1));
%save image in the right folder
imwrite(J, fullfile('/Users/mmgp/Documents/MATLAB/FTruth53/FTruth53Gnoise5/WithNoise/data1', sprintf('gun%d.jpg', j, imname)));
end
2 Comments
Rik
on 27 May 2020
Have you read the documentation for sprintf? What part didn't you understand?
By now someone must have mentioned you should be using break points if you want to understand what your code is doing. Did you do that?
Accepted Answer
Rik
on 27 May 2020
If you don't understand how a function works you should read the documentation. It contains several examples and shows you how to use it and what every option does. sprintf will create a char array from inputs. Inputs are indicated with a % symbol. All options are described in the documentation.
To the point of your code: in your first loop you have already loaded the data. Why are you now going to loop through F, which is a file name? You only need an output file name, which you can construct the same way you created your input file name.
D = '/Users/mmgp/Documents/MATLAB/FTruth53/FTruth53Gnoise5/'
S = dir(fullfile(D,'*.jpg'));
for k = 1: numel(S)
F = fullfile(D,S(k).name);
I = imread(F);
imshow(I);
S(k).data = I;
end
for j = 1:length(S)
I=S(j).data;
%apply the noise
J = imnoise(I,'salt & pepper',noise(1));
%save image in the right folder
F = fullfile(D,'WithNoise','data1',sprintf('gun%d.jpg', j))
imwrite(J,F);
end
Or:
D = '/Users/mmgp/Documents/MATLAB/FTruth53/FTruth53Gnoise5/'
S = dir(fullfile(D,'*.jpg'));
for k = 1: numel(S)
%construct file names
F_in = fullfile(D,S(k).name);
F_out = fullfile(D,'WithNoise','data1',sprintf('gun%d.jpg', k))
%read the image
I = imread(F_in);
%apply the noise
J = imnoise(I,'salt & pepper',noise(1));
%save image in the right folder
imwrite(J,F_out);
end
3 Comments
Rik
on 28 May 2020
Sometimes Matlab doesn't check if a folder exists before trying to write to it. That could be the source of your issue. If the folder does exist I don't have any solutions, because that means it is an issue with MacOS or your file system.
if ~exist(fileparts(NoiseLocation),'dir')
mkdir(fileparts(NoiseLocation));
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!