Write a function to add "randn" Gaussian noise to an specific percentage of images inside imageData store in matlab

3 次查看(过去 30 天)

回答(1 个)

Nitin Kapgate
Nitin Kapgate 2020-8-6
编辑:Nitin Kapgate 2020-8-6
Hi Mohamed,
I can understand that you want to add noise to a percentage of images in an ImageDataStore which are chosen at random.
Here is a demo code which will help you to solve your problem.
% Load images from a folder as ImageDataStore objects
imds = imageDatastore('C:\Users\XYZ\MyProject'); % Assuming your images are in “MyProject” folder
percentImages = 0.2; % Fraction of Images you want to add Noise
numTotalImg = numel(imds.Files) % Total number of Images in the Folder
numNoisyImg = round(percentImages * numTotalImg) % Number of images you want to add the Noise
selectedNoisyImages = randperm(numTotalImg,numNoisyImg); % Generate random indices of images to which noise is to be added
for k = 1:numNoisyImg
fileName = imds.Files{selectedNoisyImages(k)} % read the filename of the image with random index, to add noise
I = imread(fileName); % Read the image using imread function
% Insert Code to add noise to the image I here
% You may use "randn" or "imnoise" function to add the noise of your choice
% If needed, write back noisy image to a different folder.
end

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by