-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSCUtility.java
107 lines (87 loc) · 3.64 KB
/
SCUtility.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
/******************************************************************************
* Compilation: javac SCUtility.java
* Execution: none
* Dependencies: SeamCarver.java
*
* Some utility functions for testing SeamCarver.java.
*
******************************************************************************/
import java.awt.Color;
import edu.princeton.cs.algs4.Picture;
import edu.princeton.cs.algs4.StdRandom;
public class SCUtility {
// create random W-by-H picture
public static Picture randomPicture(int W, int H) {
Picture picture = new Picture(W, H);
for (int col = 0; col < W; col++) {
for (int row = 0; row < H; row++) {
int r = StdRandom.uniform(256);
int g = StdRandom.uniform(256);
int b = StdRandom.uniform(256);
Color color = new Color(r, g, b);
picture.set(col, row, color);
}
}
return picture;
}
// convert SeamCarver picture to W-by-H energy matrix
public static double[][] toEnergyMatrix(SeamCarver sc) {
double[][] a = new double[sc.width()][sc.height()];
for (int col = 0; col < sc.width(); col++)
for (int row = 0; row < sc.height(); row++)
a[col][row] = sc.energy(col, row);
return a;
}
// displays grayscale values as energy (converts to picture, calls show)
public static void showEnergy(SeamCarver sc) {
doubleToPicture(toEnergyMatrix(sc)).show();
}
// returns picture of energy matrix associated with SeamCarver picture
public static Picture toEnergyPicture(SeamCarver sc) {
double[][] energyMatrix = toEnergyMatrix(sc);
return doubleToPicture(energyMatrix);
}
// converts a double matrix of values into a normalized picture
// values are normalized by the maximum grayscale value
public static Picture doubleToPicture(double[][] grayValues) {
//each 1D array in the matrix represents a single column, so number
//of 1D arrays is the width, and length of each array is the height
int width = grayValues.length;
int height = grayValues[0].length;
Picture picture = new Picture(width, height);
double maxVal = 0;
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
if (grayValues[col][row] > maxVal) {
maxVal = grayValues[col][row];
}
}
}
if (maxVal == 0)
return picture; // return black picture
for (int col = 0; col < width; col++) {
for (int row = 0; row < height; row++) {
float normalizedGrayValue = (float) grayValues[col][row] / (float) maxVal;
Color gray = new Color(normalizedGrayValue, normalizedGrayValue, normalizedGrayValue);
picture.set(col, row, gray);
}
}
return picture;
}
// This method is useful for debugging seams. It overlays red
// pixels over the calculate seam.
public static Picture seamOverlay(Picture picture, boolean isHorizontal, int[] seam) {
Picture overlaid = new Picture(picture);
//if horizontal seam, set one pixel in every column
if (isHorizontal) {
for (int col = 0; col < picture.width(); col++)
overlaid.set(col, seam[col], Color.RED);
}
// if vertical seam, set one pixel in each row
else {
for (int row = 0; row < picture.height(); row++)
overlaid.set(seam[row], row, Color.RED);
}
return overlaid;
}
}