One simple way to add salt and pepper noise to an image in Matlab is by using the imnoise
function. Here is an example:
main.m132 chars4 lines
The 'salt & pepper'
keyword specifies the type of noise to be added, and 0.05
specifies the noise density, i.e., the fraction of pixels that will be affected by the noise. You can adjust 0.05
to add more or less noise.
Another way to add salt and pepper noise is to use a custom function, like this:
main.m252 chars10 lines
This function creates a binary mask of the same size as the original image, with random values between 0 and 1. Pixels with values below density/2
are set to black, and pixels with values above 1 - density/2
are set to white. This creates random black and white dots in the image, simulating salt and pepper noise. You can call this function with an image and a noise density between 0 and 1 to add noise:
main.m118 chars4 lines
gistlibby LogSnag