-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeamCarver.java
More file actions
266 lines (208 loc) · 7.31 KB
/
SeamCarver.java
File metadata and controls
266 lines (208 loc) · 7.31 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import edu.princeton.cs.algs4.Picture;
public class SeamCarver
{
public static void main(String[] args)
{}
// create a seam carver object based on the given picture
public SeamCarver(Picture picture)
{
if (picture == null)
{throw new java.lang.IllegalArgumentException();}
int width = picture.width();
int height = picture.height();
colors = new int[width][height];
for (int i=0; i<width; i++)
{
for (int j=0; j<height; j++)
{
colors[i][j]=picture.getRGB(i, j);
}
}
}
// create a seam carver object based on the given picture
private int[][] colors;
// current picture
public Picture picture()
{
Picture picture = new Picture(colors.length,colors[0].length);
for (int i=0; i<colors.length; i++)
{
for (int j=0; j<colors[0].length; j++)
{
picture.setRGB(i,j,colors[i][j]);
}
}
return picture;
}
// current picture
// width of current picture
public int width()
{return this.colors.length;}
// width of current picture
// height of current picture
public int height()
{return this.colors[0].length;}
// height of current picture
// energy of pixel at column x and row y
public double energy(int x, int y)
{
if (x<0||x>this.width()-1||y<0||y>this.height()-1)
{throw new java.lang.IllegalArgumentException();}
if (x==0||x==this.width()-1||y==0||y==this.height()-1)
{return 1000.0;}
return Math.sqrt(gradx(x, y)+grady(x, y));
}
// energy of pixel at column x and row y
private int gradx(int x, int y)
{
int rgb1 = colors[x-1][y];
int r1 = (rgb1 >> 16) & 0xFF;
int g1 = (rgb1 >> 8) & 0xFF;
int b1 = (rgb1 >> 0) & 0xFF;
int rgb2 = colors[x+1][y];
int r2 = (rgb2 >> 16) & 0xFF;
int g2 = (rgb2 >> 8) & 0xFF;
int b2 = (rgb2 >> 0) & 0xFF;
int sum = (r1-r2)*(r1-r2)
+(g1-g2)*(g1-g2)
+(b1-b2)*(b1-b2);
return sum;
}
private int grady(int x, int y)
{
int rgb1 = colors[x][y-1];
int r1 = (rgb1 >> 16) & 0xFF;
int g1 = (rgb1 >> 8) & 0xFF;
int b1 = (rgb1 >> 0) & 0xFF;
int rgb2 = colors[x][y+1];
int r2 = (rgb2 >> 16) & 0xFF;
int g2 = (rgb2 >> 8) & 0xFF;
int b2 = (rgb2 >> 0) & 0xFF;
int sum = (r1-r2)*(r1-r2)
+(g1-g2)*(g1-g2)
+(b1-b2)*(b1-b2);
return sum;
}
// sequence of indices for horizontal seam
public int[] findHorizontalSeam()
{
this.colors = transpose(this.colors);
int[] seam = findVerticalSeam();
this.colors = transpose(this.colors);
return seam;
}
// sequence of indices for horizontal seam
private int[][] transpose(int[][] org)
{
if (org == null) throw new IllegalArgumentException();
int[][] rvs = new int[org[0].length][org.length];
for (int x=0; x<org[0].length; x++)
{
for(int y=0; y<org.length; y++)
{
rvs[x][y] = org[y][x];
}
}
return rvs;
}
// sequence of indices for vertical seam
public int[] findVerticalSeam()
{
//find smallest path via topological order
int n = colors.length*colors[0].length;
int[] vertexTo = new int[n];
double[] distTo = new double[n];
//set initial distance
for (int v=0; v<n; v++)
{
if (v<colors.length)
{distTo[v] = 0;}
else
{distTo[v] = Double.POSITIVE_INFINITY;}
}
for (int j=0; j<colors[0].length-1; j++)
{
for (int i=0; i<colors.length; i++)
{
for (int k=-1; k<2; k++)
{
if ((i+k<colors.length)&&(i+k>=0))
{
int v = i+j*colors.length;//from [i][j]
int w = i+k+(j+1)*colors.length;//to [i+k][j+1]
double energy = energy(i+k,j+1);
if (distTo[w] > distTo[v]+energy)
{
distTo[w] = distTo[v]+energy;
vertexTo[w] = v;
}
}
}
}
}
//generate int[] seam!
int[] seam =new int[colors[0].length];
//find the last element of seam[]
double min = distTo[(height()-1)*width()];
int end = (height()-1)*width();
for (int i=(height()-1)*width(); i<height()*width(); i++)
{
if (min>=distTo[i])
{min = distTo[i]; end = i;}
}
//get value of seam[] from what we built
seam[height()-1] = end - (height()-1)*width();
for (int i=height()-2; i>-1; i--)
{
end = vertexTo[end];
seam[i] = end - i*width();
}
return seam;
}
// sequence of indices for vertical seam
private int index(int x, int y)
{
return width() * y + x;
}
// remove horizontal seam from current picture
public void removeHorizontalSeam(int[] seam)
{
if (seam == null)
{throw new java.lang.IllegalArgumentException();}
if (seam.length!=width())
{throw new java.lang.IllegalArgumentException();}
for (int i=0; i<seam.length; i++)
{
if (seam[i]<0||seam[i]>height()-1)
{throw new java.lang.IllegalArgumentException();}
if (i!=seam.length-1&&(seam[i]-seam[i+1]>1||seam[i]-seam[i+1]<-1))
{throw new java.lang.IllegalArgumentException();}
}
if (height()<=1)
{throw new java.lang.IllegalArgumentException();}
int[][] update = new int[width()][height()-1];
for (int j=0; j<width(); j++)
{
if (seam[j] == 0)
{System.arraycopy(this.colors[j], 1, update[j], 0, height()-1);}
else if (seam[j] == height()-1)
{System.arraycopy(this.colors[j], 0, update[j], 0, height()-1);}
else
{
System.arraycopy(this.colors[j],0, update[j], 0, seam[j]);
System.arraycopy(this.colors[j], seam[j]+1,
update[j], seam[j], height()-seam[j]-1);
}
}
this.colors = update;
}
// remove horizontal seam from current picture
// remove vertical seam from current picture
public void removeVerticalSeam(int[] seam)
{
this.colors = transpose(this.colors);
removeHorizontalSeam(seam);
this.colors = transpose(this.colors);
}
// remove vertical seam from current picture
}