Esempio n. 1
0
 public List<Vertex> MakeTestSet(int width, int height, int seed, int count)
 {
     List<Vertex> set = new List<Vertex>();
     Random.Random r = new Random.Random(seed);
     for (int i = 0; i < count; i++)
     {
         set.Add(new Vertex(r.NextFlat_Int(0, width), r.NextFlat_Int(0, height), 0));
     }
     return set;
 }
Esempio n. 2
0
 /// <summary>
 /// Constructor
 /// </summary>
 public OAuth()
 {
     Parameters = new System.Collections.Generic.List <System.Tuple <string, string> >();
     AddParameter("oauth_consumer_key", "");
     AddParameter("oauth_nonce", "");
     AddParameter("oauth_signature_method", "");
     AddParameter("oauth_timestamp", "");
     AddParameter("oauth_version", "1.0");
     RandomGenerator = new Random.Random();
 }
Esempio n. 3
0
        /// <summary>
        /// Make a test set of vertexes.
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="seed"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List <Vertex> MakeTestSet(int width, int height, int seed, int count)
        {
            List <Vertex> set = new List <Vertex>();

            Random.Random r = new Random.Random(seed);
            for (int i = 0; i < count; i++)
            {
                set.Add(new Vertex(r.NextFlat_Int(0, width), r.NextFlat_Int(0, height), 0));
            }
            return(set);
        }
Esempio n. 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="Seed">Seed for random generation</param>
 /// <param name="Width">Width of the image</param>
 /// <param name="Height">Height of the image</param>
 /// <param name="NumberOfPoints">Number of cells</param>
 public CellularMap(int Seed, int Width, int Height,int NumberOfPoints)
 {
     _Width = Width;
     _Height = Height;
     Random.Random Rand = new Random.Random(Seed);
     Distances = new float[Width, Height];
     ClosestPoint = new int[Width, Height];
     for (int x = 0; x < NumberOfPoints; ++x)
     {
         Point TempPoint = new Point();
         TempPoint.X = Rand.Next(0, Width);
         TempPoint.Y = Rand.Next(0, Height);
         Points.Add(TempPoint);
     }
     CalculateDistances();
 }
Esempio n. 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="Seed">Seed for random generation</param>
 /// <param name="Width">Width of the image</param>
 /// <param name="Height">Height of the image</param>
 /// <param name="NumberOfPoints">Number of cells</param>
 public CellularMap(int Seed, int Width, int Height, int NumberOfPoints)
 {
     _Width      = Width;
     _Height     = Height;
     MinDistance = float.MaxValue;
     MaxDistance = float.MinValue;
     Random.Random Rand = new Random.Random(Seed);
     Distances    = new float[Width, Height];
     ClosestPoint = new int[Width, Height];
     for (int x = 0; x < NumberOfPoints; ++x)
     {
         Point TempPoint = new Point();
         TempPoint.X = Rand.Next(0, Width);
         TempPoint.Y = Rand.Next(0, Height);
         Points.Add(TempPoint);
     }
     CalculateDistances();
 }
