Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. template<int N, class T>
  2. void filter(T image, double matrix[N][N])
  3. {
  4. vector<vector<int>> new_img(image->width(), vector<int>(image->height()));
  5. for (int i = 0; i < image->width(); i++)
  6. {
  7. for (int j = 0; j < image->height(); j++)
  8. {
  9. double r = 0, g = 0, b = 0;
  10. for (int k = -N / 2; k <= N / 2; k++)
  11. {
  12. for (int l = -N / 2; l <= N / 2; l++)
  13. {
  14. int x = i + k, y = j + l;
  15. if (x < 0)
  16. x *= -1;
  17. if (x >= image->width())
  18. x = image->width() - (x - image->width());
  19. if (y < 0)
  20. y *= -1;
  21. if (y >= image->height())
  22. y = image->height() - (y - image->height());
  23. auto c = image->pixel(x, y);
  24. r += qRed(c) * matrix[k + N / 2][l + N / 2];
  25. g += qGreen(c) * matrix[k + N / 2][l + N / 2];
  26. b += qBlue(c) * matrix[k + N / 2][l + N / 2];
  27. }
  28. }
  29. new_img[i][j] = qRgb(r, g, b);
  30. }
  31. }
  32. for (int i = 0; i < image->width(); i++)
  33. {
  34. for (int j = 0; j < image->height(); j++)
  35. {
  36. image->setPixel(i, j, new_img[i][j]);
  37. }
  38. }
  39. }
  40.  
  41. void MainWindow::button2_slot() {
  42. if (!image) return;
  43. system("echo \"start\"");
  44. const int N = 5;
  45. double matrix[N][N] =
  46. {
  47. {1, 1, 1, 1, 1},
  48. {1, 1, 1, 1, 1},
  49. {1, 1, 1, 1, 1},
  50. {1, 1, 1, 1, 1},
  51. {1, 1, 1, 1, 1}
  52. };
  53. for (int i = 0; i < N; i++)
  54. {
  55. for (int j = 0; j < N; j++)
  56. {
  57. matrix[i][j] /= N * N;
  58. }
  59. }
  60. filter<N>(image, matrix);
  61. system("echo \"end\"");
  62. update_image();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement