-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageSmoother.java
More file actions
59 lines (51 loc) · 1.7 KB
/
Copy pathImageSmoother.java
File metadata and controls
59 lines (51 loc) · 1.7 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
import java.util.Scanner;
public class ImageSmoother {
public int[][] imageSmoother(int[][] img) {
int m = img.length;
int n = img[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int total = 0;
int cells = 0;
for (int x = i - 1; x <= i + 1; x++) {
for (int y = j - 1; y <= j + 1; y++) {
if (x >= 0 && x < m && y >= 0 && y < n) {
total += img[x][y] % 256;
cells++;
}
}
}
img[i][j] += (total / cells) * 256;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
img[i][j] /= 256;
}
}
return img;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input row size: ");
int m = scanner.nextInt();
System.out.print("Input col size: ");
int n = scanner.nextInt();
int[][] img = new int[m][n];
System.out.println("Enter the matrix elements:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
img[i][j] = scanner.nextInt();
}
}
ImageSmoother smoother = new ImageSmoother();
img = smoother.imageSmoother(img);
System.out.println("Smoothened matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(img[i][j] + " ");
}
System.out.println();
}
}
}