Esempio n. 6
0
        /// <summary>
        /// Form a test.
        /// </summary>
        /// <param name="idealNoNoise"></param>
        protected void formTest2D(double[] idealNoNoise, double[] idealNoNoiseVelocity)
        {
            Random.Random r = new Random.Random();
            // Add in wind, auto-pilot on our rocket always successfully adapts so
            // that the effect by the wind is guassian process noise.
            double[] withProcessNoise    = new double[idealNoNoise.Length];
            double[] withProcessNoiseVel = new double[idealNoNoise.Length];
            for (int i = 0; i < idealNoNoise.Length; i++)
            {
                // Add in 1 foot = 1 std. deviation of process noise.
                withProcessNoise[i]    = idealNoNoise[i] + r.NextGuass_Double(0, (double)numericUpDown5.Value);
                withProcessNoiseVel[i] = idealNoNoiseVelocity[i] + r.NextGuass_Double(0, (double)numericUpDown6.Value);
            }

            // Add in measurement noise, the GPS on the rocket measures less often
            // than our data but we can just skip things.
            // Assume a very good GPS, accurate to +/- 6 feet even at these speeds.
            // 2 foot = 1 std dev. is about +/- 6 feet
            double[] withAllNoise    = new double[idealNoNoise.Length];
            double[] withAllNoiseVel = new double[idealNoNoise.Length];
            for (int i = 0; i < idealNoNoise.Length; i++)
            {
                // Add in 1 foot = 1 std. deviation of process noise.
                withAllNoise[i]    = withProcessNoise[i] + r.NextGuass_Double(0, (double)numericUpDown6.Value);
                withAllNoiseVel[i] = withProcessNoiseVel[i] + r.NextGuass_Double(0, 0.2 * (double)numericUpDown6.Value);
            }

            Kalman2D k = new Kalman2D();

            k.Reset(
                (double)numericUpDown1.Value,
                (double)numericUpDown2.Value,
                (double)numericUpDown3.Value,
                (double)numericUpDown4.Value, 0);

            // Assume we get to see every other measurement we calculated, and use
            // the others as the points to compare for estimates.
            // Run the filter, note our time unit is 1.
            double[] kalman = new double[idealNoNoise.Length];
            double[] vel    = new double[idealNoNoise.Length];
            double[] kGain  = new double[idealNoNoise.Length];
            for (int i = 0; i < idealNoNoise.Length; i++)
            {
                if (i == 0)
                {
                    kalman[0] = 0;
                    vel[0]    = k.Velocity;
                    kGain[0]  = k.LastGain;
                }
                else
                {
                    kalman[i] = k.Update(withAllNoise[i], withAllNoiseVel[i], 1);
                    vel[i]    = k.Velocity;
                    kGain[i]  = k.LastGain;
                }
            }

            for (int i = 0; i < kalman.Length; i++)
            {
                kalman[i] = (kalman[i] < 100000000000000) ? kalman[i] : 100000000000000;
                kalman[i] = (kalman[i] > -100000000000000) ? kalman[i] : -100000000000000;
                vel[i]    = (vel[i] < 100000000000000) ? vel[i] : 100000000000000;
                vel[i]    = (vel[i] > -100000000000000) ? vel[i] : -100000000000000;
            }

            // Compute errors.
            double[] kalmanDelIdeal   = new double[idealNoNoise.Length];
            double[] measuredDelIdeal = new double[idealNoNoise.Length];
            for (int i = 0; i < idealNoNoise.Length; i += 2)
            {
                kalmanDelIdeal[i]   = kalman[i] - idealNoNoise[i];
                measuredDelIdeal[i] = withAllNoise[i] - idealNoNoise[i];
            }

            // Chart stuff.
            chart1.Series.Clear();
            graph(chart1, "ideal", idealNoNoise, Color.Black);
            graph(chart1, "measured", withAllNoise, Color.Blue);
            graph(chart1, "kalman", kalman, Color.Red);
            chart1.ChartAreas[0].RecalculateAxesScale();

            chart2.Series.Clear();
            graph(chart2, "Measurement", measuredDelIdeal, Color.Blue);
            graph(chart2, "Kalman", kalmanDelIdeal, Color.Black);
            chart2.ChartAreas[0].RecalculateAxesScale();

            chart3.Series.Clear();
            graph(chart3, "Velocity", vel, Color.Black);
            chart3.ChartAreas[0].RecalculateAxesScale();

            chart4.Series.Clear();
            graph(chart4, "Gain", kGain, Color.Black);
            chart4.ChartAreas[0].RecalculateAxesScale();
        }
