Here is a sample program in JavaScript that takes an image as input and applies salt and pepper noise using probability pa and pb respectively.
index.tsx549 chars26 linesThis function takes an imgData object (which has the width, height and data properties of the image), as well as the probabilities pa and pb for salt and pepper noise respectively.
For each pixel in the image, a random number is generated between 0 and 1. If this number is less than pa, then the pixel is set to white (255) to simulate salt noise. If the number is less than amount (which is the sum of pa and pb), but greater than pa, then the pixel is set to black (0) to simulate pepper noise. If the number is greater than amount, then the pixel is left unchanged.
Finally, the function returns the modified imgData object.
gistlibby LogSnag