Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0 -1 0
- -1 5 -1
- 0 -1 0
- public partial class FilterForm : Form
- {
- public FilterForm()
- {
- InitializeComponent();
- Bitmap image = (Bitmap)Bitmap.FromFile("lena.jpg");
- InputPictureBox.Image = image;
- double[,] dImage = ToDouble2d(image);
- double[,] dMask = { { 0,-1, 0, },
- { -1, 5, -1, },
- { 0,-1, 0, }, };
- double[,]dConv = LinearConvolutionSpatial(dImage, dMask);
- Bitmap conv = ToBitmap2d(dConv, PixelFormat.Format32bppArgb);
- OutputPictureBox.Image = conv;
- }
- public double[,] ToDouble2d(Bitmap input)
- {
- int width = input.Width;
- int height = input.Height;
- double[,] array2d = new double[width, height];
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- Color cl = input.GetPixel(x, y);
- double gray = ((cl.R * 0.3) + (cl.G * 0.59) + (cl.B * 0.11));
- array2d[x, y] = gray / 255.0;
- }
- }
- return array2d;
- }
- public Bitmap ToBitmap2d(double[,] image, PixelFormat pixelFormat)
- {
- int Width = image.GetLength(0);
- int Height = image.GetLength(1);
- Bitmap bmp = new Bitmap(Width, Height, pixelFormat);
- for (int y = 0; y < Height; y++)
- {
- for (int x = 0; x < Width; x++)
- {
- int i = (int)(image[x, y] * 255.0);
- if (i > 255) i = 255;
- if (i < 0) i = 0;
- Color clr = Color.FromArgb(i, i, i);
- bmp.SetPixel(x, y, clr);
- }
- }
- return bmp;
- }
- public double[,] LinearConvolutionSpatial(double[,] image, double[,] mask)
- {
- int imageWidth = image.GetLength(0);
- int imageHeight = image.GetLength(1);
- int maskWidth = mask.GetLength(0);
- int maskHeight = mask.GetLength(1);
- double[,] conv = ConvolutionSpatial(image, mask);
- return conv;
- }
- private double[,] ConvolutionSpatial(double[,] paddedImage, double[,] mask)
- {
- int paddedImageWidth = paddedImage.GetLength(0);
- int paddedImageHeight = paddedImage.GetLength(1);
- int maskWidth = mask.GetLength(0);
- int maskHeight = mask.GetLength(1);
- int imageWidth = paddedImageWidth - maskWidth;
- int imageHeight = paddedImageHeight - maskHeight;
- double[,] convolve = new double[imageWidth, imageHeight];
- for (int y = 0; y < imageHeight; y++)
- {
- for (int x = 0; x < imageWidth; x++)
- {
- double sum = Sum(paddedImage, mask, x, y);
- int xxx = x;
- int yyy = y;
- convolve[xxx, yyy] = sum;
- string str = string.Empty;
- }
- }
- Rescale(convolve);
- return convolve;
- }
- double Sum(double[,] paddedImage1, double[,] mask1, int startX, int startY)
- {
- double sum = 0;
- int maskWidth = mask1.GetLength(0);
- int maskHeight = mask1.GetLength(1);
- for (int y = startY; y < (startY + maskHeight); y++)
- {
- for (int x = startX; x < (startX + maskWidth); x++)
- {
- double img = paddedImage1[x, y];
- double msk = mask1[maskWidth - x + startX - 1, maskHeight - y + startY - 1];
- sum = sum + (img * msk);
- }
- }
- return sum;
- }
- void Rescale(double[,] convolve)
- {
- int imageWidth = convolve.GetLength(0);
- int imageHeight = convolve.GetLength(1);
- double minAmp = 0.0;
- double maxAmp = 0.0;
- for (int j = 0; j < imageHeight; j++)
- {
- for (int i = 0; i < imageWidth; i++)
- {
- minAmp = Math.Min(minAmp, convolve[i, j]);
- maxAmp = Math.Max(maxAmp, convolve[i, j]);
- }
- }
- double scale = 1 / (Math.Abs(minAmp) + maxAmp);
- for (int j = 0; j < imageHeight; j++)
- {
- for (int i = 0; i < imageWidth; i++)
- {
- double d = (convolve[i, j] + Math.Abs(minAmp)) * scale;
- convolve[i, j] = d;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment