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/Jitter to an Image in C#

1/16/2009

This is one of those items that I have no idea if there is a decent use for it or not. I simply did it for the sake of doing it. Anyway, I wanted to do some image manipulation through code that would take an image that I picked and make it look like a Monet... I failed miserably on my first attempt but I did come up with an interesting algorithm that adds some "jitter" to the image:

   1: /// <summary>
   2: /// Causes a "Jitter" effect
   3: /// </summary>
   4: /// <param name="OriginalImage">Image to manipulate</param>
   5: /// <param name="MaxJitter">Maximum number of pixels the item can move</param>
   6: public static Bitmap Jitter(Bitmap OriginalImage, int MaxJitter)
   7: {
   8:     Bitmap NewBitmap = new Bitmap(OriginalImage, OriginalImage.Width, OriginalImage.Height);
   9:     BitmapData NewData = Image.LockImage(NewBitmap);
  10:     BitmapData OldData = Image.LockImage(OriginalImage);
  11:     int NewPixelSize = Image.GetPixelSize(NewData);
  12:     int OldPixelSize = Image.GetPixelSize(OldData);
  13:     Random.Random TempRandom = new Random.Random();
  14:     for (int x = 0; x < NewBitmap.Width; ++x)
  15:     {
  16:         for (int y = 0; y < NewBitmap.Height; ++y)
  17:         {
  18:             int NewX = TempRandom.Next(-MaxJitter, MaxJitter);
  19:             int NewY = TempRandom.Next(-MaxJitter, MaxJitter);
  20:             NewX += x;
  21:             NewY += y;
  22:             NewX = MathHelper.Clamp(NewX, NewBitmap.Width - 1, 0);
  23:             NewY = MathHelper.Clamp(NewY, NewBitmap.Height - 1, 0);
  24:  
  25:             Image.SetPixel(NewData, x, y, Image.GetPixel(OldData, NewX, NewY, OldPixelSize), NewPixelSize);
  26:         }
  27:     }
  28:     Image.UnlockImage(NewBitmap, NewData);
  29:     Image.UnlockImage(OriginalImage, OldData);
  30:     return NewBitmap;
  31: }

The function takes in two items, the image and the maximum amount that you want pixels to move. Note that this also uses functions from my utility library for locking/unlocking an image to speed things up but you can simply remove them and replace the SetPixel function with the built in function. Also it uses a function from my MathHelper class to simply clamp the values between a max/min. Anyway, a smaller number in MaxJitter causes smaller movements while a larger number causes larger movements. Doing this multiple times with a value of 1 causes an almost oil painting look if you smooth it out a bit. Once again I have no idea if you will find this useful or not but I thought it was interesting. Anyway, try it out, leave feedback, and happy coding.



Comments