/// <summary> /// Generates a number of faults, returning an image /// </summary> /// <param name="Width">Width of the resulting image</param> /// <param name="Height">Height of the resulting image</param> /// <param name="NumberFaults">Number of faults</param> /// <param name="Seed">Random seed</param> /// <returns>An image from the resulting faults</returns> public static Bitmap Generate(int Width, int Height, int NumberFaults, int Seed) { float[,] Heights = new float[Width, Height]; float IncreaseVal = 0.1f; System.Random Generator = new System.Random(Seed); for (int x = 0; x < NumberFaults; ++x) { IncreaseVal = GenerateFault(Width, Height, NumberFaults, Heights, IncreaseVal, Generator); } Bitmap ReturnValue = new Bitmap(Width, Height); BitmapData ImageData = ReturnValue.LockImage(); int ImagePixelSize = ImageData.GetPixelSize(); for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { float Value = Heights[x, y]; Value = (Value * 0.5f) + 0.5f; Value *= 255; int RGBValue = ((int)Value).Clamp(255, 0); ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize); } } ReturnValue.UnlockImage(ImageData); return(ReturnValue); }
public static Bitmap Generate(int Width, int Height, int NumberOfPoints, int Seed) { float[,] DistanceBuffer = new float[Width, Height]; float MinimumDistance = float.MaxValue; float MaxDistance = float.MinValue; CellularMap Map = new CellularMap(Seed, Width, Height, NumberOfPoints); MaxDistance = Map.MaxDistance; MinimumDistance = Map.MinDistance; DistanceBuffer = Map.Distances; Bitmap ReturnValue = new Bitmap(Width, Height); BitmapData ImageData = ReturnValue.LockImage(); int ImagePixelSize = ImageData.GetPixelSize(); for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { float Value = GetHeight(x, y, DistanceBuffer, MinimumDistance, MaxDistance); Value *= 255; int RGBValue = ((int)Value).Clamp(255, 0); ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize); } } ReturnValue.UnlockImage(ImageData); return(ReturnValue); }
/// <summary> /// Loads an image /// </summary> /// <param name="ImageUsing">Image to load</param> public virtual void LoadImage(Bitmap ImageUsing) { if (ImageUsing == null) { throw new ArgumentNullException("ImageUsing"); } BitmapData OldData = ImageUsing.LockImage(); int PixelSize = OldData.GetPixelSize(); Width = ImageUsing.Width; Height = ImageUsing.Height; R.Clear(); G.Clear(); B.Clear(); for (int x = 0; x < ImageUsing.Width; ++x) { for (int y = 0; y < ImageUsing.Height; ++y) { Color TempColor = OldData.GetPixel(x, y, PixelSize); ++R[(int)TempColor.R]; ++G[(int)TempColor.G]; ++B[(int)TempColor.B]; } } ImageUsing.UnlockImage(OldData); }
public static Bitmap AddNoise2(Bitmap OriginalImage, int Amount) { Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height); BitmapData NewData = NewBitmap.LockImage(); //Image.LockImage(NewBitmap); BitmapData OldData = OriginalImage.LockImage(); //Image.LockImage(OriginalImage); int NewPixelSize = NewData.GetPixelSize(); //Image.GetPixelSize(NewData); int OldPixelSize = OldData.GetPixelSize(); //Image.GetPixelSize(OldData); Utilities.Random.Random TempRandom = new Utilities.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); //Image.GetPixel(OldData, x, y, OldPixelSize); int noise = (int)AWGN() * 255; int R = CurrentPixel.R + noise; //(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1); int G = CurrentPixel.G + noise; //(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1); int B = CurrentPixel.B + noise; //(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//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);//Image.SetPixel(NewData, x, y, TempValue, NewPixelSize); } } NewBitmap.UnlockImage(NewData); //Image.UnlockImage(NewBitmap, NewData); OriginalImage.UnlockImage(OldData); //Image.UnlockImage(OriginalImage, OldData); return(NewBitmap); }
public static Bitmap AdjustGamma(Bitmap OriginalImage, float Value) { Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height); BitmapData NewData = NewBitmap.LockImage(); //Image.LockImage(NewBitmap); BitmapData OldData = OriginalImage.LockImage(); //Image.LockImage(OriginalImage); int NewPixelSize = NewData.GetPixelSize(); //Image.GetPixelSize(NewData); int OldPixelSize = OldData.GetPixelSize(); //Image.GetPixelSize(OldData); int[] RedRamp = new int[256]; int[] GreenRamp = new int[256]; int[] BlueRamp = new int[256]; for (int x = 0; x < 256; ++x) { RedRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0); GreenRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0); BlueRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0); } for (int x = 0; x < NewBitmap.Width; ++x) { for (int y = 0; y < NewBitmap.Height; ++y) { Color Pixel = OldData.GetPixel(x, y, OldPixelSize);//Image.GetPixel(OldData, x, y, OldPixelSize); int Red = RedRamp[Pixel.R]; int Green = GreenRamp[Pixel.G]; int Blue = BlueRamp[Pixel.B]; NewData.SetPixel(x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);//Image.SetPixel(NewData, x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize); } } NewBitmap.UnlockImage(NewData); //Image.UnlockImage(NewBitmap, NewData); OriginalImage.UnlockImage(OldData); //Image.UnlockImage(OriginalImage, OldData); return(NewBitmap); }
private void SetupImage() { BitmapData ImageData = _Image.LockImage(); int ImagePixelSize = ImageData.GetPixelSize(); int Width = _Image.Width; int Height = _Image.Height; Parallel.For(0, _NumberOfPoints, i => { int Red = 0; int Green = 0; int Blue = 0; int Counter = 0; for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { if (Map.ClosestPoint[x, y] == i) { Color Pixel = ImageData.GetPixel(x, y, ImagePixelSize); Red += Pixel.R; Green += Pixel.G; Blue += Pixel.B; ++Counter; } } } int Counter2 = 0; for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { if (Map.ClosestPoint[x, y] == i) { ImageData.SetPixel(x, y, Color.FromArgb(Red / Counter, Green / Counter, Blue / Counter), ImagePixelSize); ++Counter2; if (Counter2 == Counter) { break; } } } if (Counter2 == Counter) { break; } } }); _Image.UnlockImage(ImageData); }
/// <summary> /// Creates the bump map /// </summary> public virtual Bitmap Create(Bitmap ImageUsing) { if (ImageUsing == null) { throw new ArgumentNullException("ImageUsing"); } CreateFilter(); using (Bitmap TempImageX = FilterX.Create(ImageUsing)) { using (Bitmap TempImageY = FilterY.Create(ImageUsing)) { Bitmap ReturnImage = new Bitmap(TempImageX.Width, TempImageX.Height); Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0); BitmapData TempImageXData = TempImageX.LockImage(); BitmapData TempImageYData = TempImageY.LockImage(); BitmapData ReturnImageData = ReturnImage.LockImage(); int TempImageXPixelSize = TempImageXData.GetPixelSize(); int TempImageYPixelSize = TempImageYData.GetPixelSize(); int ReturnImagePixelSize = ReturnImageData.GetPixelSize(); for (int y = 0; y < TempImageX.Height; ++y) { for (int x = 0; x < TempImageX.Width; ++x) { Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize); Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize); TempVector.X = (double)(TempPixelX.R) / 255.0; TempVector.Y = (double)(TempPixelY.R) / 255.0; TempVector.Z = 1.0; TempVector.Normalize(); TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0; TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0; TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0; ReturnImageData.SetPixel(x, y, Color.FromArgb((int)TempVector.X, (int)TempVector.Y, (int)TempVector.Z), ReturnImagePixelSize); } } TempImageX.UnlockImage(TempImageXData); TempImageY.UnlockImage(TempImageYData); ReturnImage.UnlockImage(ReturnImageData); return(ReturnImage); } } }
/// <summary> /// Creates the bump map /// </summary> public virtual Bitmap Create(Bitmap ImageUsing) { ImageUsing.ThrowIfNull("ImageUsing"); CreateFilter(); using (Bitmap TempImageX = FilterX.Create(ImageUsing)) { using (Bitmap TempImageY = FilterY.Create(ImageUsing)) { Bitmap ReturnImage = new Bitmap(TempImageX.Width, TempImageX.Height); BitmapData TempImageXData = TempImageX.LockImage(); BitmapData TempImageYData = TempImageY.LockImage(); BitmapData ReturnImageData = ReturnImage.LockImage(); int TempImageXPixelSize = TempImageXData.GetPixelSize(); int TempImageYPixelSize = TempImageYData.GetPixelSize(); int ReturnImagePixelSize = ReturnImageData.GetPixelSize(); int Width = TempImageX.Width; int Height = TempImageX.Height; Parallel.For(0, Height, y => { Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0); for (int x = 0; x < Width; ++x) { Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize); Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize); TempVector.X = (double)(TempPixelX.R) / 255.0; TempVector.Y = (double)(TempPixelY.R) / 255.0; TempVector.Z = 1.0; TempVector.Normalize(); TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0; TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0; TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0; ReturnImageData.SetPixel(x, y, Color.FromArgb((int)TempVector.X, (int)TempVector.Y, (int)TempVector.Z), ReturnImagePixelSize); } }); TempImageX.UnlockImage(TempImageXData); TempImageY.UnlockImage(TempImageYData); ReturnImage.UnlockImage(ReturnImageData); return(ReturnImage); } } }
/// <summary> /// Generates perlin noise /// </summary> /// <param name="Width">Width of the resulting image</param> /// <param name="Height">Height of the resulting image</param> /// <param name="MaxRGBValue">MaxRGBValue</param> /// <param name="MinRGBValue">MinRGBValue</param> /// <param name="Frequency">Frequency</param> /// <param name="Amplitude">Amplitude</param> /// <param name="Persistance">Persistance</param> /// <param name="Octaves">Octaves</param> /// <param name="Seed">Random seed</param> /// <returns>An image containing perlin noise</returns> public static Bitmap Generate(int Width, int Height, int MaxRGBValue, int MinRGBValue, float Frequency, float Amplitude, float Persistance, int Octaves, int Seed) { Bitmap ReturnValue = new Bitmap(Width, Height); BitmapData ImageData = ReturnValue.LockImage(); int ImagePixelSize = ImageData.GetPixelSize(); float[,] Noise = GenerateNoise(Seed, Width, Height); for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { float Value = GetValue(x, y, Width, Height, Frequency, Amplitude, Persistance, Octaves, Noise); Value = (Value * 0.5f) + 0.5f; Value *= 255; int RGBValue = ((int)Value).Clamp(MaxRGBValue, MinRGBValue); ImageData.SetPixel(x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize); } } ReturnValue.UnlockImage(ImageData); return(ReturnValue); }
/// <summary> /// Loads an image /// </summary> /// <param name="ImageUsing">Image to load</param> public virtual void LoadImage(Bitmap ImageUsing) { Contract.Requires <ArgumentNullException>(ImageUsing != null, "ImageUsing"); BitmapData OldData = ImageUsing.LockImage(); int PixelSize = OldData.GetPixelSize(); Width = ImageUsing.Width; Height = ImageUsing.Height; R.Clear(); G.Clear(); B.Clear(); for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { Color TempColor = OldData.GetPixel(x, y, PixelSize); ++R[(int)TempColor.R]; ++G[(int)TempColor.G]; ++B[(int)TempColor.B]; } } ImageUsing.UnlockImage(OldData); }
/// <summary> /// Loads an image /// </summary> /// <param name="ImageUsing">Image to load</param> public virtual void LoadImage(Bitmap ImageUsing) { ImageUsing.ThrowIfNull("ImageUsing"); BitmapData OldData = ImageUsing.LockImage(); int PixelSize = OldData.GetPixelSize(); Width = ImageUsing.Width; Height = ImageUsing.Height; R.Clear(); G.Clear(); B.Clear(); for (int x = 0; x < Width; ++x) { for (int y = 0; y < Height; ++y) { Color TempColor = OldData.GetPixel(x, y, PixelSize); ++R[(int)TempColor.R]; ++G[(int)TempColor.G]; ++B[(int)TempColor.B]; } } ImageUsing.UnlockImage(OldData); }
/// <summary> /// Converts an image to ASCII art /// </summary> /// <param name="Input">The image you wish to convert</param> /// <returns>A string containing the art</returns> public static string ConvertToASCII(Bitmap Input) { if (Input == null) { throw new ArgumentNullException("Input"); } bool ShowLine = true; using (Bitmap TempImage = Input.BlackAndWhite()) { BitmapData OldData = TempImage.LockImage(); int OldPixelSize = OldData.GetPixelSize(); StringBuilder Builder = new StringBuilder(); for (int x = 0; x < TempImage.Height; ++x) { for (int y = 0; y < TempImage.Width; ++y) { if (ShowLine) { Color CurrentPixel = OldData.GetPixel(y, x, OldPixelSize); Builder.Append(_ASCIICharacters[((CurrentPixel.R * _ASCIICharacters.Length) / 255)]); } } if (ShowLine) { Builder.Append(System.Environment.NewLine); ShowLine = false; } else { ShowLine = true; } } TempImage.UnlockImage(OldData); return(Builder.ToString()); } }
/// <summary> /// Runs the motion detection algorithm /// </summary> /// <param name="NewImage">The "new" frame</param> /// <param name="OldImage">The "old" frame</param> /// <param name="Threshold">The threshold used to detect changes in the image</param> /// <param name="DetectionColor">Color to display changes in the images as</param> /// <returns>A bitmap indicating where changes between frames have occurred overlayed on top of the new image.</returns> public static Bitmap Process(Bitmap NewImage, Bitmap OldImage, int Threshold, Color DetectionColor) { NewImage.ThrowIfNull("NewImage"); OldImage.ThrowIfNull("OldImage"); DetectionColor.ThrowIfNull("DetectionColor"); using (Bitmap NewImage1 = NewImage.BlackAndWhite()) { using (Bitmap OldImage1 = OldImage.BlackAndWhite()) { using (Bitmap NewImage2 = NewImage1.SNNBlur(5)) { using (Bitmap OldImage2 = OldImage1.SNNBlur(5)) { using (Bitmap OutputImage = new Bitmap(NewImage2, NewImage2.Width, NewImage2.Height)) { using (Bitmap Overlay = new Bitmap(NewImage, NewImage.Width, NewImage.Height)) { BitmapData NewImage2Data = NewImage2.LockImage(); int NewImage2PixelSize = NewImage2Data.GetPixelSize(); BitmapData OldImage2Data = OldImage2.LockImage(); int OldImage2PixelSize = OldImage2Data.GetPixelSize(); BitmapData OverlayData = Overlay.LockImage(); int OverlayPixelSize = OverlayData.GetPixelSize(); int Width = OutputImage.Width; int Height = OutputImage.Height; Parallel.For(0, Width, x => { for (int y = 0; y < Height; ++y) { Color NewPixel = NewImage2Data.GetPixel(x, y, NewImage2PixelSize); Color OldPixel = OldImage2Data.GetPixel(x, y, OldImage2PixelSize); if (System.Math.Pow((double)(NewPixel.R - OldPixel.R), 2.0) > Threshold) { OverlayData.SetPixel(x, y, Color.FromArgb(100, 0, 100), OverlayPixelSize); } else { OverlayData.SetPixel(x, y, Color.FromArgb(200, 0, 200), OverlayPixelSize); } } }); Overlay.UnlockImage(OverlayData); NewImage2.UnlockImage(NewImage2Data); OldImage2.UnlockImage(OldImage2Data); using (Bitmap Overlay2 = Overlay.EdgeDetection(25, DetectionColor)) { BitmapData Overlay2Data = Overlay2.LockImage(); int Overlay2PixelSize = Overlay2Data.GetPixelSize(); Width = OutputImage.Width; Height = OutputImage.Height; Parallel.For(0, Width, x => { for (int y = 0; y < Height; ++y) { Color Pixel1 = Overlay2Data.GetPixel(x, y, Overlay2PixelSize); if (Pixel1.R != DetectionColor.R || Pixel1.G != DetectionColor.G || Pixel1.B != DetectionColor.B) { Overlay2Data.SetPixel(x, y, Color.FromArgb(200, 0, 200), Overlay2PixelSize); } } }); Overlay2.UnlockImage(Overlay2Data); return(OutputImage.Watermark(Overlay2, 1.0f, 0, 0, Color.FromArgb(200, 0, 200))); } } } } } } } }
public virtual Bitmap ApplyFilter(Bitmap Input) { Contract.Requires <ArgumentNullException>(Input != null, "Input"); Bitmap NewBitmap = new Bitmap(Input.Width, Input.Height); BitmapData NewData = NewBitmap.LockImage(); BitmapData OldData = Input.LockImage(); int NewPixelSize = NewData.GetPixelSize(); int OldPixelSize = OldData.GetPixelSize(); int Width2 = Input.Width; int Height2 = Input.Height; Parallel.For(0, Width2, x => { for (int y = 0; y < Height2; ++y) { int RValue = 0; int GValue = 0; int BValue = 0; int Weight = 0; int XCurrent = -Width / 2; for (int x2 = 0; x2 < Width; ++x2) { if (XCurrent + x < Width2 && XCurrent + x >= 0) { int YCurrent = -Height / 2; for (int y2 = 0; y2 < Height; ++y2) { if (YCurrent + y < Height2 && YCurrent + y >= 0) { Color Pixel = OldData.GetPixel(XCurrent + x, YCurrent + y, OldPixelSize); RValue += MyFilter[x2, y2] * Pixel.R; GValue += MyFilter[x2, y2] * Pixel.G; BValue += MyFilter[x2, y2] * Pixel.B; Weight += MyFilter[x2, y2]; } ++YCurrent; } } ++XCurrent; } Color MeanPixel = OldData.GetPixel(x, y, OldPixelSize); if (Weight == 0) { Weight = 1; } if (Weight > 0) { if (Absolute) { RValue = System.Math.Abs(RValue); GValue = System.Math.Abs(GValue); BValue = System.Math.Abs(BValue); } RValue = (RValue / Weight) + Offset; RValue = RValue.Clamp(255, 0); GValue = (GValue / Weight) + Offset; GValue = GValue.Clamp(255, 0); BValue = (BValue / Weight) + Offset; BValue = BValue.Clamp(255, 0); MeanPixel = Color.FromArgb(RValue, GValue, BValue); } NewData.SetPixel(x, y, MeanPixel, NewPixelSize); } }); NewBitmap.UnlockImage(NewData); Input.UnlockImage(OldData); return(NewBitmap); }