Esempio n. 7
0
 /// <summary>
 /// adds noise to the image
 /// </summary>
 /// <param name="Image">Image to manipulate</param>
 /// <param name="Amount">Amount of noise to add</param>
 /// <returns>A bitmap image</returns>
 public static Bitmap AddNoise(Bitmap Image, int Amount)
 {
     System.Drawing.Bitmap TempBitmap = Image;
     System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
     System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
     NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
     NewGraphics.Dispose();
     Random.Random TempRandom = new Random.Random();
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             Color CurrentPixel = TempBitmap.GetPixel(x, y);
             int R = CurrentPixel.R + TempRandom.Next(-Amount, Amount + 1);
             int G = CurrentPixel.G + TempRandom.Next(-Amount, Amount + 1);
             int B = CurrentPixel.B + TempRandom.Next(-Amount, Amount + 1);
             R = R > 255 ? 255 : R;
             R = R < 0 ? 0 : R;
             G = G > 255 ? 255 : G;
             G = G < 0 ? 0 : G;
             B = B > 255 ? 255 : B;
             B = B < 0 ? 0 : B;
             Color TempValue = Color.FromArgb(R, G, B);
             NewBitmap.SetPixel(x, y, TempValue);
         }
     }
     return NewBitmap;
 }
Esempio n. 8
0
 /// <summary>
 /// Does dilation
 /// </summary>
 /// <param name="Image">Image to manipulate</param>
 /// <param name="Size">Size of the aperture</param>
 public static Bitmap Dilate(Bitmap Image, int Size)
 {
     System.Drawing.Bitmap TempBitmap = Image;
     System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
     System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
     NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
     NewGraphics.Dispose();
     Random.Random TempRandom = new Random.Random();
     int ApetureMin = -(Size / 2);
     int ApetureMax = (Size / 2);
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
             {
                 int TempX = x + x2;
                 if (TempX >= 0 && TempX < NewBitmap.Width)
                 {
                     for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
                     {
                         int TempY = y + y2;
                         if (TempY >= 0 && TempY < NewBitmap.Height)
                         {
                             Color TempColor = TempBitmap.GetPixel(TempX, TempY);
                             if (TempColor.R > RValue)
                                 RValue = TempColor.R;
                             if (TempColor.G > GValue)
                                 GValue = TempColor.G;
                             if (TempColor.B > BValue)
                                 BValue = TempColor.B;
                         }
                     }
                 }
             }
             Color TempPixel = Color.FromArgb(RValue, GValue, BValue);
             NewBitmap.SetPixel(x, y, TempPixel);
         }
     }
     return NewBitmap;
 }
Esempio n. 9
0
 /// <summary>
 /// Does smoothing using a SNN blur
 /// </summary>
 /// <param name="Image">Image to manipulate</param>
 /// <param name="Size">Size of the aperture</param>
 public static Bitmap SNNBlur(Bitmap Image, int Size)
 {
     System.Drawing.Bitmap TempBitmap = Image;
     System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
     System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
     NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
     NewGraphics.Dispose();
     Random.Random TempRandom = new Random.Random();
     int ApetureMinX = -(Size / 2);
     int ApetureMaxX = (Size / 2);
     int ApetureMinY = -(Size / 2);
     int ApetureMaxY = (Size / 2);
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             int RValue = 0;
             int GValue = 0;
             int BValue = 0;
             int NumPixels=0;
             for (int x2 = ApetureMinX; x2 < ApetureMaxX; ++x2)
             {
                 int TempX1 = x + x2;
                 int TempX2 = x - x2;
                 if (TempX1 >= 0 && TempX1 < NewBitmap.Width && TempX2 >= 0 && TempX2 < NewBitmap.Width)
                 {
                     for (int y2 = ApetureMinY; y2 < ApetureMaxY; ++y2)
                     {
                         int TempY1 = y + y2;
                         int TempY2 = y - y2;
                         if (TempY1 >= 0 && TempY1 < NewBitmap.Height && TempY2 >= 0 && TempY2 < NewBitmap.Height)
                         {
                             Color TempColor = TempBitmap.GetPixel(x, y);
                             Color TempColor2 = TempBitmap.GetPixel(TempX1, TempY1);
                             Color TempColor3 = TempBitmap.GetPixel(TempX2, TempY2);
                             if (Distance(TempColor.R, TempColor2.R, TempColor.G, TempColor2.G, TempColor.B, TempColor2.B) <
                                 Distance(TempColor.R, TempColor3.R, TempColor.G, TempColor3.G, TempColor.B, TempColor3.B))
                             {
                                 RValue += TempColor2.R;
                                 GValue += TempColor2.G;
                                 BValue += TempColor2.B;
                             }
                             else
                             {
                                 RValue += TempColor3.R;
                                 GValue += TempColor3.G;
                                 BValue += TempColor3.B;
                             }
                             ++NumPixels;
                         }
                     }
                 }
             }
             Color MeanPixel = Color.FromArgb(RValue / NumPixels,
                 GValue / NumPixels,
                 BValue / NumPixels);
             NewBitmap.SetPixel(x, y, MeanPixel);
         }
     }
     return NewBitmap;
 }
