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#

Perlin Noise

3/19/2008

I've always found the simple fact that math defines damn near everything to be truly amazing. As of late with games like Spore on the horizon, I've been rather interested in procedural content. In fact the engine and game I'm creating utilize procedural content quite a bit, so expect there to be a lot of posts on the subject but today I'm going to talk about Perlin Noise.

Perlin Noise was developed by Ken Perlin (which he ended up getting an Academy Award for... Odd but true) and that's really all you need to know about the history. The algorithm creates pseudo random noise. We can use this for height maps, clouds, textures, an "interesting" background for your website... Although I'm sure that if you're here all you really want is the code. So here you go:

   1: /*
   2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
   3: 
   4: Permission is hereby granted, free of charge, to any person obtaining a copy
   5: of this software and associated documentation files (the "Software"), to deal
   6: in the Software without restriction, including without limitation the rights
   7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8: copies of the Software, and to permit persons to whom the Software is
   9: furnished to do so, subject to the following conditions:
  10: 
  11: The above copyright notice and this permission notice shall be included in
  12: all copies or substantial portions of the Software.
  13: 
  14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20: THE SOFTWARE.*/
  21:  
  22: #region Usings
  23: using System;
  24: using System.Collections.Generic;
  25: using System.Linq;
  26: using System.Text;
  27: using System.Drawing;
  28: using System.Drawing.Imaging;
  29: #endregion
  30:  
  31: namespace Utilities.Media.Image
  32: {
  33:     /// <summary>
  34:     /// Perlin noise helper class
  35:     /// </summary>
  36:     public static class PerlinNoise
  37:     {
  38:         #region Functions
  39:  
  40:         /// <summary>
  41:         /// Generates perlin noise
  42:         /// </summary>
  43:         /// <param name="Width">Width of the resulting image</param>
  44:         /// <param name="Height">Height of the resulting image</param>
  45:         /// <param name="MaxRGBValue">MaxRGBValue</param>
  46:         /// <param name="MinRGBValue">MinRGBValue</param>
  47:         /// <param name="Frequency">Frequency</param>
  48:         /// <param name="Amplitude">Amplitude</param>
  49:         /// <param name="Persistance">Persistance</param>
  50:         /// <param name="Octaves">Octaves</param>
  51:         /// <param name="Seed">Random seed</param>
  52:         /// <returns>An image containing perlin noise</returns>
  53:         public static Bitmap Generate(int Width,int Height,int MaxRGBValue,int MinRGBValue,
  54:             float Frequency,float Amplitude,float Persistance,int Octaves,int Seed)
  55:         {
  56:             Bitmap ReturnValue = new Bitmap(Width, Height);
  57:             BitmapData ImageData = Image.LockImage(ReturnValue);
  58:             int ImagePixelSize = Image.GetPixelSize(ImageData);
  59:             float[,] Noise=GenerateNoise(Seed,Width,Height);
  60:             for (int x = 0; x < Width; ++x)
  61:             {
  62:                 for (int y = 0; y < Height; ++y)
  63:                 {
  64:                     float Value = GetValue(x, y, Width, Height, Frequency, Amplitude, Persistance, Octaves, Noise);
  65:                     Value = (Value * 0.5f) + 0.5f;
  66:                     Value *= 255;
  67:                     int RGBValue=Math.MathHelper.Clamp((int)Value, MaxRGBValue, MinRGBValue);
  68:                     Image.SetPixel(ImageData, x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
  69:                 }
  70:             }
  71:             Image.UnlockImage(ReturnValue, ImageData);
  72:             return ReturnValue;
  73:         }
  74:  
  75:         private static float GetValue(int X, int Y, int Width,int Height,float Frequency, float Amplitude,
  76:             float Persistance, int Octaves,float[,]Noise)
  77:         {
  78:             float FinalValue = 0.0f;
  79:             for (int i = 0; i < Octaves; ++i)
  80:             {
  81:                 FinalValue += GetSmoothNoise(X * Frequency, Y * Frequency,Width,Height,Noise) * Amplitude;
  82:                 Frequency *= 2.0f;
  83:                 Amplitude *= Persistance;
  84:             }
  85:             if (FinalValue < -1.0f)
  86:             {
  87:                 FinalValue = -1.0f;
  88:             }
  89:             else if (FinalValue > 1.0f)
  90:             {
  91:                 FinalValue = 1.0f;
  92:             }
  93:             return FinalValue;
  94:         }
  95:  
  96:         private static float GetSmoothNoise(float X, float Y,int Width,int Height,float[,]Noise)
  97:         {
  98:             float FractionX = X - (int)X;
  99:             float FractionY = Y - (int)Y;
 100:             int X1 = ((int)X + Width) % Width;
 101:             int Y1 = ((int)Y + Height) % Height;
 102:             int X2 = ((int)X + Width - 1) % Width;
 103:             int Y2 = ((int)Y + Height - 1) % Height;
 104:  
 105:             float FinalValue = 0.0f;
 106:             FinalValue += FractionX * FractionY * Noise[X1, Y1];
 107:             FinalValue += FractionX * (1 - FractionY) * Noise[X1, Y2];
 108:             FinalValue += (1 - FractionX) * FractionY * Noise[X2, Y1];
 109:             FinalValue += (1 - FractionX) * (1 - FractionY) * Noise[X2, Y2];
 110:  
 111:             return FinalValue;
 112:         }
 113:  
 114:         private static float[,] GenerateNoise(int Seed,int Width,int Height)
 115:         {
 116:             float[,] Noise = new float[Width,Height];
 117:             System.Random RandomGenerator = new System.Random(Seed);
 118:             for (int x = 0; x < Width; ++x)
 119:             {
 120:                 for (int y = 0; y < Height; ++y)
 121:                 {
 122:                     Noise[x, y] = ((float)(RandomGenerator.NextDouble()) - 0.5f) * 2.0f;
 123:                 }
 124:             }
 125:             return Noise;
 126:         }
 127:  
 128:         #endregion
 129:     }
 130: }

When you call the Generate function, play around with the variables a bit, but I've noticed that using a Frequency of 0.0625, an Amplitude of 1.0, a Persistance of 0.5 and 16 Octaves gives good results. Also note that the code uses a bit of code from my utility library that locks/unlocks the bitmap image, the code for which can be found here. Anyway, try out the code, leave comments, and happy coding.



Comments