-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeamCarver.java
285 lines (260 loc) · 9.37 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* *****************************************************************************
* Name: Ahmed Sherif
* Date: 30/01/2024
* Description: Content Awareness Image Resizing
**************************************************************************** */
import edu.princeton.cs.algs4.Picture;
public class SeamCarver {
private Picture picture;
private double[][] energy;
private double[][] distance;
public SeamCarver(Picture picture) {
if (picture == null)
throw new IllegalArgumentException();
this.picture = new Picture(picture);
energy = new double[width()][height()];
distance = new double[width()][height()];
initEnergy();
}
private void initEnergy() {
int col = width();
int row = height();
for (int i = 0; i < col; i++) {
energy[i][0] = 1000;
energy[i][row - 1] = 1000;
}
for (int i = 1; i < row - 1; i++) {
energy[0][i] = 1000;
energy[col - 1][i] = 1000;
}
for (int x = 1; x < col - 1; x++)
for (int y = 1; y < row - 1; y++)
energy(x, y);
}
// current picture
public Picture picture() {
return this.picture;
}
// width of current picture
public int width() {
return picture.width();
}
// height of current picture
public int height() {
return picture.height();
}
// energy of pixel at column x and row y
public double energy(int x, int y) {
int col = width();
int row = height();
if (x >= col || y >= row || x < 0 || y < 0)
throw new IllegalArgumentException();
if (x > 0 && y > 0 && x < width() - 1 && y < height() - 1) {
double deltaX = getDeltaX(x, y);
double deltaY = getDeltaY(x, y);
energy[x][y] = Math.sqrt(deltaX + deltaY);
return energy[x][y];
}
energy[x][y] = 1000;
return 1000;
}
private double getDeltaX(int x, int y) {
int rgbNext = picture.getRGB(x + 1, y);
int rN = (rgbNext >> 16) & 0xFF;
int gN = (rgbNext >> 8) & 0xFF;
int bN = rgbNext & 0xFF;
int rgbPrev = picture.getRGB(x - 1, y);
int rP = (rgbPrev >> 16) & 0xFF;
int gP = (rgbPrev >> 8) & 0xFF;
int bP = rgbPrev & 0xFF;
int rX = rN - rP;
int gX = gN - gP;
int bX = bN - bP;
double deltaX = Math.pow(rX, 2) + Math.pow(gX, 2) + Math.pow(bX, 2);
return deltaX;
}
private double getDeltaY(int x, int y) {
int rgbNext = picture.getRGB(x, y + 1);
int rN = (rgbNext >> 16) & 0xFF;
int gN = (rgbNext >> 8) & 0xFF;
int bN = rgbNext & 0xFF;
int rgbPrev = picture.getRGB(x, y - 1);
int rP = (rgbPrev >> 16) & 0xFF;
int gP = (rgbPrev >> 8) & 0xFF;
int bP = rgbPrev & 0xFF;
int rY = rN - rP;
int gY = gN - gP;
int bY = bN - bP;
double deltaY = Math.pow(rY, 2) + Math.pow(gY, 2) + Math.pow(bY, 2);
return deltaY;
}
// sequence of indices for horizontal seam
/*
* each vertix has an edge with
* (x,y+1)
* (x+1,y+1)
* (x-1,y+1)
* */
public int[] findHorizontalSeam() {
transpose();
int[] seam = findVerticalSeam();
transpose();
return seam;
}
private void transpose() {
Picture transposed = new Picture(picture.height(), picture.width());
double[][] newEnergy = new double[picture.height()][picture.width()];
double[][] newDistance = new double[picture.height()][picture.width()];
for (int x = 0; x < picture.width(); x++)
for (int y = 0; y < picture.height(); y++) {
transposed.set(y, x, picture.get(x, y));
newEnergy[y][x] = energy[x][y];
newDistance[y][x] = distance[x][y];
}
energy = newEnergy;
distance = newDistance;
picture = transposed;
}
// Function to manually transpose the picture
// sequence of indices for vertical seam
public int[] findVerticalSeam() {
int[] seam = new int[height()];
if (width() == 1 || height() == 1) {
for (int x = 0; x < width(); x++) {
for (int y = 0; y < height(); y++)
seam[y] = x;
}
return seam;
}
initEnergy();
int height = picture.height() - 1;
for (int i = 0; i < picture.width(); i++)
distance[i][height] = energy[i][height];
for (int y = height() - 2; y >= 0; y--) {
for (int x = 0; x < width(); x++) {
if (x == 0)
distance[x][y] = energy[x][y] + (Math.min(distance[x][y + 1],
distance[x + 1][y + 1]));
else if (x == width() - 1)
distance[x][y] = energy[x][y] + Math.min(distance[x][y + 1],
distance[x - 1][y + 1]);
else {
distance[x][y] = energy[x][y] + Math.min(
Math.min(distance[x][y + 1], distance[x - 1][y + 1]),
distance[x + 1][y + 1]);
}
}
}
int x;
int minIndex = -1;
double min = Double.POSITIVE_INFINITY;
for (x = 0; x < width(); x++) {
if (distance[x][0] < min) {
min = distance[x][0];
minIndex = x;
}
}
seam[0] = minIndex;
for (int y = 0; y < height() - 2; y++) {
if (minIndex == width() - 1)
seam[y + 1] = distance[minIndex - 1][y + 1] >= distance[minIndex][y + 1] ?
minIndex : minIndex - 1;
else if (minIndex == 0)
seam[y + 1] = distance[minIndex + 1][y + 1] >= distance[minIndex][y + 1] ?
minIndex : minIndex + 1;
else {
min = Math.min(distance[minIndex][y + 1], Math.min(distance[minIndex - 1][y + 1],
distance[minIndex + 1][y + 1]));
if (min == distance[minIndex][y + 1])
seam[y + 1] = minIndex;
else if (min == distance[minIndex + 1][y + 1])
seam[y + 1] = minIndex + 1;
else
seam[y + 1] = minIndex - 1;
}
minIndex = seam[y + 1];
}
seam[height() - 1] = seam[height() - 2];
return seam;
}
// remove horizontal seam from current picture
public void removeHorizontalSeam(int[] seam) {
if (seam == null)
throw new IllegalArgumentException();
checkValidity(seam);
Picture newPicture = new Picture(width(), height() - 1);
for (int x = 0; x < width(); x++) {
for (int y = 0; y < height() - 1; y++) {
if (seam[x] < 0)
throw new IllegalArgumentException();
if (y < seam[x]) {
newPicture.setRGB(x, y, picture.getRGB(x, y));
}
else {
newPicture.setRGB(x, y, picture.getRGB(x, y + 1));
}
}
}
picture = newPicture;
initEnergy();
}
// remove vertical seam from current picture
public void removeVerticalSeam(int[] seam) {
if (seam == null)
throw new IllegalArgumentException();
checkValidity(seam);
Picture newPicture = new Picture(width() - 1, height());
for (int y = 0; y < height(); y++) {
for (int x = 0; x < width() - 1; x++) {
if (seam[y] < 0)
throw new IllegalArgumentException();
if (x < seam[y]) {
newPicture.setRGB(x, y, picture.getRGB(x, y));
}
else {
newPicture.setRGB(x, y, picture.getRGB(x + 1, y));
}
}
}
picture = newPicture;
initEnergy();
}
private void checkValidity(int[] seam) {
if (width() < 1 || height() < 1) {
throw new IllegalArgumentException(
"The width and height of the picture must be greatern than 1");
}
if (seam.length < 1) {
throw new IllegalArgumentException("The seam size must be greater than 1.");
}
for (int i = 0; i < seam.length - 1; i++) {
if (Math.abs(seam[i] - seam[i + 1]) > 1) {
throw new IllegalArgumentException();
}
}
}
/*
private void printEnergy() {
int row = height();
int col = width();
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++)
System.out.print(energy[c][r] + " ");
System.out.print("\n");
}
}
private void printDistanceTo() {
int row = height();
int col = width();
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++)
System.out.print(distance[c][r] + " ");
System.out.print("\n");
}
}
*/
/* public static void main(String[] args) {
SeamCarver obj = new SeamCarver(new Picture("logo.png"));
obj.printEnergy();
}*/
}