-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
44 lines (37 loc) · 689 Bytes
/
Copy pathmain.go
File metadata and controls
44 lines (37 loc) · 689 Bytes
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
import "math"
func average(M [][]int, i, j int) int {
dirs := [][]int{
{1, 0},
{-1, 0},
{0, 1},
{0, -1},
{1, -1},
{1, 1},
{-1, 1},
{-1, -1},
{0, 0},
}
row, col := len(M), len(M[0])
sum, cnt := 0, 0
for _, dir := range dirs {
x, y := i+dir[0], j+dir[1]
if x >= 0 && x < row && y >= 0 && y < col {
sum += M[x][y]
cnt += 1
}
}
return int(math.Floor(float64(sum) / float64(cnt)))
}
func imageSmoother(M [][]int) [][]int {
row, col := len(M), len(M[0])
ans := make([][]int, row)
for i := 0; i < row; i++ {
ans[i] = make([]int, col)
}
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
ans[i][j] = average(M, i, j)
}
}
return ans
}