-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image_Smoother.java
63 lines (61 loc) · 2.11 KB
/
Image_Smoother.java
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
// leetcode - 661. Image Smoother
public class Image_Smoother {
public static void main(String[] args) {
int [][]img = {{1,1,1},{1,0,1},{1,1,1}};
int[][] result = imageSmoother(img);
for(int r = 0;r<result.length;r++){
for(int c = 0;c<result[0].length;c++){
System.out.print(result[r][c]+" ");
}System.out.println("\n");
}
}
public static int[][] imageSmoother(int[][] img) {
int row = img.length;
int column = img[0].length;
int[][] result = new int[row][column];
for(int r = 0;r<row;r++){
for(int c = 0;c<column;c++){
int total = 0;
int count = 0;
for(int i = r-1;i<=r+1;i++){
for(int j = c-1;j<=c+1;j++){
if(i<0 || i == row || j<0 || j==column){
continue;
}
total = total+img[i][j];
count++;
}
}
result[r][c] = total/count;
}
}
return result;
}
//optimised solution
// public static int[][] imageSmoother(int[][] img) {
// int row = img.length;
// int column = img[0].length;
// // int[][] result = new int[row][column];
// for(int r = 0;r<row;r++){
// for(int c = 0;c<column;c++){
// int total = 0;
// int count = 0;
// for(int i = r-1;i<=r+1;i++){
// for(int j = c-1;j<=c+1;j++){
// if(i<0 || i == row || j<0 || j==column){
// continue;
// }
// total = total+img[i][j] % 256;
// count++;
// }
// }
// img[r][c] = img[r][c] ^ (total / count) << 8;
// }
// }
// for(int r = 0;r<row;r++){
// for(int c = 0;c<column;c++){
// img[r][c] = img[r][c]>>8;
// }}
// return img;
// }
}