summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-II/2-SeamCarving/SCUtility.java
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-12-12 15:40:02 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2013-12-12 15:40:02 +0100
commite1ec44b3078a20c4e7ab85e30083063205159761 (patch)
tree195bad21bd885dc6a1f78ba741b16a1355ef4d92 /Algorithms/Part-II/2-SeamCarving/SCUtility.java
parenta60ba769f451259caba14886fdb206593b92df76 (diff)
downloadcoursera-e1ec44b3078a20c4e7ab85e30083063205159761.zip
coursera-e1ec44b3078a20c4e7ab85e30083063205159761.tar.gz
Algorithms-II : 2-SeamCarving: add prototypes and data
Diffstat (limited to 'Algorithms/Part-II/2-SeamCarving/SCUtility.java')
-rw-r--r--Algorithms/Part-II/2-SeamCarving/SCUtility.java105
1 files changed, 105 insertions, 0 deletions
diff --git a/Algorithms/Part-II/2-SeamCarving/SCUtility.java b/Algorithms/Part-II/2-SeamCarving/SCUtility.java
new file mode 100644
index 0000000..bcd95f3
--- /dev/null
+++ b/Algorithms/Part-II/2-SeamCarving/SCUtility.java
@@ -0,0 +1,105 @@
+import java.awt.Color;
+
+public class SCUtility {
+
+
+ // create random W-by-H array of tiles
+ public static Picture randomPicture(int W, int H) {
+ Picture p = new Picture(W, H);
+ for (int i = 0; i < W; i++)
+ for (int j = 0; j < H; j++) {
+ int r = StdRandom.uniform(255);
+ int g = StdRandom.uniform(255);
+ int b = StdRandom.uniform(255);
+ Color c = new Color(r, g, b);
+ p.set(i, j, c);
+ }
+ return p;
+ }
+
+
+ public static double[][] toEnergyMatrix(SeamCarver sc)
+ {
+ double[][] returnDouble = new double[sc.width()][sc.height()];
+ for (int i = 0; i < sc.width(); i++)
+ for (int j = 0; j < sc.height(); j++)
+ returnDouble[i][j] = sc.energy(i, j);
+
+ return returnDouble;
+ }
+
+ // displays grayvalues as energy (converts to picture, calls show)
+ public static void showEnergy(SeamCarver sc)
+ {
+ doubleToPicture(toEnergyMatrix(sc)).show();
+ }
+
+ 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 p = new Picture(width, height);
+
+ double maxVal = 0;
+ for (int i = 0; i < width; i++)
+ for (int j = 0; j < height; j++)
+ if (grayValues[i][j] > maxVal)
+ maxVal = grayValues[i][j];
+
+ if (maxVal == 0)
+ return p; //return black picture
+
+ for (int i = 0; i < width; i++)
+ for (int j = 0; j < height; j++)
+ {
+ float normalizedGrayValue =
+ (float) grayValues[i][j] / (float) maxVal;
+ p.set(i, j, new Color(normalizedGrayValue,
+ normalizedGrayValue, normalizedGrayValue));
+ }
+
+ return p;
+ }
+
+
+ // This method is useful for debugging seams. It overlays red
+ // pixels over the calculate seam. Due to the lack of a copy
+ // constructor, it also alters the original picture.
+
+ public static Picture seamOverlay(Picture p, boolean horizontal,
+ int[] seamIndices)
+ {
+ Picture overlaid = new Picture(p.width(), p.height());
+
+ for (int i = 0; i < p.width(); i++)
+ for (int j = 0; j < p.height(); j++)
+ overlaid.set(i, j, p.get(i, j));
+
+ int width = p.width();
+ int height = p.height();
+
+ //if horizontal seam, then set one pixel in every column
+ if (horizontal)
+ for (int i = 0; i < width; i++)
+ overlaid.set(i, seamIndices[i], new Color(255, 0, 0));
+ else //if vertical, put one pixel in every row
+ for (int j = 0; j < height; j++)
+ overlaid.set(seamIndices[j], j, new Color(255, 0, 0));
+
+ return overlaid;
+ }
+
+
+}