Adding Noise To A Folder OF Images in A Loop

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

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

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

hi Rik it is not that simple for someone that lacks the understanding pal!
I read the documentation and it was still confusing me to the point of frustration, so I decided to ask for help!
My process is to make every possible attempt until I have to ask for help that is how I manage to get this far learning on my own!
It will take some time to get to that point of efficiency but for now I am forced to rely on you guys when the matlab docs confused me!
Forgive me in advance for my trival questions but when I get by those hurdles this is where my learning process takes place!
As for the example you provided, I figured out some of it yesterday but it was not efficient!
%%%%%%%
This is my code:
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(ImgLoadDrive,'WithNoise','data1',sprintf('gun%d.jpg',j));
imwrite(ApplyNoise,NoiseLocation);
end
My OS is MAC and I am affected by this error
Error using imwrite (line 541)
Unable to open file "/Users/mmgp/Documents/MATLAB/FTruth53Gnoise5/WithNoise/data1/gun1.jpg" for writing. You might
not have write permission.
Error in GunFtruth53Gnoise5 (line 189)
imwrite(ApplyNoise,NoiseLocation);
Can you help me resolve this please?
I have been searching all of yesterday after adjusting my code but was not able to get pass this error!
and thank once more Rik for responding to my trival questions!!
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
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)

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 27 May 2020

Commented:

on 28 May 2020

Community Treasure Hunt

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

Start Hunting!