Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<int N, class T>
- void filter(T image, double matrix[N][N])
- {
- vector<vector<int>> new_img(image->width(), vector<int>(image->height()));
- for (int i = 0; i < image->width(); i++)
- {
- for (int j = 0; j < image->height(); j++)
- {
- double r = 0, g = 0, b = 0;
- for (int k = -N / 2; k <= N / 2; k++)
- {
- for (int l = -N / 2; l <= N / 2; l++)
- {
- int x = i + k, y = j + l;
- if (x < 0)
- x *= -1;
- if (x >= image->width())
- x = image->width() - (x - image->width());
- if (y < 0)
- y *= -1;
- if (y >= image->height())
- y = image->height() - (y - image->height());
- auto c = image->pixel(x, y);
- r += qRed(c) * matrix[k + N / 2][l + N / 2];
- g += qGreen(c) * matrix[k + N / 2][l + N / 2];
- b += qBlue(c) * matrix[k + N / 2][l + N / 2];
- }
- }
- new_img[i][j] = qRgb(r, g, b);
- }
- }
- for (int i = 0; i < image->width(); i++)
- {
- for (int j = 0; j < image->height(); j++)
- {
- image->setPixel(i, j, new_img[i][j]);
- }
- }
- }
- void MainWindow::button2_slot() {
- if (!image) return;
- system("echo \"start\"");
- const int N = 5;
- double matrix[N][N] =
- {
- {1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1},
- {1, 1, 1, 1, 1}
- };
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- {
- matrix[i][j] /= N * N;
- }
- }
- filter<N>(image, matrix);
- system("echo \"end\"");
- update_image();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement