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#

Procedural Textures and Dilation

5/15/2008

It's amazing how I tend to get sucked into projects or think up new projects when I already have too many (including such projects as a web portal, multiplayer game, a proof for an algorithm to find a p-like cycle in polynomial time, etc.). However I find image editing to be the one that interests me the most.  Anyway, I've been looking back through a couple projects that I've used in the past, including FxGen and found an interesting function, Dilate. After a quick bit of work, I created a C# function to accomplish it:

   1: /// <summary>
   2: /// Does dilation
   3: /// </summary>
   4: /// <param name="OriginalImage">Image to manipulate</param>
   5: /// <param name="Size">Size of the aperture</param>
   6: public static Bitmap Dilate(Bitmap OriginalImage, int Size)
   7: {
   8:     Bitmap NewBitmap = new Bitmap(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:     int ApetureMin = -(Size / 2);
  14:     int ApetureMax = (Size / 2);
  15:     for (int x = 0; x < NewBitmap.Width; ++x)
  16:     {
  17:         for (int y = 0; y < NewBitmap.Height; ++y)
  18:         {
  19:             int RValue = 0;
  20:             int GValue = 0;
  21:             int BValue = 0;
  22:             for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
  23:             {
  24:                 int TempX = x + x2;
  25:                 if (TempX >= 0 && TempX < NewBitmap.Width)
  26:                 {
  27:                     for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
  28:                     {
  29:                         int TempY = y + y2;
  30:                         if (TempY >= 0 && TempY < NewBitmap.Height)
  31:                         {
  32:                             Color TempColor = Image.GetPixel(OldData, TempX, TempY, OldPixelSize);
  33:                             if (TempColor.R > RValue)
  34:                                 RValue = TempColor.R;
  35:                             if (TempColor.G > GValue)
  36:                                 GValue = TempColor.G;
  37:                             if (TempColor.B > BValue)
  38:                                 BValue = TempColor.B;
  39:                         }
  40:                     }
  41:                 }
  42:             }
  43:             Color TempPixel = Color.FromArgb(RValue, GValue, BValue);
  44:             Image.SetPixel(NewData, x, y, TempPixel, NewPixelSize);
  45:         }
  46:     }
  47:     Image.UnlockImage(NewBitmap, NewData);
  48:     Image.UnlockImage(OriginalImage, OldData);
  49:     return NewBitmap;
  50: }

The basic concept is we got through and check each pixel against its neighbors and whichever neighbor is the highest, we set the pixel to that value. In turn it looks like an image that's dilated. It's probably not the most useful function, but it can come in handy every once in a while. Anyway, try the code, leave feedback, and happy coding.



Comments