-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageSmoother.java
More file actions
144 lines (129 loc) · 4.12 KB
/
ImageSmoother.java
File metadata and controls
144 lines (129 loc) · 4.12 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package array;
/**
* @author :DengSiYuan
* @date :2019/9/25 11:33
* @desc : 661.图片平滑器
* 【题目】
* 包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。
* 【示例】
* 输入:
* [[1,1,1],
* [1,0,1],
* [1,1,1]]
* 输出:
* [[0, 0, 0],
* [0, 0, 0],
* [0, 0, 0]]
* 解释:
* 对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
* 对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
* 对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
* 【注意】
* 给定矩阵中的整数范围为 [0, 255]。
* 矩阵的长和宽的范围均为 [1, 150]。
*/
public class ImageSmoother {
int row = 0;
int col = 0;
public int[][] imageSmoother(int[][] M) {
if (M == null || M.length < 1 || M[0] == null || M[0].length < 1) {
return null;
}
row = M.length;
col = M[row - 1].length;
int ans[][] = new int[row][col];
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
ans[i][j] = calcul(M,i,j);
}
}
return ans;
}
// 上、下、左、右,上左,上右,下左,下右
int dirR[] = {-1,1,0,0,-1,-1,1,1};
int dirC[] = {0,0,-1,1,-1,1,-1,1};
private int calcul(int arr[][],int i,int j) {
int count = 1;
int sum = arr[i][j];
for (int k = 0;k < dirR.length;k++) {
int nextR = i + dirR[k];
int nextC = j + dirC[k];
if (nextR >= 0 && nextR < row && nextC >= 0 && nextC < col) {
count++;
sum += arr[nextR][nextC];
}
}
return sum / count;
}
public int[][] imageSmoother1(int[][] M) {
int height = M.length;
int width = M[0].length;
int[][] result = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
result[i][j] = avg(M, i, j, height, width);
}
}
return result;
}
int avg(int[][] M, int i, int j, int height, int width) {
int sum = M[i][j];
int count = 1;
if (i > 0) {
sum += M[i - 1][j];
count++;
if (j > 0) {
sum += M[i - 1][j - 1];
count++;
}
if (j < width - 1) {
sum += M[i - 1][j + 1];
count++;
}
}
if (i < height - 1) {
sum += M[i + 1][j];
count++;
if (j > 0) {
sum += M[i + 1][j - 1];
count++;
}
if (j < width - 1) {
sum += M[i + 1][j + 1];
count++;
}
}
if (j > 0) {
sum += M[i][j - 1];
count++;
}
if (j < width - 1) {
sum += M[i][j + 1];
count++;
}
return sum / count;
}
public static void main(String[] args) {
ImageSmoother smoother = new ImageSmoother();
int[][] arr = new int[][]{{1,1,1},{1,0,1},{1,1,1}};
long start = System.nanoTime();
int[][] result = smoother.imageSmoother(arr);
long end = System.nanoTime();
System.out.println("运行时间:" + (end - start) / 1000000.0 + "ms");
for (int[] ints : result) {
for (int i : ints) {
System.out.print(i);
}
}
System.out.println();
start = System.nanoTime();
result = smoother.imageSmoother1(arr);
end = System.nanoTime();
System.out.println("运行时间:" + (end - start) / 1000000.0 + "ms");
for (int[] ints : result) {
for (int i : ints) {
System.out.print(i);
}
}
}
}