Code: import java.awt.Color; import edu.princeton.cs.algs4.Picture; public class
ID: 3583011 • Letter: C
Question
Code:
import java.awt.Color;
import edu.princeton.cs.algs4.Picture;
public class SeamCarver {
private Picture picture; // current picture.
// Create a seam carver object based on the given picture, making a
// defensive copy of picture.
public SeamCarver(Picture picture) {
...
}
// Current picture.
public Picture picture() {
...
}
// Width of current picture.
public int width() {
...
}
// Height of current picture.
public int height() {
...
}
// Energy of pixel at column x and row y.
public double energy(int x, int y) {
...
}
// Sequence of indices for horizontal seam.
public int[] findHorizontalSeam() {
...
}
// Sequence of indices for vertical seam.
public int[] findVerticalSeam() {
...
}
// Remove horizontal seam from current picture.
public void removeHorizontalSeam(int[] seam) {
...
}
// Remove vertical seam from current picture.
public void removeVerticalSeam(int[] seam) {
...
}
// Return y - 1 if x < 0; 0 if x >= y; and x otherwise.
private static int wrap(int x, int y) {
if (x < 0) {
return y - 1;
}
else if (x >= y) {
return 0;
}
return x;
}
// Return a new picture that is a transpose of the given picture.
private static Picture transpose(Picture picture) {
Picture transpose = new Picture(picture.height(), picture.width());
for (int i = 0; i < transpose.width(); i++) {
for (int j = 0; j < transpose.height(); j++) {
transpose.set(i, j, picture.get(j, i));
}
}
return transpose;
}
}
I have to replace the "...". Please help. Thank you!
We are using algs4.jar -> http://algs4.cs.princeton.edu/code/
In this assignment you will create a data type called Seamcarver that resizes a W-by-H image using the seam-carving technique. Seam carving is a content-aware image resizing technique where the image is reduced in size by one pixel of height (or width) at a time. A vertical seam in an image is a path of pixels connected from the top to the bottom with one pixel in each row a horizontal seam is a path of pixels connected from the left to the right with one pixel in each column. Below left is the iconic 298-by-298 pixel image of a Mandrill; below right is the image after removing 50 vertical and horizontal seams. Unlike Standard content-agnostic resizing techniques (such as cropping and scaling), seam carving preserves the most interesting features (aspect ratio, set of objects present, et of the image Although the underlying algorithm is simple and elegant, it was not discovered until 2007 by Shai Avidan and Ariel Shamir Now, it is a core feature in many computer graphics applications. Your task is to implement a mutable data type seamCarver, with the following API: method description. create a seamCarver object based on the given picture public SeamCarver (Picture picture) current picture public Picture picture C) width of current picture public int width height of current picture public int height energy of pixel at column z and row y public double energy (int x, int y) sequence of indices for horizontal seam public int 0 findHorizontalSeamO sequence of indices for vertical seam public int findverticalSeamO remove horizontal seam from current picture public void removeHorizontalSeamCint0 seam) remove vertical seam from current picture public id remove Vertical Seam(int[1 seam vo Finding and removing a seam involves three parts and a tiny bit of notation. In image processing, pixel (z, y) refers to the pixel in column z and row y, with pixel (0,0) at the upper-left corner and pixel (W -1,H-1) at the lower-right corner. This is consistent with the picture data type in stdlib.jar. Note that this is the opposite of the standard mathematical notation used in linear algebra, where (i,j) refers to row i and column j and (0,0) is at the lower-left corner. We also assume that the color of each pixel is represented in RGB space, using three integers between 0 and 255. This is consistent with the java awt.color data typeExplanation / Answer
import java.util.Arrays;
public class SeamCarver {
private Picture picture;
private double[][] energyTo;
private int[][] xTo;
// create a seam carver object based on the given picture
public SeamCarver(Picture picture) {
this.picture = picture;
}
// current picture
public Picture picture() {
return 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) {
if (x < 0 || x >= width()) {
throw new IndexOutOfBoundsException();
}
if (y < 0 || y >= height()) {
throw new IndexOutOfBoundsException();
}
if (x == 0 || x == width() - 1
|| y == 0 || y == height() - 1) {
return 195075;
} else {
return xGradientSquared(x, y) + yGradientSquared(x, y);
}
}
private int xGradientSquared(int x, int y) {
int redDiff = Math.abs(picture.get(x - 1, y).getRed()
- picture.get(x + 1, y).getRed());
int greenDiff = Math.abs(picture.get(x - 1, y).getGreen()
- picture.get(x + 1, y).getGreen());
int blueDiff = Math.abs(picture.get(x - 1, y).getBlue()
- picture.get(x + 1, y).getBlue());
return redDiff * redDiff + greenDiff * greenDiff + blueDiff * blueDiff;
}
private int yGradientSquared(int x, int y) {
int redDiff = Math.abs(picture.get(x, y - 1).getRed()
- picture.get(x, y + 1).getRed());
int greenDiff = Math.abs(picture.get(x, y - 1).getGreen()
- picture.get(x, y + 1).getGreen());
int blueDiff = Math.abs(picture.get(x, y - 1).getBlue()
- picture.get(x, y + 1).getBlue());
return redDiff * redDiff + greenDiff * greenDiff + blueDiff * blueDiff;
}
// sequence of indices for horizontal seam
public int[] findHorizontalSeam() {
// Transpose picture.
Picture original = picture;
Picture transpose = new Picture(original.height(), original.width());
for (int w = 0; w < transpose.width(); w++) {
for (int h = 0; h < transpose.height(); h++) {
transpose.set(w, h, original.get(h, w));
}
}
this.picture = transpose;
// call findVerticalSeam
int[] seam = findVerticalSeam();
// Transpose back.
this.picture = original;
return seam;
}
// sequence of indices for vertical seam
public int[] findVerticalSeam() {
energyTo = new double[width()][height()];
xTo = new int[width()][height()];
for (int x = 0; x < width(); x++) {
for (int y = 0; y < height(); y++) {
energyTo[x][y] = Double.POSITIVE_INFINITY;
}
}
for (int x = 0; x < width(); x++) {
energyTo[x][0] = 195075;
}
for (int y = 0; y < height() - 1; y++) {
for (int x = 0; x < width(); x++) {
if (x > 0) {
relax(x, y, x - 1, y + 1);
}
relax(x, y, x, y + 1);
if (x < width() - 1) {
relax(x, y, x + 1, y + 1);
}
}
}
// find minimum energy path
double minEnergy = Double.POSITIVE_INFINITY;
int minEnergyX = -1;
for (int w = 0; w < width(); w++) {
if (energyTo[w][height() - 1] < minEnergy) {
minEnergyX = w;
minEnergy = energyTo[w][height() - 1];
}
}
assert minEnergyX != -1;
int[] seam = new int[height()];
seam[height() - 1] = minEnergyX;
int prevX = xTo[minEnergyX][height() - 1];
for (int h = height() - 2; h >= 0; h--) {
seam[h] = prevX;
prevX = xTo[prevX][h];
}
return seam;
}
private void relax(int x1, int y1, int x2, int y2) {
if (energyTo[x2][y2] > energyTo[x1][y1] + energy(x2, y2)) {
energyTo[x2][y2] = energyTo[x1][y1] + energy(x2, y2);
xTo[x2][y2] = x1;
}
}
// remove horizontal seam from current picture
public void removeHorizontalSeam(int[] seam) {
// Transpose picture.
Picture original = picture;
Picture transpose = new Picture(original.height(), original.width());
for (int w = 0; w < transpose.width(); w++) {
for (int h = 0; h < transpose.height(); h++) {
transpose.set(w, h, original.get(h, w));
}
}
this.picture = transpose;
transpose = null;
original = null;
// call removeVerticalSeam
removeVerticalSeam(seam);
// Transpose back.
original = picture;
transpose = new Picture(original.height(), original.width());
for (int w = 0; w < transpose.width(); w++) {
for (int h = 0; h < transpose.height(); h++) {
transpose.set(w, h, original.get(h, w));
}
}
this.picture = transpose;
transpose = null;
original = null;
}
// remove vertical seam from current picture
public void removeVerticalSeam(int[] seam) {
if (seam == null) {
throw new NullPointerException();
}
if (seam.length != height()) {
throw new IllegalArgumentException();
}
Picture original = this.picture;
Picture carved = new Picture(original.width() - 1, original.height());
for (int h = 0; h < carved.height(); h++) {
for (int w = 0; w < seam[h]; w++) {
carved.set(w, h, original.get(w, h));
}
for (int w = seam[h]; w < carved.width(); w++) {
carved.set(w, h, original.get(w + 1, h));
}
}
this.picture = carved;
}
public static void main(String[] args) {
Picture picture;
SeamCarver seamCarver;
int[] seam;
picture = new Picture("seamCarving/4x6.png");
// picture = new Picture("seamCarving/6x5.png");
seamCarver = new SeamCarver(picture);
// StdOut.println(seamCarver.energy(0, 0)); // should be 195075.0
// StdOut.println(seamCarver.energy(1, 1)); // should be 56334.0
for (int i = 0; i < seamCarver.width(); i++) {
for (int j = 0; j < seamCarver.height(); j++) {
System.out.println("energy(" + i + "," + j + "): "
+ seamCarver.energy(i, j));
}
}
// should be 6
System.out.println(seamCarver.width());
// should be 5
System.out.println(seamCarver.height());
// should be 5
seam = seamCarver.findVerticalSeam();
System.out.println(seam.length);
System.out.println(Arrays.toString(seam));
seamCarver.removeVerticalSeam(seam);
for (int i = 0; i < seamCarver.width(); i++) {
for (int j = 0; j < seamCarver.height(); j++) {
System.out.println("energy(" + i + "," + j + "): "
+ seamCarver.energy(i, j));
}
}
seam = seamCarver.findHorizontalSeam();
System.out.println(Arrays.toString(seam));
System.out.println(seam.length);
seamCarver.removeHorizontalSeam(seam);
}
}