コード例 #1
0
  /**
   * Removes one entire seam from the picture
   *
   * @param seam
   * @param vertical Defines if the seam is vertical or horizontal
   * @return A new picture with the seam removed
   */
  private Picture removeSeam(int[] seam, boolean vertical) {
    if (vertical) {
      Picture p = new Picture(width() - 1, height());
      for (int y = 0; y < height(); y++) {
        int k = 0;
        for (int x = 0; x < width(); x++) {
          if (x != seam[y]) {
            p.set(k, y, picture.get(x, y));
            k++;
          }
        }
      }
      return p;
    }

    Picture p = new Picture(width(), height() - 1);
    for (int y = 0; y < width(); y++) {
      int k = 0;
      for (int x = 0; x < height(); x++) {
        if (x != seam[y]) {
          p.set(y, k, picture.get(y, x));
          k++;
        }
      }
    }
    return p;
  }
コード例 #2
0
 /**
  * The energy of pixel (x, y) is Δx^2(x, y) + Δy^2(x, y)
  *
  * @param x The pixel at column x
  * @param y The pixel at column y
  * @return The energy of pixel at column x and row y in current picture
  */
 public double energy(int x, int y) {
   if (x < 0 || x > width() - 1 || y < 0 || y > height() - 1) {
     throw new IndexOutOfBoundsException();
   }
   if (x == 0 || x == width() - 1 || y == 0 || y == height() - 1) {
     return MAX_ENERGY;
   }
   double xDiff = gradient(picture.get(x - 1, y), picture.get(x + 1, y));
   double yDiff = gradient(picture.get(x, y - 1), picture.get(x, y + 1));
   return xDiff + yDiff;
 }
コード例 #3
0
 /**
  * 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()];
 }
コード例 #4
0
  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);
      }
    }
  }
コード例 #5
0
  public double energy(int x, int y) {
    // System.out.println("x coord:" + x + ", y coord:"+y);
    // System.out.println("width: "+width() + ", height: "+height());
    if (x < 0 || x > width() - 1 || y < 0 || y > height() - 1) {
      throw new IndexOutOfBoundsException(
          "pixel indices out of bounds. x: "
              + x
              + ", y: "
              + y
              + ":: width: "
              + width()
              + ", height: "
              + height());
    }

    if (x == 0 || x == width() - 1 || y == 0 || y == height() - 1) {
      return BORDER_ENERGY;
    }

    //  get the colors of the pixels adjacent to pixel (x,y)
    Color top = pic.get(x, y - 1);
    Color bottom = pic.get(x, y + 1);
    Color left = pic.get(x - 1, y);
    Color right = pic.get(x + 1, y);

    double gradient_x =
        Math.pow(left.getRed() - right.getRed(), 2)
            + Math.pow(left.getGreen() - right.getGreen(), 2)
            + Math.pow(left.getBlue() - right.getBlue(), 2);
    double gradient_y =
        Math.pow(top.getRed() - bottom.getRed(), 2)
            + Math.pow(top.getGreen() - bottom.getGreen(), 2)
            + Math.pow(top.getBlue() - bottom.getBlue(), 2);

    return gradient_x + gradient_y;
  }
コード例 #6
0
  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");
  }
コード例 #7
0
ファイル: ImageData.java プロジェクト: jschear/AnimotoCrop
  /* 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];
    }
  }
コード例 #8
0
ファイル: Brighter.java プロジェクト: sushilshah/learnJava
  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();
  }