Skip to content

Commit

Permalink
Merge pull request StanfordVL#18 from StanfordVL/mv_publish_hw4
Browse files Browse the repository at this point in the history
Prevent cascading errors
  • Loading branch information
maximevo authored Oct 22, 2019
2 parents b5f1019 + f8f0b68 commit b0c1ab2
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions hw4_release/seam_carving.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,21 @@ def reduce(image, size, axis=1, efunc=energy_function, cfunc=compute_cost, bfunc
At each step, we remove the lowest energy seam from the image. We repeat the process
until we obtain an output of desired size.
Use functions:
- efunc
- cfunc
- backtrack_seam
- remove_seam
SUPER IMPORTANT: IF YOU WANT TO PREVENT CASCADING ERRORS IN THE CODE OF reduce(), USE FUNCTIONS:
- efunc (instead of energy_function)
- cfunc (instead of compute_cost)
- bfunc (instead of backtrack_seam)
- rfunc (instead of remove_seam)
Args:
image: numpy array of shape (H, W, 3)
size: size to reduce height or width to (depending on axis)
axis: reduce in width (axis=1) or height (axis=0)
efunc: energy function to use
cfunc: cost function to use
bfunc: backtrack seam function to use
rfunc: remove seam function to use
Returns:
out: numpy array of shape (size, W, 3) if axis=0, or (H, size, 3) if axis=1
Expand Down Expand Up @@ -228,18 +231,21 @@ def enlarge_naive(image, size, axis=1, efunc=energy_function, cfunc=compute_cost
At each step, we duplicate the lowest energy seam from the image. We repeat the process
until we obtain an output of desired size.
Use functions:
- efunc
- cfunc
- backtrack_seam
- duplicate_seam
SUPER IMPORTANT: IF YOU WANT TO PREVENT CASCADING ERRORS IN THE CODE OF enlarge_naive(), USE FUNCTIONS:
- efunc (instead of energy_function)
- cfunc (instead of compute_cost)
- bfunc (instead of backtrack_seam)
- dfunc (instead of duplicate_seam)
Args:
image: numpy array of shape (H, W, C)
size: size to increase height or width to (depending on axis)
axis: increase in width (axis=1) or height (axis=0)
efunc: energy function to use
cfunc: cost function to use
bfunc: backtrack seam function to use
dfunc: duplicate seam function to use
Returns:
out: numpy array of shape (size, W, C) if axis=0, or (H, size, C) if axis=1
Expand All @@ -264,7 +270,7 @@ def enlarge_naive(image, size, axis=1, efunc=energy_function, cfunc=compute_cost
return out


def find_seams(image, k, axis=1, efunc=energy_function, cfunc=compute_cost):
def find_seams(image, k, axis=1, efunc=energy_function, cfunc=compute_cost, bfunc=backtrack_seam, rfunc=remove_seam):
"""Find the top k seams (with lowest energy) in the image.
We act like if we remove k seams from the image iteratively, but we need to store their
Expand All @@ -274,18 +280,20 @@ def find_seams(image, k, axis=1, efunc=energy_function, cfunc=compute_cost):
is the output of find_seams.
We also keep an indices array to map current pixels to their original position in the image.
Use functions:
- efunc
- cfunc
- backtrack_seam
- remove_seam
SUPER IMPORTANT: IF YOU WANT TO PREVENT CASCADING ERRORS IN THE CODE OF find_seams(), USE FUNCTIONS:
- efunc (instead of energy_function)
- cfunc (instead of compute_cost)
- bfunc (instead of backtrack_seam)
- rfunc (instead of remove_seam)
Args:
image: numpy array of shape (H, W, C)
k: number of seams to find
axis: find seams in width (axis=1) or height (axis=0)
efunc: energy function to use
cfunc: cost function to use
bfunc: backtrack seam function to use
rfunc: remove seam function to use
Returns:
seams: numpy array of shape (H, W)
Expand Down Expand Up @@ -320,10 +328,10 @@ def find_seams(image, k, axis=1, efunc=energy_function, cfunc=compute_cost):
energy = efunc(image)
cost, paths = cfunc(image, energy)
end = np.argmin(cost[H - 1])
seam = backtrack_seam(paths, end)
seam = bfunc(paths, end)

# Remove that seam from the image
image = remove_seam(image, seam)
image = rfunc(image, seam)

# Store the new seam with value i+1 in the image
# We can assert here that we are only writing on zeros (not overwriting existing seams)
Expand All @@ -332,30 +340,37 @@ def find_seams(image, k, axis=1, efunc=energy_function, cfunc=compute_cost):
seams[np.arange(H), indices[np.arange(H), seam]] = i + 1

# We remove the indices used by the seam, so that `indices` keep the same shape as `image`
indices = remove_seam(indices, seam)
indices = rfunc(indices, seam)

if axis == 0:
seams = np.transpose(seams, (1, 0))

return seams


def enlarge(image, size, axis=1, efunc=energy_function, cfunc=compute_cost):
def enlarge(image, size, axis=1, efunc=energy_function, cfunc=compute_cost, dfunc=duplicate_seam, bfunc=backtrack_seam, rfunc=remove_seam):
"""Enlarges the size of the image by duplicating the low energy seams.
We start by getting the k seams to duplicate through function find_seams.
We iterate through these seams and duplicate each one iteratively.
Use functions:
SUPER IMPORTANT: IF YOU WANT TO PREVENT CASCADING ERRORS IN THE CODE OF enlarge(), USE FUNCTIONS:
- efunc (instead of energy_function)
- cfunc (instead of compute_cost)
- dfunc (instead of duplicate_seam)
- bfunc (instead of backtrack_seam)
- rfunc (instead of remove_seam)
- find_seams
- duplicate_seam
Args:
image: numpy array of shape (H, W, C)
size: size to reduce height or width to (depending on axis)
axis: enlarge in width (axis=1) or height (axis=0)
efunc: energy function to use
cfunc: cost function to use
dfunc: duplicate seam function to use
bfunc: backtrack seam function to use
rfunc: remove seam function to use
Returns:
out: numpy array of shape (size, W, C) if axis=0, or (H, size, C) if axis=1
Expand Down

0 comments on commit b0c1ab2

Please sign in to comment.