-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFilter.java
More file actions
executable file
·72 lines (59 loc) · 1.72 KB
/
Filter.java
File metadata and controls
executable file
·72 lines (59 loc) · 1.72 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
package bric.Image;
public class Filter {
private int[][] filter;
private int width;
private int height;
public Filter(int[][] k, int x, int y) {
filter = k;
width = x;
height = y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getWeight() {
int w = 0;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
w += filter[i][j];
}
}
return w;
}
public ImageLayer convolve(ImageLayer src, boolean weighted) {
int[][] s = src.getImage();
int sW = src.getWidth();
int sH = src.getHeight();
int[][] r = new int[sH][sW]; //convolved image; returned
for (int i = 0; i < sH; ++i) { //row
for (int j = 0; j < sW; ++j) { //col //loop over image
int sum = 0;
for (int h = 0; h < height; ++h) { //row
for (int w = 0; w < width; ++w) { //col // loop over filter
if ( (i + h - 1) >= 0 && //check upper boundary
(i + h - 1) < sH && //check lower boundary
(j + w - 1) >= 0 && //check left boundary
(j + w - 1) < sW ){ //check right boundary
sum += filter[h][w] * s[i + h - 1][j + w - 1];
}
else {
sum += s[i][j];
}
}
} //end filter loop
int w = this.getWeight();
if (weighted && w != 0)
r[i][j] = sum/w;
else
r[i][j] = sum;
}
}
return new ImageLayer(r, sW, sH, src.getColor());
}
public ImageLayer convolve(ImageLayer src) {
return convolve(src, true);
}
}