public SeamCarver(Picture picture) { this.picture = new Picture(picture); energy = new double[picture.width()][picture.height()]; parent = new int[picture.width()][picture.height()]; for (int y = 0; y < height(); y++) { for (int x = 0; x < width(); x++) { energy[x][y] = energy(x, y); } } }
/** * Transposes the picture <br> * IMPROVEMENT: transpose only the energy matrix when needed. Instead of transposing it back * again, check if the energy matrix was actually transposed */ private void transpose() { Picture transposedPicture = new Picture(picture.height(), picture.width()); double[][] newEnergy = new double[picture.height()][picture.width()]; for (int i = 0; i < picture.width(); i++) for (int k = 0; k < picture.height(); k++) { transposedPicture.set(k, i, picture.get(i, k)); newEnergy[k][i] = energy[i][k]; } energy = newEnergy; picture = transposedPicture; parent = new int[picture.width()][picture.height()]; }
/* Load an image from filename, and return it * as a 2D array of integers in row-major order * * and empty, 0x0 array is returned on errors */ public static int[][] imageData(String filename) { try { Picture p = new Picture(filename); int[][] img = new int[p.height()][p.width()]; for (int row = 0; row < p.height(); row++) { for (int col = 0; col < p.width(); col++) { img[row][col] = p.get(col, row).getRGB(); } } return img; } catch (RuntimeException e) { return new int[0][0]; } }
public static void main(String[] args) { Picture inputImg = new Picture(args[0]); System.out.printf("image is %d columns by %d rows\n", inputImg.width(), inputImg.height()); inputImg.show(); SeamCarver sc = new SeamCarver(inputImg); System.out.printf("Displaying horizontal seam calculated.\n"); showHorizontalSeam(sc); System.out.printf("Displaying vertical seam calculated.\n"); showVerticalSeam(sc); }
public SeamCarver(Picture picture) { // create a seam carver object based on the given picture int picWidth = picture.width(); int picHeight = picture.height(); currentPicture = picture; pixelColor = new Color[picWidth][picHeight]; for (int i = 0; i < picHeight; i++) { for (int j = 0; j < picWidth; j++) { pixelColor[j][i] = picture.get(j, i); } } }
public static void main(String[] args) { int[][] filter1 = { {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1} }; int[][] filter2 = { {1, 2, 1}, {0, 0, 0}, {-1, -2, -1} }; Picture pic0 = new Picture("traffic_signal.jpg"); int width = pic0.width(); int height = pic0.height(); Picture pic1 = new Picture(width, height); for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { // get 3-by-3 array of colors in neighborhood int[][] gray = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { gray[i][j] = (int) Luminance.lum(pic0.get(x - 1 + i, y - 1 + j)); } } // apply filter int gray1 = 0, gray2 = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { gray1 += gray[i][j] * filter1[i][j]; gray2 += gray[i][j] * filter2[i][j]; } } // int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2)); int magnitude = 255 - truncate((int) Math.sqrt(gray1 * gray1 + gray2 * gray2)); Color grayscale = new Color(magnitude, magnitude, magnitude); pic1.set(x, y, grayscale); } } pic0.show(); pic1.show(); // pic1.save("baboon-edge.jpg"); }
public static void main(String[] args) { Picture pic1 = new Picture(args[0]); int width = pic1.width(); int height = pic1.height(); pic1.show(); Picture pic2 = new Picture(width, height); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color c1 = pic1.get(x, y); Color c2 = c1.brighter(); pic2.set(x, y, c2); } } pic2.show(); }
private Picture initialImage() { pic2 = new Picture(500, 600); pic1 = new Picture(500, 600); for (int x = 0; x < pic2.width(); x++) for (int y = 0; y < pic2.height(); y++) { double dist = 1.0 - Math.sqrt((x - 300) * (x - 300) + (y - 200) * (y - 200)) / 500; int red = (int) (dist < 0.5 ? 0 : Math.min(Math.pow(dist, 0.4) + Math.pow(dist - 0.5, 0.1), 1.0) * 255); int green = (int) (dist * 255); int blue = 0; pic2.set(x, y, new Color(red, green, blue)); } return pic2; }
public void actionPerformed(ActionEvent e) { String cmd = (e.getActionCommand()); if (cmd.equals(aboutItem.getText())) JOptionPane.showMessageDialog( this, "Simple Image Program for DB2004\nversion 0.1\nThanks to BvS", "About imageLab", JOptionPane.INFORMATION_MESSAGE); else if (cmd.equals(quitItem.getText())) System.exit(0); else if (cmd.equals(openItem.getText())) { int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { pic2 = new Picture(chooser.getSelectedFile().getName()); pic1 = new Picture(pic2.width(), pic2.height()); lab.setIcon(pic2.getJLabel().getIcon()); sliderPanel.setVisible(false); pack(); repaint(); } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Could not open " + chooser.getSelectedFile().getName() + "\n" + ex.getMessage(), "Open Error", JOptionPane.INFORMATION_MESSAGE); } } } else if (cmd.equals(saveItem.getText())) { int returnVal = chooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { pic2.save(chooser.getSelectedFile().getName()); } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Could not write " + chooser.getSelectedFile().getName() + "\n" + ex.getMessage(), "Save Error", JOptionPane.INFORMATION_MESSAGE); } } } }
/** @return The width of the current picture */ public int width() { return picture.width(); }
public int width() { // width of current picture return currentPicture.width(); }
// picture width public int width() { return pic.width(); }