Esempio n. 10
0
 /// <summary>
 /// Does smoothing using a median filter
 /// </summary>
 /// <param name="Image">Image to manipulate</param>
 /// <param name="Size">Size of the aperture</param>
 public static Bitmap MedianFilter(Bitmap Image, int Size)
 {
     System.Drawing.Bitmap TempBitmap = Image;
     System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
     System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
     NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
     NewGraphics.Dispose();
     Random.Random TempRandom = new Random.Random();
     int ApetureMin = -(Size / 2);
     int ApetureMax = (Size / 2);
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             List<int> RValues = new List<int>();
             List<int> GValues = new List<int>();
             List<int> BValues = new List<int>();
             for (int x2 = ApetureMin; x2 < ApetureMax; ++x2)
             {
                 int TempX = x + x2;
                 if (TempX >= 0 && TempX < NewBitmap.Width)
                 {
                     for (int y2 = ApetureMin; y2 < ApetureMax; ++y2)
                     {
                         int TempY = y + y2;
                         if (TempY >= 0 && TempY < NewBitmap.Height)
                         {
                             Color TempColor = TempBitmap.GetPixel(TempX, TempY);
                             RValues.Add(TempColor.R);
                             GValues.Add(TempColor.G);
                             BValues.Add(TempColor.B);
                         }
                     }
                 }
             }
             RValues.Sort();
             GValues.Sort();
             BValues.Sort();
             Color MedianPixel = Color.FromArgb(RValues[RValues.Count / 2],
                 GValues[GValues.Count / 2],
                 BValues[BValues.Count / 2]);
             NewBitmap.SetPixel(x, y, MedianPixel);
         }
     }
     return NewBitmap;
 }
