src/io/github/marwlod/seam_carving/SeamCarver.java
// sequence of indices for vertical seam
sequence of indices for vertical seam
sequence of indices for vertical seam
public int[] findVerticalSeam() {
        int width = rgb.length;
        int height = rgb[0].length;
        double[][] energies = new double[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                energies[x][y] = energy(x, y);
            }
        }
        double[][] distTo = new double[width][height];
        for (int x = 0; x < width; x++) {
            for (int y = 1; y < height; y++) {
                distTo[x][y] = Integer.MAX_VALUE;
            }
        }
        int[][] pathTo = new int[width][height];
        for (int y = 0; y < height-1; y++) {
            for (int x = 0; x < width; x++) {
                double currMinEnergy = distTo[x][y] + energies[x][y];
                if (x-1 >= 0 && distTo[x-1][y+1] > currMinEnergy) {
                    distTo[x-1][y+1] = currMinEnergy;
                    pathTo[x-1][y+1] = x;
                }
                if (distTo[x][y+1] > currMinEnergy) {
                    distTo[x][y+1] = currMinEnergy;
                    pathTo[x][y+1] = x;
                }
                if (x+1 < width && distTo[x+1][y+1] > currMinEnergy) {
                    distTo[x+1][y+1] = currMinEnergy;
                    pathTo[x+1][y+1] = x;
                }
            }
        }
        double minEnergy = Double.POSITIVE_INFINITY;
        int minEnergyV = Integer.MAX_VALUE;
        for (int x = 0; x < width; x++) {
            if (minEnergy > distTo[x][height-1]) {
                minEnergy = distTo[x][height-1];
                minEnergyV = x;
            }
        }
        int[] verticalSeam = new int[height];
        verticalSeam[height-1] = minEnergyV;
        for (int i = height-2, y = height-1; i >= 0; i--, y--) {
            verticalSeam[i] = pathTo[verticalSeam[i+1]][y];
        }
        return verticalSeam;
    }
public int [ ] findVerticalSeam ( ) { int width = rgb . length ; int height = rgb [ 0 ] . length ; double [ ] [ ] energies = new double [ width ] [ height ] ; for ( int x = 0 ; x < width ; x ++ ) { for ( int y = 0 ; y < height ; y ++ ) { energies [ x ] [ y ] = energy ( x , y ) ; } } double [ ] [ ] distTo = new double [ width ] [ height ] ; for ( int x = 0 ; x < width ; x ++ ) { for ( int y = 1 ; y < height ; y ++ ) { distTo [ x ] [ y ] = Integer . MAX_VALUE ; } } int [ ] [ ] pathTo = new int [ width ] [ height ] ; for ( int y = 0 ; y < height - 1 ; y ++ ) { for ( int x = 0 ; x < width ; x ++ ) { double currMinEnergy = distTo [ x ] [ y ] + energies [ x ] [ y ] ; if ( x - 1 >= 0 && distTo [ x - 1 ] [ y + 1 ] > currMinEnergy ) { distTo [ x - 1 ] [ y + 1 ] = currMinEnergy ; pathTo [ x - 1 ] [ y + 1 ] = x ; } if ( distTo [ x ] [ y + 1 ] > currMinEnergy ) { distTo [ x ] [ y + 1 ] = currMinEnergy ; pathTo [ x ] [ y + 1 ] = x ; } if ( x + 1 < width && distTo [ x + 1 ] [ y + 1 ] > currMinEnergy ) { distTo [ x + 1 ] [ y + 1 ] = currMinEnergy ; pathTo [ x + 1 ] [ y + 1 ] = x ; } } } double minEnergy = Double . POSITIVE_INFINITY ; int minEnergyV = Integer . MAX_VALUE ; for ( int x = 0 ; x < width ; x ++ ) { if ( minEnergy > distTo [ x ] [ height - 1 ] ) { minEnergy = distTo [ x ] [ height - 1 ] ; minEnergyV = x ; } } int [ ] verticalSeam = new int [ height ] ; verticalSeam [ height - 1 ] = minEnergyV ; for ( int i = height - 2 , y = height - 1 ; i >= 0 ; i -- , y -- ) { verticalSeam [ i ] = pathTo [ verticalSeam [ i + 1 ] ] [ y ] ; } return verticalSeam ; }
sequence of indices for vertical seam
sequence of indices for vertical seam
// calculate energies for all the vertices (pixels) // all vertices start out with "infinity" distance from first row, except the first row with dist 0 // relax three vertices below this vertex (if they exist at all) so they all have minimal possible energy // find minimal total energy vertex at the bottom of the picture (last row) // vertical seam defined as array of (x,y) where x = verticalSeam[i], y = i and i = (0, 1, ..., height-1) // go from vertex with minimal total energy at the bottom back to the top and construct the path