-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageSmoother.txt
32 lines (28 loc) · 1.16 KB
/
imageSmoother.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
661. Image Smoother
/*
An image smoother is a filter of the size 3 x 3 that can be applied to each cell
of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother).
If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
Given an m x n integer matrix img representing the grayscale of an image, return the image after applying the smoother on each cell of it.
*\
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
int m = img.size();
int n = img[0].size();
vector<vector<int>> result(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int total = 0, count = 0;
for (int x = max(0, i - 1); x < min(m, i + 2); x++) {
for (int y = max(0, j - 1); y < min(n, j + 2); y++) {
total += img[x][y];
count++;
}
}
result[i][j] = total / count;
}
}
return result;
}
};