-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintSeams.java
94 lines (81 loc) · 3.31 KB
/
PrintSeams.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
/*
/ *****************************************************************************
* Compilation: javac PrintSeams.java
* Execution: java PrintSeams input.png
* Dependencies: SeamCarver.java
*
* Read image from file specified as command-line argument. Print square
* of energies of pixels, a vertical seam, and a horizontal seam.
*
* % java PrintSeams 6x5.png
* 6x5.png (6-by-5 image)
*
* The table gives the dual-gradient energies of each pixel.
* The asterisks denote a minimum energy vertical or horizontal seam.
*
* Vertical seam: { 3 4 3 2 1 }
* 1000.00 1000.00 1000.00 1000.00* 1000.00 1000.00
* 1000.00 237.35 151.02 234.09 107.89* 1000.00
* 1000.00 138.69 228.10 133.07* 211.51 1000.00
* 1000.00 153.88 174.01* 284.01 194.50 1000.00
* 1000.00 1000.00* 1000.00 1000.00 1000.00 1000.00
* Total energy = 2414.973496
*
*
* Horizontal seam: { 2 2 1 2 1 2 }
* 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00
* 1000.00 237.35 151.02* 234.09 107.89* 1000.00
* 1000.00* 138.69* 228.10 133.07* 211.51 1000.00*
* 1000.00 153.88 174.01 284.01 194.50 1000.00
* 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00
* Total energy = 2530.681960
*
*****************************************************************************
*/
import edu.princeton.cs.algs4.Picture;
import edu.princeton.cs.algs4.StdOut;
public class PrintSeams {
private static final boolean HORIZONTAL = true;
private static final boolean VERTICAL = false;
private static void printSeam(SeamCarver carver, int[] seam, boolean direction) {
double totalSeamEnergy = 0.0;
for (int row = 0; row < carver.height(); row++) {
for (int col = 0; col < carver.width(); col++) {
double energy = carver.energy(col, row);
String marker = " ";
if ((direction == HORIZONTAL && row == seam[col]) ||
(direction == VERTICAL && col == seam[row])) {
marker = "*";
totalSeamEnergy += energy;
}
StdOut.printf("%7.2f%s ", energy, marker);
}
StdOut.println();
}
// StdOut.println();
StdOut.printf("Total energy = %f\n", totalSeamEnergy);
StdOut.println();
StdOut.println();
}
public static void main(String[] args) {
Picture picture = new Picture(args[0]);
StdOut.printf("%s (%d-by-%d image)\n", args[0], picture.width(), picture.height());
StdOut.println();
StdOut.println("The table gives the dual-gradient energies of each pixel.");
StdOut.println("The asterisks denote a minimum energy vertical or horizontal seam.");
StdOut.println();
SeamCarver carver = new SeamCarver(picture);
StdOut.printf("Vertical seam: { ");
int[] verticalSeam = carver.findVerticalSeam();
for (int x : verticalSeam)
StdOut.print(x + " ");
StdOut.println("}");
printSeam(carver, verticalSeam, VERTICAL);
StdOut.printf("Horizontal seam: { ");
int[] horizontalSeam = carver.findHorizontalSeam();
for (int y : horizontalSeam)
StdOut.print(y + " ");
StdOut.println("}");
printSeam(carver, horizontalSeam, HORIZONTAL);
}
}