Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10dadcc

Browse files
committed
Add: Image Smoother
1 parent c60ec29 commit 10dadcc

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
11
package image_smoother
2+
3+
func imageSmoother(M [][]int) [][]int {
4+
r, c := len(M), len(M[0])
5+
ans := make([][]int, r)
6+
for i := 0; i < r; i++ {
7+
ans[i] = make([]int, c)
8+
for j := 0; j < c; j++ {
9+
sum, count := 0, 0
10+
for m := i - 1; m <= i+1; m++ {
11+
for n := j - 1; n <= j+1; n++ {
12+
if m >= 0 && n >= 0 && m < r && n < c {
13+
sum += M[m][n]
14+
count++
15+
}
16+
}
17+
}
18+
ans[i][j] = sum / count
19+
}
20+
}
21+
return ans
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
11
package image_smoother
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input [][]int
10+
expected [][]int
11+
}
12+
13+
func TestImageSmoother(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: [][]int{
17+
{1, 1, 1},
18+
{1, 0, 1},
19+
{1, 1, 1},
20+
},
21+
expected: [][]int{
22+
{0, 0, 0},
23+
{0, 0, 0},
24+
{0, 0, 0},
25+
},
26+
},
27+
{
28+
input: [][]int{
29+
{1, 2, 3},
30+
{4, 5, 6},
31+
{7, 8, 9},
32+
},
33+
expected: [][]int{
34+
{3, 3, 4},
35+
{4, 5, 5},
36+
{6, 6, 7},
37+
},
38+
},
39+
}
40+
for _, tc := range tests {
41+
output := imageSmoother(tc.input)
42+
if !reflect.DeepEqual(output, tc.expected) {
43+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
44+
}
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /