-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernelFilter.java
More file actions
127 lines (113 loc) · 5.04 KB
/
Copy pathKernelFilter.java
File metadata and controls
127 lines (113 loc) · 5.04 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
import java.awt.Color;
public class KernelFilter {
// Returns a new picture that applies an arbitrary kernel filter to the given picture.
private static Picture kernel(Picture picture, double[][] weights) {
int w = picture.width();
int h = picture.height();
int[][] reds = new int[w][h];
int[][] greens = new int[w][h];
int[][] blues = new int[w][h];
Picture pic = new Picture(w, h);
for (int col = 0; col < w; col++) {
for (int row = 0; row < h; row++) {
Color color = picture.get(col, row);
reds[col][row] = color.getRed();
greens[col][row] = color.getGreen();
blues[col][row] = color.getBlue();
}
}
for (int col = 0; col < w; col++) {
for (int row = 0; row < h; row++) {
double r = 0.0, g = 0.0, b = 0.0;
int length = weights.length, mid = length / 2;
for (int k = 0; k < length; k++) {
for (int l = 0; l < length; l++) {
int col_midPlusL = (col - mid + l) % w;
int row_midPlusk = (row - mid + k) % h;
if (col_midPlusL < 0) col_midPlusL = w + col_midPlusL;
if (row_midPlusk < 0) row_midPlusk = h + row_midPlusk;
r += weights[k][l] * reds[col_midPlusL][row_midPlusk];
g += weights[k][l] * greens[col_midPlusL][row_midPlusk];
b += weights[k][l] * blues[col_midPlusL][row_midPlusk];
}
}
int red = (int) Math.round(r), blue = (int) Math.round(b), green = (int) Math.round(
g);
if (red > 255) red = 255;
if (green > 255) green = 255;
if (blue > 255) blue = 255;
if (red < 0) red = 0;
if (green < 0) green = 0;
if (blue < 0) blue = 0;
Color color = new Color(red, green, blue);
pic.set(col, row, color);
}
}
return pic;
}
// Returns a new picture that applies the identity filter to the given picture.
public static Picture identity(Picture picture) {
return picture;
}
// Returns a new picture that applies a Gaussian blur filter to the given picture.
public static Picture gaussian(Picture picture) {
double[][] matrix = {
{ 0.0625, 0.125, 0.0625 }, { 0.125, 0.25, 0.125 }, { 0.0625, 0.125, 0.0625 }
};
return kernel(picture, matrix);
}
// Returns a new picture that applies a sharpen-filter to the given picture.
public static Picture sharpen(Picture picture) {
double[][] matrix = { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } };
return kernel(picture, matrix);
}
// Returns a new picture that applies a Laplacian filter to the given picture.
public static Picture laplacian(Picture picture) {
double[][] matrix = { { -1, -1, -1 }, { -1, 8, -1 }, { -1, -1, -1 } };
return kernel(picture, matrix);
}
// Returns a new picture that applies an emboss filter to the given picture.
public static Picture emboss(Picture picture) {
double[][] matrix = { { -2, -1, 0 }, { -1, 1, 1 }, { 0, 1, 2 } };
return kernel(picture, matrix);
}
// Returns a new picture that applies a motion blur filter to the given picture.
public static Picture motionBlur(Picture picture) {
double[][] matrix = {
{ 1 / 9.0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1 / 9.0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1 / 9.0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1 / 9.0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1 / 9.0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1 / 9.0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1 / 9.0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1 / 9.0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1 / 9.0 },
};
return kernel(picture, matrix);
}
// these weren't included in assignment
public static Picture outline(Picture picture) {
double[][] matrix = { { -1, -1, -1 }, { -1, 8, -1 }, { -1, -1, -1 } };
return kernel(picture, matrix);
}
public static Picture bottomSobel(Picture picture) {
double[][] matrix = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } };
return kernel(picture, matrix);
}
public static Picture topSobel(Picture picture) {
double[][] matrix = { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };
return kernel(picture, matrix);
}
public static Picture leftSobel(Picture picture) {
double[][] matrix = { { 1, 0, -1 }, { 2, 0, -2 }, { 1, 0, -1 } };
return kernel(picture, matrix);
}
public static Picture rightSobel(Picture picture) {
double[][] matrix = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
return kernel(picture, matrix);
}
// Test client (ungraded).
public static void main(String[] args) {
}
}