Esempio n. 11
0
        /// <summary>
        /// Does smoothing using a kuwahara blur
        /// </summary>
        /// <param name="Image">Image to manipulate</param>
        /// <param name="Size">Size of the aperture</param>
        public static Bitmap KuwaharaBlur(Bitmap Image, int Size)
        {
            System.Drawing.Bitmap TempBitmap = Image;
            System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
            System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
            NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
            NewGraphics.Dispose();
            Random.Random TempRandom = new Random.Random();
            int[] ApetureMinX = { -(Size / 2), 0, -(Size / 2), 0 };
            int[] ApetureMaxX = { 0, (Size / 2), 0, (Size / 2) };
            int[] ApetureMinY = { -(Size / 2), -(Size / 2), 0, 0 };
            int[] ApetureMaxY = { 0, 0, (Size / 2), (Size / 2) };
            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    int[] RValues = { 0, 0, 0, 0 };
                    int[] GValues = { 0, 0, 0, 0 };
                    int[] BValues = { 0, 0, 0, 0 };
                    int[] NumPixels = { 0, 0, 0, 0 };
                    int[] MaxRValue = { 0, 0, 0, 0 };
                    int[] MaxGValue = { 0, 0, 0, 0 };
                    int[] MaxBValue = { 0, 0, 0, 0 };
                    int[] MinRValue = { 255, 255, 255, 255 };
                    int[] MinGValue = { 255, 255, 255, 255 };
                    int[] MinBValue = { 255, 255, 255, 255 };
                    for (int i = 0; i < 4; ++i)
                    {
                        for (int x2 = ApetureMinX[i]; x2 < ApetureMaxX[i]; ++x2)
                        {
                            int TempX = x + x2;
                            if (TempX >= 0 && TempX < NewBitmap.Width)
                            {
                                for (int y2 = ApetureMinY[i]; y2 < ApetureMaxY[i]; ++y2)
                                {
                                    int TempY = y + y2;
                                    if (TempY >= 0 && TempY < NewBitmap.Height)
                                    {
                                        Color TempColor = TempBitmap.GetPixel(TempX, TempY);
                                        RValues[i] += TempColor.R;
                                        GValues[i] += TempColor.G;
                                        BValues[i] += TempColor.B;
                                        if (TempColor.R > MaxRValue[i])
                                        {
                                            MaxRValue[i] = TempColor.R;
                                        }
                                        else if (TempColor.R < MinRValue[i])
                                        {
                                            MinRValue[i] = TempColor.R;
                                        }

                                        if (TempColor.G > MaxGValue[i])
                                        {
                                            MaxGValue[i] = TempColor.G;
                                        }
                                        else if (TempColor.G < MinGValue[i])
                                        {
                                            MinGValue[i] = TempColor.G;
                                        }

                                        if (TempColor.B > MaxBValue[i])
                                        {
                                            MaxBValue[i] = TempColor.B;
                                        }
                                        else if (TempColor.B < MinBValue[i])
                                        {
                                            MinBValue[i] = TempColor.B;
                                        }
                                        ++NumPixels[i];
                                    }
                                }
                            }
                        }
                    }
                    int j = 0;
                    int MinDifference = 10000;
                    for (int i = 0; i < 4; ++i)
                    {
                        int CurrentDifference = (MaxRValue[i] - MinRValue[i]) + (MaxGValue[i] - MinGValue[i]) + (MaxBValue[i] - MinBValue[i]);
                        if (CurrentDifference < MinDifference && NumPixels[i] > 0)
                        {
                            j = i;
                            MinDifference = CurrentDifference;
                        }
                    }

                    Color MeanPixel = Color.FromArgb(RValues[j] / NumPixels[j],
                        GValues[j] / NumPixels[j],
                        BValues[j] / NumPixels[j]);
                    NewBitmap.SetPixel(x, y, MeanPixel);
                }
            }
            return NewBitmap;
        }
