Adding Noise To A Folder OF Images in A Loop

10 views (last 30 days)
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?
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
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?
Matpar
Matpar on 27 May 2020
Edited: 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!

Sign in to comment.

Accepted Answer

Rik
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
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
Matpar
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'
ImgLoadDrive = '/Users/mmgp/Documents/MATLAB/FTruth53Gnoise5'
Storage =dir(fullfile(ImgLoadDrive,'*.jpg'));
for k = 1: numel(Storage)
Files = fullfile(ImgLoadDrive,Storage(k).name);
Input = imread(Files);
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!