-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSeamCarver.java
254 lines (210 loc) · 8.02 KB
/
SeamCarver.java
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
package sc;
import java.awt.Color;
import java.lang.IllegalArgumentException;
import java.lang.IndexOutOfBoundsException;
import java.lang.Math;
import java.util.HashMap;
import edu.princeton.cs.algs4.Picture;
public class SeamCarver {
private Picture pic;
private int width;
private int height;
//Constructor
public SeamCarver(Picture picture) {
pic = new Picture(picture);
width = pic.width();
height = pic.height();
}
//The picture contained in the sc data structure.
public Picture picture() {
return pic;
}
//Width of the picture
public int width() {
return width;
}
//Height of the picture
public int height() {
return height;
}
//Returns the energy of a pixel, given its coordinates.
//Energy is a calculation of the contrast between a pixel's RGB values
// and its neighboring pixels' RGB values.
public double energy(int x, int y) {
if (x < 0 || x >= width() || y < 0 || y >= height())
throw new IndexOutOfBoundsException();
if (x == 0 || x == width() - 1 || y == 0 || y == height() - 1)
{
return Math.pow(255.0, 2) * 3;
}
double deltaX = 0.0, deltaY = 0.0;
Color x1, x2, y1, y2;
x1 = pic.get(x - 1, y);
x2 = pic.get(x + 1, y);
y1 = pic.get(x, y - 1);
y2 = pic.get(x, y + 1);
deltaX = Math.pow((x1.getRed() - x2.getRed()), 2) + Math.pow((x1.getGreen() - x2.getGreen()), 2) + Math.pow((x1.getBlue() - x2.getBlue()), 2);
deltaY = Math.pow((y1.getRed() - y2.getRed()), 2) + Math.pow((y1.getGreen() - y2.getGreen()), 2) + Math.pow((y1.getBlue() - y2.getBlue()), 2);
return deltaX + deltaY;
}
private int[] getSeam(String mode, HashMap edgeTo, String end) {
int size;
if (mode.equals("h"))
size = width();
else if (mode.equals("v"))
size = height();
else
throw new IllegalArgumentException();
int[] path = new int[size];
String cur = end;
while (size > 0) {
path[--size] = str2id(mode, cur);
cur = (String)edgeTo.get(cur);
}
// path represents the seam as a 1D array of the coordinates in the seam.
//y-coordinates are stored if the seam traverses horizontally.
//x-coordinates are stored if the seam traverses vertically.
return path;
}
private String id2str(int col, int row) {
return col + " " + row;
}
private int str2id(String mode, String str) {
if (mode.equals("v"))
return Integer.parseInt(str.split(" ")[0]);
else if (mode.equals("h"))
return Integer.parseInt(str.split(" ")[1]);
else
throw new IllegalArgumentException();
}
// Loops through indices to find horizontal seam.
public int[] findHorizontalSeam() {
String mode = "h";
HashMap<String, String> edgeTo = new HashMap<String, String>();
HashMap<String, Double> energyTo = new HashMap<String, Double>();
double cost = Double.MAX_VALUE;
//cur represents the current pixel.
//next represents a potential pixel to connect cur to.
String cur, next, end = null;
for (int col = 0; col < width() - 1; col++)
for (int row = 0; row < height(); row++) {
cur = id2str(col, row);
if (col == 0) {
edgeTo.put(cur, null);
energyTo.put(cur, energy(col, row));
}
for (int i = row - 1; i <= row + 1; i++)
if (i >= 0 && i < height()) {
next = id2str(col + 1, i);
double newEng = energy(col + 1, i) + energyTo.get(cur);
//If we don't have a next edge yet, add one. Or, if this edge
// is better than the one we have, use it.
if (energyTo.get(next) == null || newEng < energyTo.get(next)) {
edgeTo.put(next, cur);
energyTo.put(next, newEng);
//End at the second to last column, because 'next' inolves
// the next column.
if (col + 1 == width() - 1 && newEng < cost) {
cost = newEng;
end = next;
}
}
}
}
return getSeam(mode, edgeTo, end);
}
// Loops through indices to find vertical seam.
public int[] findVerticalSeam() {
//See comments in findHorizontalSeam() for equivalent explanations.
String mode = "v";
HashMap<String, String> edgeTo = new HashMap<String, String>();
HashMap<String, Double> energyTo = new HashMap<String, Double>();
double cost = Double.MAX_VALUE;
String cur, next, end = null;
for (int row = 0; row < height() - 1; row++)
for (int col = 0; col < width(); col++) {
cur = id2str(col, row);
if (row == 0) {
edgeTo.put(cur, null);
energyTo.put(cur, energy(col, row));
}
for (int k = col - 1; k <= col + 1; k++)
if (k >= 0 && k < width()) {
next = id2str(k, row + 1);
double newEng = energy(k, row + 1) + energyTo.get(cur);
if (energyTo.get(next) == null || newEng < energyTo.get(next)) {
edgeTo.put(next, cur);
energyTo.put(next, newEng);
if (row + 1 == height() - 1 && newEng < cost) {
cost = newEng;
end = next;
}
}
}
}
return getSeam(mode, edgeTo, end);
}
private boolean isValidSeam(int[] seam) {
for (int i = 0; i < seam.length - 1; i++) {
if (Math.abs(seam[i] - seam[i + 1]) > 1) {
return false;
}
}
return true;
}
// Removes horizontal seam from picture.
public void removeHorizontalSeam(int[] seam) {
if (width() <= 1 || height() <= 1 || seam.length < 0 || seam.length >width() || !isValidSeam(seam))
throw new IllegalArgumentException();
Picture newPic = new Picture(width(), height() - 1);
for (int col = 0; col < width(); col++)
for (int row = 0; row < height() - 1; row++) {
if (row < seam[col])
newPic.set(col, row, pic.get(col, row));
else
newPic.set(col, row, pic.get(col, row + 1));
}
height--;
pic = new Picture(newPic);
}
// Removes vertical seam from picture.
public void removeVerticalSeam(int[] seam) {
if (width() <= 1 || height() <= 1 || seam.length < 0 || seam.length > height() || !isValidSeam(seam))
throw new IllegalArgumentException();
Picture newPic = new Picture(width() - 1, height());
for (int row = 0; row < height(); row++)
for (int col = 0; col < width() - 1; col++) {
if (col < seam[row])
newPic.set(col, row, pic.get(col, row));
else
newPic.set(col, row, pic.get(col + 1, row));
}
width--;
pic = new Picture(newPic);
}
// Resizes the picture to a specified width or height.
public Picture resizeTo(String mode, int dimension)
{
// Resize the width; remove vertical seams.
if (mode.equals("width")) {
while (this.width() > dimension) {
System.out.println("resizing... Currently at width " +
this.width());
int[] seam = this.findVerticalSeam();
this.removeVerticalSeam(seam);
}
}
// Resize the height; remove horizontal seams.
else if (mode.equals("height")) {
while (this.height() > dimension) {
System.out.println("resizing... Currently at height " +
this.height());
int[] seam = this.findHorizontalSeam();
this.removeHorizontalSeam(seam);
}
}
else throw new IllegalArgumentException();
// Return the resized image.
return this.picture();
}
}