Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Adding Noise to an Image in C#

4/21/2009

When it comes to image processing, more often than not, you're trying to reduce the amount of noise within an image. However there are times where you actually want to add noise to an image... The number of instances where this occurs is small, but they are out there. For instance let's say you want to make an image look like it's from an old movie. You convert the image to black and white, add a bit of noise, use a Gaussian blur, and you have a old, grainy looking image. So how do we add noise?

   1: /// <summary>
   2: /// adds noise to the image
   3: /// </summary>
   4: /// <param name="OriginalImage">Image to manipulate</param>
   5: /// <param name="Amount">Amount of noise to add</param>
   6: /// <returns>A bitmap image</returns>
   7: public static Bitmap AddNoise(Bitmap OriginalImage, int Amount)
   8: {
   9:     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
  10:     BitmapData NewData = Image.LockImage(NewBitmap);
  11:     BitmapData OldData = Image.LockImage(OriginalImage);
  12:     int NewPixelSize = Image.GetPixelSize(NewData);
  13:     int OldPixelSize = Image.GetPixelSize(OldData);
  14:     Random.Random TempRandom = new Random.Random();
  15:     for (int x = 0; x < NewBitmap.Width; ++x)
  16:     {
  17:         for (int y = 0; y < NewBitmap.Height; ++y)
  18:         {
  19:             Color CurrentPixel = Image.GetPixel(OldData, x, y, OldPixelSize);
  20:             int R = CurrentPixel.R + TempRandom.Next(-Amount, Amount + 1);
  21:             int G = CurrentPixel.G + TempRandom.Next(-Amount, Amount + 1);
  22:             int B = CurrentPixel.B + TempRandom.Next(-Amount, Amount + 1);
  23:             R = R > 255 ? 255 : R;
  24:             R = R < 0 ? 0 : R;
  25:             G = G > 255 ? 255 : G;
  26:             G = G < 0 ? 0 : G;
  27:             B = B > 255 ? 255 : B;
  28:             B = B < 0 ? 0 : B;
  29:             Color TempValue = Color.FromArgb(R, G, B);
  30:             Image.SetPixel(NewData, x, y, TempValue, NewPixelSize);
  31:         }
  32:     }
  33:     Image.UnlockImage(NewBitmap, NewData);
  34:     Image.UnlockImage(OriginalImage, OldData);
  35:     return NewBitmap;
  36: }

Note that the code uses a couple of functions from my utility library. Specifically it uses the Image.LockImage, UnlockImage, etc. which can be removed/replaced with the built in GetPixel/SetPixel functions (or just download my utility library and get a ton of extra code along with it). Anyway, the code above works like the other image processing functions that I've shown. You send in your bitmap and the intensity of the noise you want to show up (it's random but Amount indicates the max/min change allowed for each pixel). And all it does is randomly increases/decreases the brightness of a pixel. That's all there is to it really. And while it works best with black and white images, you can definitely use it on color images without much of an issue. Anyway, I hope this helps someone out. So give it a try, leave feedback, and happy coding.



Comments