-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image Smoother.cpp
46 lines (46 loc) · 1.37 KB
/
Image Smoother.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
int m = img.size(), n = img[0].size();
auto res = img;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
int deno = 1;
if(i > 0) {
if(j > 0) {
res[i][j] += img[i-1][j-1];
deno++;
}
res[i][j] += img[i-1][j];
deno++;
if(j < n-1) {
res[i][j] += img[i-1][j+1];
deno++;
}
}
if(i < m-1) {
if(j > 0) {
res[i][j] += img[i+1][j-1];
deno++;
}
res[i][j] += img[i+1][j];
deno++;
if(j < n-1) {
res[i][j] += img[i+1][j+1];
deno++;
}
}
if(j > 0) {
res[i][j] += img[i][j-1];
deno++;
}
if(j < n-1) {
res[i][j] += img[i][j+1];
deno++;
}
res[i][j] /= deno;
}
}
return res;
}
};