-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeamCarver.java
More file actions
218 lines (180 loc) · 4.69 KB
/
SeamCarver.java
File metadata and controls
218 lines (180 loc) · 4.69 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import edu.princeton.cs.algs4.Picture;
public class SeamCarver {
// create a seam carver object based on the given picture
private double energy[][];
private int rgb[][];
public SeamCarver(Picture picture) {
if (picture == null) {
throw new java.lang.IllegalArgumentException();
}
rgb = new int[picture.height()][picture.width()];
for (int i=0; i<picture.height(); i++) {
for (int j=0; j<picture.width(); j++) {
rgb[i][j] = picture.getRGB(j, i);
}
}
calcEnergyMatrix();
}
// current picture
public Picture picture() {
Picture p = new Picture(rgb[0].length, rgb.length);
for (int i=0; i<height(); i++) {
for (int j=0; j<width(); j++) {
p.setRGB(j, i, rgb[i][j]);
}
}
return p;
}
// width of current picture
public int width() {
return rgb[0].length;
}
// height of current picture
public int height() {
return rgb.length;
}
private double getDelta(int a, int b) {
int ra = (a >> 16) & 0xFF;
int ga = (a >> 8) & 0xFF;
int ba = (a >> 0) & 0xFF;
int rb = (b >> 16) & 0xFF;
int gb = (b >> 8) & 0xFF;
int bb = (b >> 0) & 0xFF;
return Math.pow(ra - rb,2) + Math.pow(ga - gb,2) + Math.pow(ba - bb,2);
}
private double calcEnergy(int row, int col) {
if (row == 0 || col == 0 || row == height()-1 || col == width()-1) {
return 1000;
}
int l = rgb[row][col-1];
int r = rgb[row][col+1];
int u = rgb[row-1][col];
int d = rgb[row+1][col];
Double delta1 = getDelta(l, r);
Double delta2 = getDelta(u, d);
return Math.sqrt(delta1 + delta2);
}
// energy of pixel at column x and row y
public double energy(int x, int y) {
if (x < 0 || y < 0 || x >= width() || y >= height()) {
throw new java.lang.IllegalArgumentException();
}
return energy[y][x];
}
// sequence of indices for horizontal seam
public int[] findHorizontalSeam() {
transposeRGB();
transposeEnergy();
int id[] = findVerticalSeam();
transposeRGB();
transposeEnergy();
return id;
}
// sequence of indices for vertical seam
public int[] findVerticalSeam() {
double[][] minSumEnengy = new double[height()][width()];
int[][] link = new int[height()][width()];
for (int i=0; i<height(); i++) {
for (int j=0; j<width(); j++) {
minSumEnengy[i][j] = Double.MAX_VALUE;
}
}
for (int i=0; i<height()-1; i++) {
for (int j=0; j<width(); j++) {
if (i==0) {
minSumEnengy[i][j] = energy[i][j];
}
for (int k=-1; k<=1; k++) {
if (j+k<0 || j+k>=width()) {
continue;
}
double min = minSumEnengy[i][j] + energy[i+1][j+k];
if (min < minSumEnengy[i+1][j+k]) {
minSumEnengy[i+1][j+k] = min;
link[i+1][j+k] = j;
}
}
}
}
double min = Double.MAX_VALUE;
int minIndex = 0;
for (int j=0; j<width(); j++) {
if (minSumEnengy[height()-1][j] < min) {
min = minSumEnengy[height()-1][j];
minIndex = j;
}
}
int [] id = new int[height()];
for (int i=height()-1; i>=0; i--) {
id[i] = minIndex;
minIndex = link[i][minIndex];
}
return id;
}
// remove horizontal seam from current picture
public void removeHorizontalSeam(int[] seam) {
if (seam == null || seam.length != width()) {
throw new java.lang.IllegalArgumentException();
}
checkSeamDistance(seam);
transposeRGB();
removeVerticalSeam(seam);
transposeRGB();
transposeEnergy();
}
// remove vertical seam from current picture
public void removeVerticalSeam(int[] seam) {
if (seam == null || seam.length != height()) {
throw new java.lang.IllegalArgumentException();
}
checkSeamDistance(seam);
int [][] newRgb = new int [height()][width()-1];
for (int i=0; i<height(); i++) {
int k=0;
for (int j=0; j<width(); j++) {
if (seam[i] <0 || seam[i] >= width()) {
throw new java.lang.IllegalArgumentException();
}
if (j==seam[i]) {
continue;
}
newRgb[i][k++] = rgb[i][j];
}
}
rgb = newRgb;
calcEnergyMatrix();
}
private void transposeEnergy() {
double [][] newEnergy = new double [energy[0].length][energy.length];
for (int i=0; i<energy.length; i++) {
for (int j=0; j<energy[0].length; j++) {
newEnergy[j][i] = energy[i][j];
}
}
energy = newEnergy;
}
private void transposeRGB() {
int [][] newRgb = new int [width()][height()];
for (int i=0; i<height(); i++) {
for (int j=0; j<width(); j++) {
newRgb[j][i] = rgb[i][j];
}
}
rgb = newRgb;
}
private void calcEnergyMatrix() {
energy = new double[height()][width()];
for (int i=0; i<height(); i++) {
for (int j=0; j<width(); j++) {
energy[i][j] = calcEnergy(i, j);
}
}
}
private void checkSeamDistance(int[] seam) {
for (int i=0; i<seam.length-1; i++) {
if (Math.abs(seam[i]-seam[i+1]) > 1) {
throw new java.lang.IllegalArgumentException();
}
}
}
}