public GrayscaleImageAdapter(BitmapSource rgbSource) { if (rgbSource == null) { throw new ArgumentNullException("source"); } int width = rgbSource.PixelWidth; int height = rgbSource.PixelHeight; GrayscalePixel[,] result = new GrayscalePixel[height, width]; byte[] byteArray = new byte[height * width * 4]; byte[] grayscaleArray = new byte[height * width]; int stride = width * 4; rgbSource.CopyPixels(byteArray, stride, 0); int k = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { byte blue = byteArray[(i * width + j) * 4 + 0]; byte green = byteArray[(i * width + j) * 4 + 1]; byte red = byteArray[(i * width + j) * 4 + 2]; GrayscalePixel grayscalePixel = new GrayscalePixel(Convert.ToByte(0.2125 * red + 0.7154 * green + 0.0721 * blue)); result[i, j] = grayscalePixel; grayscaleArray[k++] = grayscalePixel.Level; } } this.image = new GrayscaleImage(result); this.source = BitmapSource.Create(image.Width, image.Height, 96, 96, PixelFormats.Gray8, null, grayscaleArray, image.Width); }
public GrayscaleImage(GrayscalePixel[,] pixels) { if (pixels == null) { throw new ArgumentNullException("pixels"); } this.pixels = new IPixel[pixels.GetLength(0), pixels.GetLength(1)]; Array.Copy(pixels, this.pixels, pixels.GetLength(0) * pixels.GetLength(1)); }
public override IImage ApplyEqualizedHistogram(IHistogram histogram) { List<int> equalizedValues = new List<int>(histogram.EqualizedValues); GrayscalePixel[,] pixels = new GrayscalePixel[this.Height, this.Width]; for (int row = 0; row < histogram.Image.Height; row++) { for (int column = 0; column < histogram.Image.Width; column++) { int level = ((GrayscalePixel)this.pixels[row, column]).Level; pixels[row, column] = new GrayscalePixel((byte)equalizedValues[level]); } } return new GrayscaleImage(pixels); }
public static GrayscaleImage ToGrayscaleImage(this RgbImage rgbImage) { if (rgbImage == null) { throw new ArgumentNullException("rgbImage", "RgbImage constructor can't be invoked with null"); } GrayscalePixel[,] grayscalePixels = new GrayscalePixel[rgbImage.Height, rgbImage.Width]; for (int i = 0; i < rgbImage.Height; i++) { for (int j = 0; j < rgbImage.Width; j++) { RgbPixel rgbPixel = (RgbPixel)rgbImage.Pixels[i, j]; grayscalePixels[i, j] = rgbPixel.ToGrayscalePixel(); } } return new GrayscaleImage(grayscalePixels); }