Guest User

Untitled

a guest
Jul 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 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. public double[,] LinearConvolutionSpatial(double[,] image, double[,] mask)
  74. {
  75. int imageWidth = image.GetLength(0);
  76. int imageHeight = image.GetLength(1);
  77.  
  78. int maskWidth = mask.GetLength(0);
  79. int maskHeight = mask.GetLength(1);
  80.  
  81. double[,] conv = ConvolutionSpatial(image, mask);
  82.  
  83. return conv;
  84. }
  85.  
  86. private double[,] ConvolutionSpatial(double[,] paddedImage, double[,] mask)
  87. {
  88. int paddedImageWidth = paddedImage.GetLength(0);
  89. int paddedImageHeight = paddedImage.GetLength(1);
  90.  
  91. int maskWidth = mask.GetLength(0);
  92. int maskHeight = mask.GetLength(1);
  93.  
  94. int imageWidth = paddedImageWidth - maskWidth;
  95. int imageHeight = paddedImageHeight - maskHeight;
  96.  
  97. double[,] convolve = new double[imageWidth, imageHeight];
  98.  
  99. for (int y = 0; y < imageHeight; y++)
  100. {
  101. for (int x = 0; x < imageWidth; x++)
  102. {
  103. double sum = Sum(paddedImage, mask, x, y);
  104. int xxx = x;
  105. int yyy = y;
  106. convolve[xxx, yyy] = sum;
  107.  
  108. string str = string.Empty;
  109. }
  110. }
  111.  
  112. Rescale(convolve);
  113.  
  114. return convolve;
  115. }
  116.  
  117. double Sum(double[,] paddedImage1, double[,] mask1, int startX, int startY)
  118. {
  119. double sum = 0;
  120.  
  121. int maskWidth = mask1.GetLength(0);
  122. int maskHeight = mask1.GetLength(1);
  123.  
  124. for (int y = startY; y < (startY + maskHeight); y++)
  125. {
  126. for (int x = startX; x < (startX + maskWidth); x++)
  127. {
  128. double img = paddedImage1[x, y];
  129. double msk = mask1[maskWidth - x + startX - 1, maskHeight - y + startY - 1];
  130. sum = sum + (img * msk);
  131. }
  132. }
  133.  
  134. return sum;
  135. }
  136.  
  137. void Rescale(double[,] convolve)
  138. {
  139. int imageWidth = convolve.GetLength(0);
  140. int imageHeight = convolve.GetLength(1);
  141.  
  142. double minAmp = 0.0;
  143. double maxAmp = 0.0;
  144.  
  145. for (int j = 0; j < imageHeight; j++)
  146. {
  147. for (int i = 0; i < imageWidth; i++)
  148. {
  149. minAmp = Math.Min(minAmp, convolve[i, j]);
  150. maxAmp = Math.Max(maxAmp, convolve[i, j]);
  151. }
  152. }
  153.  
  154. double scale = 1 / (Math.Abs(minAmp) + maxAmp);
  155.  
  156. for (int j = 0; j < imageHeight; j++)
  157. {
  158. for (int i = 0; i < imageWidth; i++)
  159. {
  160. double d = (convolve[i, j] + Math.Abs(minAmp)) * scale;
  161. convolve[i, j] = d;
  162. }
  163. }
  164. }
  165. }
Add Comment
Please, Sign In to add comment