Esempio n. 12
0
        /// <summary>
        /// Causes a "Jitter" effect
        /// </summary>
        /// <param name="Image">Image to manipulate</param>
        /// <param name="MaxJitter">Maximum number of pixels the item can move</param>
        public static Bitmap Jitter(Bitmap Image, int MaxJitter)
        {
            System.Drawing.Bitmap TempBitmap = Image;
            System.Drawing.Bitmap NewBitmap = new System.Drawing.Bitmap(TempBitmap.Width, TempBitmap.Height);
            System.Drawing.Graphics NewGraphics = System.Drawing.Graphics.FromImage(NewBitmap);
            NewGraphics.DrawImage(TempBitmap, new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), new System.Drawing.Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), System.Drawing.GraphicsUnit.Pixel);
            NewGraphics.Dispose();
            Random.Random TempRandom = new Random.Random();
            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    int NewX=TempRandom.Next(-MaxJitter, MaxJitter);
                    int NewY = TempRandom.Next(-MaxJitter, MaxJitter);
                    NewX += x;
                    NewY += y;

                    if (NewX >= NewBitmap.Width)
                        NewX = NewBitmap.Width - 1;
                    else if (NewX < 0)
                        NewX = 0;

                    if (NewY >= NewBitmap.Height)
                        NewY = NewBitmap.Height - 1;
                    else if (NewY < 0)
                        NewY = 0;

                    NewBitmap.SetPixel(NewX, NewY, Image.GetPixel(x,y));
                }
            }
            return NewBitmap;
        }
        /// <summary>
        /// Causes a "Jitter" effect
        /// </summary>
        /// <param name="OriginalImage">Image to manipulate</param>
        /// <param name="MaxJitter">Maximum number of pixels the item can move</param>
        /// <param name="FileName">File to save to</param>
        /// <returns>A bitmap object</returns>
        public static Bitmap Jitter(this Bitmap OriginalImage, int MaxJitter = 5, string FileName = "")
        {
            if (OriginalImage == null)
                throw new ArgumentNullException("OriginalImage");
            ImageFormat FormatUsing = FileName.GetImageFormat();
            Bitmap NewBitmap = new Bitmap(OriginalImage, OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData = NewBitmap.LockImage();
            BitmapData OldData = OriginalImage.LockImage();
            int NewPixelSize = NewData.GetPixelSize();
            int OldPixelSize = OldData.GetPixelSize();
            Random.Random TempRandom = new Random.Random();
            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    int NewX = TempRandom.Next(-MaxJitter, MaxJitter);
                    int NewY = TempRandom.Next(-MaxJitter, MaxJitter);
                    NewX += x;
                    NewY += y;
                    NewX = NewX.Clamp(NewBitmap.Width - 1, 0);
                    NewY = NewY.Clamp(NewBitmap.Height - 1, 0);

                    NewData.SetPixel(x, y, OldData.GetPixel(NewX, NewY, OldPixelSize), NewPixelSize);
                }
            }
            NewBitmap.UnlockImage(NewData);
            OriginalImage.UnlockImage(OldData);
            if (!string.IsNullOrEmpty(FileName))
                NewBitmap.Save(FileName, FormatUsing);
            return NewBitmap;
        }
 /// <summary>
 /// adds noise to the image
 /// </summary>
 /// <param name="OriginalImage">Image to add noise to</param>
 /// <param name="FileName">Location to save the image to (optional)</param>
 /// <param name="Amount">Amount of noise to add (defaults to 10)</param>
 /// <returns>New image object with the noise added</returns>
 public static Bitmap AddNoise(this Bitmap OriginalImage, int Amount = 10, string FileName = "")
 {
     if (OriginalImage == null)
         throw new ArgumentNullException("OriginalImage");
     ImageFormat FormatUsing = FileName.GetImageFormat();
     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
     BitmapData NewData = NewBitmap.LockImage();
     BitmapData OldData = OriginalImage.LockImage();
     int NewPixelSize = NewData.GetPixelSize();
     int OldPixelSize = OldData.GetPixelSize();
     Random.Random TempRandom = new Random.Random();
     for (int x = 0; x < NewBitmap.Width; ++x)
     {
         for (int y = 0; y < NewBitmap.Height; ++y)
         {
             Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize);
             int R = CurrentPixel.R + TempRandom.Next(-Amount, Amount + 1);
             int G = CurrentPixel.G + TempRandom.Next(-Amount, Amount + 1);
             int B = CurrentPixel.B + TempRandom.Next(-Amount, Amount + 1);
             R = R > 255 ? 255 : R;
             R = R < 0 ? 0 : R;
             G = G > 255 ? 255 : G;
             G = G < 0 ? 0 : G;
             B = B > 255 ? 255 : B;
             B = B < 0 ? 0 : B;
             Color TempValue = Color.FromArgb(R, G, B);
             NewData.SetPixel(x, y, TempValue, NewPixelSize);
         }
     }
     NewBitmap.UnlockImage(NewData);
     OriginalImage.UnlockImage(OldData);
     if (!string.IsNullOrEmpty(FileName))
         NewBitmap.Save(FileName, FormatUsing);
     return NewBitmap;
 }