private void Go_Click(object sender, RoutedEventArgs e) { try { System.Windows.Media.Imaging.BitmapImage imagesource = imageBox.Source as System.Windows.Media.Imaging.BitmapImage; if (imageBox.Source == null) { System.Windows.MessageBox.Show("Add your image to the Image 1 box!"); return; } System.Drawing.Bitmap image = BmpImage2Bmp(imagesource); System.Windows.Media.Imaging.BitmapImage final = new System.Windows.Media.Imaging.BitmapImage(); if (!AForge.Imaging.Image.IsGrayscale(image)) { AForge.Imaging.Filters.ExtractChannel Grayer = new AForge.Imaging.Filters.ExtractChannel(0); image = Grayer.Apply(image); } if (Threshold.IsChecked == true) { imageBox.Source = null; AForge.Imaging.Filters.Threshold threshold = new AForge.Imaging.Filters.Threshold((int)slider.Value); threshold.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (GaussianFilter.IsChecked == true) { AForge.Imaging.Filters.GaussianBlur Gauss = new AForge.Imaging.Filters.GaussianBlur(); Gauss.Sigma = GaussSigma_Slide.Value; Gauss.Size = (int)GaussSize_Slide.Value; AForge.Imaging.UnmanagedImage unmanagedImage = AForge.Imaging.UnmanagedImage.FromManagedImage(image); AForge.Imaging.UnmanagedImage Dst = unmanagedImage.Clone(); Gauss.Apply(unmanagedImage, Dst); final = Bmp2BmpImage(Dst.ToManagedImage()); } else if (HiPass.IsChecked == true) { AForge.Imaging.Filters.Sharpen filter = new AForge.Imaging.Filters.Sharpen(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Erode.IsChecked == true) { AForge.Imaging.Filters.Erosion filter = new AForge.Imaging.Filters.Erosion(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Invert.IsChecked == true) { AForge.Imaging.Filters.Invert filter = new AForge.Imaging.Filters.Invert(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (EdgeDetector.IsChecked == true) { AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Median.IsChecked == true) { AForge.Imaging.Filters.Median filter = new AForge.Imaging.Filters.Median(); filter.Size = (int)GaussSize_Slide.Value; filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (More.IsChecked == true) { if (Dilate.IsSelected) { AForge.Imaging.Filters.Dilatation filter = new AForge.Imaging.Filters.Dilatation(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } } imageBox.Source = final; TransformImage = image; boxWidth = imageBox.RenderSize.Width; boxHeight = imageBox.RenderSize.Height; } catch (Exception exc) { System.Windows.MessageBox.Show(exc.ToString()); } }
public void Dilate() { // apply filter AForge.Imaging.Filters.Dilatation dilation = new AForge.Imaging.Filters.Dilatation(); Texture = dilation.Apply(this.Texture); }
private void generateHeightmap_Click(object sender, EventArgs e) { Heightmap heightmap = Heightmap.Instance; heightmap.SetDimensions((int)mapWidth.Value, (int)mapHeight.Value); int seed; if (this.enableSeed.Checked) { seed = (int)seedVal.Value; } else { seed = (new Random()).Next(); seedVal.Value = (Decimal)seed; } heightmap.DiffusionLimitedAgregation(seed, (float)(this.occupation.Value / this.occupation.Maximum), (int)this.frequency.Value); // apply gaussian pass, multiple kernel sizes sum and normalize if (gaussianEnable.Checked) { List <Bitmap> dlaCopies = new List <Bitmap>(); Bitmap dilatedBitmap = (Bitmap)heightmap.Texture.Clone(); AForge.Imaging.Filters.GaussianBlur gaussian = new AForge.Imaging.Filters.GaussianBlur(4.0, 21); AForge.Imaging.Filters.Dilatation dilatationFilter = new AForge.Imaging.Filters.Dilatation(); GaussianBlur gaus = new GaussianBlur(2); for (int i = 0; i < (int)copyCount.Value; i++) { // apply filter, first increasinly dilate the picture dilatedBitmap = dilatationFilter.Apply(dilatedBitmap); // then apply gaussian blur with an increasing radius size Bitmap blurredBitmap = (Bitmap)dilatedBitmap.Clone(); if (radialBlur.Checked) { for (int j = 0; j < (i * 3) + 1; j++) { blurredBitmap = gaussian.Apply(blurredBitmap); } } else { gaus.Radius = (int)Math.Pow(2, i + 1); blurredBitmap = gaus.ProcessImage(blurredBitmap); } dlaCopies.Add(blurredBitmap); } // add noise to final texture collections if (noiseEnabled.Checked) { dlaCopies.Add(NoiseTexture((int)perlinOctaves.Value, (double)perlinPersistence.Value, (double)perlinFreq.Value, (double)perlinAmplitude.Value)); } // normalize all blurred heightmaps Bitmap bmp = (Bitmap)heightmap.Texture.Clone(); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); byte[] finalData = new byte[bmpData.Height * bmpData.Stride]; // sum average between all blurred pictures for (int i = 0; i < dlaCopies.Count; i++) { // AForge.Imaging.Filters addFilter = new AForge.Imaging.Filters.Add(blurredBitmap); Bitmap current = dlaCopies[i]; BitmapData currentData = current.LockBits(new Rectangle(0, 0, current.Width, current.Height), ImageLockMode.ReadOnly, current.PixelFormat); byte[] currentRawData = new byte[currentData.Height * currentData.Stride]; Marshal.Copy(currentData.Scan0, currentRawData, 0, currentData.Height * currentData.Stride); // lamba operation sums averages between blurred pictures Parallel.For(0, currentRawData.Length, index => { finalData[index] = (byte)(currentRawData[index] * 1.0f / (float)dlaCopies.Count + finalData[index]); }); } // save final raw pixel data Marshal.Copy(finalData, 0, bmpData.Scan0, finalData.Length); bmp.UnlockBits(bmpData); // store final values on heightmap heightmap.MapValues = finalData; heightmap.Texture = bmp; } // show height map, set up new values this.heightmapPicture.Height = heightmap.Height; this.heightmapPicture.Width = heightmap.Width; this.heightmapPicture.Image = heightmap.Texture; }