How to apply Gaussian filter with for loop without imnoise?

How can I apply Matlab code to do what the imnoise function does but manually using for loop?

 Accepted Answer

Replicating the gaussian noise functionality of imnoise() does not require any loops. If someone insists that you need to use loops, make sure to let them know they're wrong.
inpict = im2double(imread('cameraman.tif'));
s0 = size(inpict);
% default parameters used by imnoise()
gaumean = 0;
gauvar = 0.01;
outpict = inpict + gaumean + sqrt(gauvar)*randn(s0);
imshow(outpict)
If you want to know how imnoise() does something, open imnoise() and look at $MLROOT/toolbox/images/images/+images/+internal/algimnoise.m

7 Comments

Thank you very much and sorry for the inconvenience, but can you explain to me why we chose the values 0 and 0.01 , and are inpict , gaumean and gauvar just variable names or do they signify something?, and I apologize once again but I want to understand
how i get there ($MLROOT/toolbox/images/images/+images/algimnoise.m)?
The code I posted is an excerpt from my own implementation of a replacement for imnoise(). Gaumean and gauvar are just the variable names I used internally for the mean and variance parameters for the gaussian noise mode.
From the imnoise() docs:
J = imnoise(I,'gaussian',M,V) adds Gaussian white noise of mean M and
variance V to the image I. When unspecified, M and V default to 0 and
0.01 respectively.
I chose those values specifically because they were the defaults used by imnoise().
The path $MLROOT/toolbox/images/images/+images/algimnoise.m is a location on your hard drive, where $MLROOT is the installation directory for MATLAB. That said, I see I made a mistake in remembering the path... This should work:
open([matlabroot '/toolbox/images/images/+images/+internal/algimnoise.m'])
Does this work on any function I want to know how it works?
Chances are what you want is alsread on the path, so you can open/edit the m-file simply by saying edit without all that other stuff about the path. For example to edit imnoise, say
>> edit imnoise
That should work for most built-in functions. However many of them are just wrappers to a binary mex DLL file.
Some functions are very simple (e.g. immse()) and can just be opened and viewed in whole. A lot of functions will call lower-level files, some of which may be viewable m-code. Some may call precompiled binaries which aren't viewable. If you want to find out what something does, start by opening the function file itself and see what it calls. The rest is just digging through the directories in the toolbox.
For a lot of things, you can tell what something does by reading the docs and observing the behavior, although there are plenty of cases where the documentation is incomplete or the wording is vague or misleading in my opinion.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 4 Sep 2021

Commented:

DGM
on 5 Sep 2021

Community Treasure Hunt

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

Start Hunting!