MATLAB Answers

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

27 ビュー (過去 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.

  0 件のコメント





Community Treasure Hunt

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

Start Hunting!

Translated by