# Adding Noise To A Folder OF Images in A Loop

21 views (last 30 days)
Matpar on 27 May 2020
Commented: Matpar on 28 May 2020
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:
1. the sprintf s%glow%d.jpg should output glow1.jpg?
2. some how the common g is being taken for %g function and output low1.jpg??!!!!
3. required some understanding why this outputs 1 noise file rather than 53 different noise images?
D = '/Users/mmgp/Documents/MATLAB/FTruth53/FTruth53Gnoise5/'
S = dir(fullfile(D,'*.jpg'));
for k = 1: numel(S)
F = fullfile(D,S(k).name);
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 CommentsShowHide 1 older comment
Matpar on 27 May 2020
not sure what you are speaking of Rik,
I remembered the break point but that is not going to help me move forward if I am lacking the understanding to figure it out and this is how I am trying to gain the understanding! if my methods for trying to understand is wrong by all means please guide me!!
I am willing and determine to learn!

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);
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))
%apply the noise
J = imnoise(I,'salt & pepper',noise(1));
%save image in the right folder
imwrite(J,F_out);
end
Matpar on 28 May 2020
See Rik , This is how I learn, I decided to create the address rather than creating the path from the imwrite fucntion and it worked. I did not figure out the wirte permissions aspect but I manage to get by with another process!
thank you so much RIK
here is my code,
NoiseImStore = '/Users/mmgp/Documents/MATLAB/FTruth53/WithNoise'
for k = 1: numel(Storage)
imshow(Input);
Storage(k).data = Input;
end
for j = 1:length (Storage)
ImageData = Storage(j).data;
%apply noise
ApplyNoise = imnoise(ImageData,'gaussian',noise(1));
%save images in tghe right folder
NoiseLocation = fullfile(NoiseImStore,sprintf('gun%d.jpg',j));
imwrite(ApplyNoise,NoiseLocation);
end