-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobertsOperator.java
More file actions
57 lines (53 loc) · 1.78 KB
/
RobertsOperator.java
File metadata and controls
57 lines (53 loc) · 1.78 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
/**
*
* @author CuongAcQuy
*/
import java.util.Scanner;
public class RobertsOperator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Nhận kích thước ảnh (ảnh vuông)
int n = sc.nextInt();
// Khởi tạo ma trận cấp xám của ảnh
int[][] a = new int[n][n];
// Nhận giá trị của ma trận cấp xám
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
// Khởi tạo ma trận Gx và Gy cho Roberts Operator
int[][] Gx = {{-1, 0}, {0, 1}};
int[][] Gy = {{0, -1}, {1, 0}};
// Khởi tạo ma trận gradient
int[][] gradient = new int[n][n];
// Tính gradient theo công thức G = sqrt(Gx^2 + Gy^2)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
int GxValue = 0;
int GyValue = 0;
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
GxValue += Gx[k][l] * a[i + k][j + l];
GyValue += Gy[k][l] * a[i + k][j + l];
}
}
gradient[i + 1][j + 1] = (int) Math.sqrt(GxValue * GxValue + GyValue * GyValue);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(i == n-1 || j == n-1){
gradient[i][j] = 0;
}
}
}
// In ảnh gradient
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(gradient[i][j] + " ");
}
System.out.println();
}
}
}