Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. 0 -1 0
  2. -1 5 -1
  3. 0 -1 0
  4.  
  5. public partial class FilterForm : Form
  6. {
  7. public FilterForm()
  8. {
  9. InitializeComponent();
  10.  
  11. Bitmap image = (Bitmap)Bitmap.FromFile("lena.jpg");
  12. InputPictureBox.Image = image;
  13.  
  14. double[,] dImage = ToDouble2d(image);
  15. double[,] dMask = { { 0,-1, 0, },
  16. { -1, 5, -1, },
  17. { 0,-1, 0, }, };
  18.  
  19. double[,]dConv = LinearConvolutionSpatial(dImage, dMask);
  20.  
  21. Bitmap conv = ToBitmap2d(dConv, PixelFormat.Format32bppArgb);
  22.  
  23. OutputPictureBox.Image = conv;
  24. }
  25.  
  26. public double[,] ToDouble2d(Bitmap input)
  27. {
  28. int width = input.Width;
  29. int height = input.Height;
  30.  
  31. double[,] array2d = new double[width, height];
  32.  
  33. for (int y = 0; y < height; y++)
  34. {
  35. for (int x = 0; x < width; x++)
  36. {
  37. Color cl = input.GetPixel(x, y);
  38.  
  39. double gray = ((cl.R * 0.3) + (cl.G * 0.59) + (cl.B * 0.11));
  40.  
  41. array2d[x, y] = gray / 255.0;
  42. }
  43. }
  44.  
  45. return array2d;
  46. }
  47.  
  48. public Bitmap ToBitmap2d(double[,] image, PixelFormat pixelFormat)
  49. {
  50. int Width = image.GetLength(0);
  51. int Height = image.GetLength(1);
  52.  
  53. Bitmap bmp = new Bitmap(Width, Height, pixelFormat);
  54.  
  55. for (int y = 0; y < Height; y++)
  56. {
  57. for (int x = 0; x < Width; x++)
  58. {
  59. int i = (int)(image[x, y] * 255.0);
  60.  
  61. if (i > 255) i = 255;
  62. if (i < 0) i = 0;
  63.  
  64. Color clr = Color.FromArgb(i, i, i);
  65.  
  66. bmp.SetPixel(x, y, clr);
  67. }
  68. }
  69.  
  70. return bmp;
  71. }
  72.  
  73. private double[,] LinearConvolutionSpatial(double[,] paddedImage, double[,] mask)
  74. {
  75. int paddedImageWidth = paddedImage.GetLength(0);
  76. int paddedImageHeight = paddedImage.GetLength(1);
  77.  
  78. int maskWidth = mask.GetLength(0);
  79. int maskHeight = mask.GetLength(1);
  80.  
  81. int imageWidth = paddedImageWidth - maskWidth;
  82. int imageHeight = paddedImageHeight - maskHeight;
  83.  
  84. double[,] convolve = new double[imageWidth, imageHeight];
  85.  
  86. for (int y = 0; y < imageHeight; y++)
  87. {
  88. for (int x = 0; x < imageWidth; x++)
  89. {
  90. double sum = Sum(paddedImage, mask, x, y);
  91. int xxx = x;
  92. int yyy = y;
  93. convolve[xxx, yyy] = sum;
  94.  
  95. string str = string.Empty;
  96. }
  97. }
  98.  
  99. Rescale(convolve);
  100.  
  101. return convolve;
  102. }
  103.  
  104. double Sum(double[,] paddedImage1, double[,] mask1, int startX, int startY)
  105. {
  106. double sum = 0;
  107.  
  108. int maskWidth = mask1.GetLength(0);
  109. int maskHeight = mask1.GetLength(1);
  110.  
  111. for (int y = startY; y < (startY + maskHeight); y++)
  112. {
  113. for (int x = startX; x < (startX + maskWidth); x++)
  114. {
  115. double img = paddedImage1[x, y];
  116. double msk = mask1[maskWidth - x + startX - 1, maskHeight - y + startY - 1];
  117. sum = sum + (img * msk);
  118. }
  119. }
  120.  
  121. return sum;
  122. }
  123.  
  124. void Rescale(double[,] convolve)
  125. {
  126. int imageWidth = convolve.GetLength(0);
  127. int imageHeight = convolve.GetLength(1);
  128.  
  129. double minAmp = 0.0;
  130. double maxAmp = 0.0;
  131.  
  132. for (int j = 0; j < imageHeight; j++)
  133. {
  134. for (int i = 0; i < imageWidth; i++)
  135. {
  136. minAmp = Math.Min(minAmp, convolve[i, j]);
  137. maxAmp = Math.Max(maxAmp, convolve[i, j]);
  138. }
  139. }
  140.  
  141. double scale = 1 / (Math.Abs(minAmp) + maxAmp);
  142.  
  143. for (int j = 0; j < imageHeight; j++)
  144. {
  145. for (int i = 0; i < imageWidth; i++)
  146. {
  147. double d = (convolve[i, j] + Math.Abs(minAmp)) * scale;
  148. convolve[i, j] = d;
  149. }
  150. }
  151. }
  152. }
Add Comment
Please, Sign In to add comment