Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4.  
  5. namespace WindowsFormsApplication1
  6. {
  7. class MatrixFilter : Filters
  8. {
  9. protected float[,] kernel = null;
  10. protected MatrixFilter() { }
  11. public MatrixFilter(float[,] kernel)
  12. {
  13. this.kernel = kernel;
  14. }
  15. protected override Color calculateNewPixelColor(Bitmap sourceImage, int x, int y)
  16. {
  17. int radiusX = kernel.GetLength(0) / 2;
  18. int radiusY = kernel.GetLength(1) / 2;
  19. float resultR = 0;
  20. float resultG = 0;
  21. float resultB = 0;
  22. for (int l = - radiusY; l <= radiusY; l++)
  23. {
  24. for (int k = - radiusX; k <= radiusX; k++)
  25. {
  26. int idX = Clamp(x + k, 0, sourceImage.Width - 1);
  27. int idY = Clamp(y + l, 0, sourceImage.Height - 1);
  28. Color neighborColor = sourceImage.GetPixel(idX, idY);
  29. resultR += neighborColor.R * kernel[k + radiusX, l + radiusY];
  30. resultG += neighborColor.G * kernel[k + radiusX, l + radiusY];
  31. resultB += neighborColor.B * kernel[k + radiusX, l + radiusY];
  32. }
  33. }
  34. return Color.FromArgb(
  35. Clamp((int)resultR, 0, 255),
  36. Clamp((int)resultG, 0, 255),
  37. Clamp((int)resultB, 0, 255)
  38. );
  39. }
  40.  
  41. protected override void CalcInten(Bitmap sourceImage)
  42. {
  43. //throw new NotImplementedException();
  44. }
  45. protected override void CalcAvg(Bitmap sourceImage)
  46. {
  47. //throw new NotImplementedException();
  48. }
  49.  
  50. protected override Bitmap Open(Bitmap sourceImage, BackgroundWorker worker)
  51. {
  52. return sourceImage;
  53. }
  54. protected override Bitmap Tophat(Bitmap sourceImage, Bitmap resultImage, BackgroundWorker worker)
  55. {
  56. return sourceImage;
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement