Skip to content

Commit

Permalink
Done HW5
Browse files Browse the repository at this point in the history
  • Loading branch information
VidocqH committed Jan 5, 2021
1 parent ee25ce9 commit 8236bf0
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 46 deletions.
355 changes: 316 additions & 39 deletions hw5_release/hw5.ipynb

Large diffs are not rendered by default.

47 changes: 40 additions & 7 deletions hw5_release/seam_carving.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ def compute_cost(image, energy, axis=1):
paths[0] = 0 # we don't care about the first row of paths

### YOUR CODE HERE
pass
# MIT 18.S191 gives two for iterators solution
for i in range(1, H):
M1 = np.insert(cost[i-1, 0:W-1], 0, 1e10, axis=0)
M2 = cost[i-1, :]
M3 = np.insert(cost[i-1, 1:W], W-1, 1e10, axis=0)
M = np.r_[M1, M2, M3].reshape(3, -1)
cost[i] = energy[i] + np.min(M, axis=0)
paths[i] = np.argmin(M, axis=0) - 1
### END YOUR CODE

if axis == 0:
Expand Down Expand Up @@ -119,7 +126,10 @@ def backtrack_seam(paths, end):
seam[H-1] = end

### YOUR CODE HERE
pass
j = end
for i in range(H-2, -1, -1):
j = paths[i+1, j] + seam[i+1]
seam[i] = j
### END YOUR CODE

# Check that seam only contains values in [0, W-1]
Expand Down Expand Up @@ -149,7 +159,12 @@ def remove_seam(image, seam):
out = None
H, W, C = image.shape
### YOUR CODE HERE
pass
# out = np.zeros((H, W-1, C))
# for i in range(H):
# idx = seam[i]
# out[i] = np.concatenate((image[i, 0:idx], image[i, idx+1: W]), axis=None).reshape(W-1, -1)
out = image[np.arange(W) != seam[:, None]].reshape(H, W-1, C)

### END YOUR CODE
out = np.squeeze(out) # remove last dimension if C == 1

Expand Down Expand Up @@ -197,7 +212,12 @@ def reduce(image, size, axis=1, efunc=energy_function, cfunc=compute_cost, bfunc
assert size > 0, "Size must be greater than zero"

### YOUR CODE HERE
pass
while out.shape[1] > size:
energy = efunc(out)
cost, paths = cfunc(out, energy)
end = np.argmin(cost[-1])
seam = backtrack_seam(paths, end)
out = remove_seam(out, seam)
### END YOUR CODE

assert out.shape[1] == size, "Output doesn't have the right shape"
Expand All @@ -224,7 +244,11 @@ def duplicate_seam(image, seam):
H, W, C = image.shape
out = np.zeros((H, W + 1, C))
### YOUR CODE HERE
pass
out = np.zeros((H, W+1, C))
for i in range(H):
idx = seam[i]
out[i] = np.concatenate((image[i, 0:idx+1], image[i, idx: W]), axis=None).reshape(W+1, -1)
# out[i] = np.insert(image[i], seam[i], image[i, seam[i]], axis=0)
### END YOUR CODE

return out
Expand Down Expand Up @@ -265,7 +289,12 @@ def enlarge_naive(image, size, axis=1, efunc=energy_function, cfunc=compute_cost
assert size > W, "size must be greather than %d" % W

### YOUR CODE HERE
pass
while out.shape[1] < size:
energy = efunc(out)
cost, paths = cfunc(out, energy)
end = np.argmin(cost[-1])
seam = backtrack_seam(paths, end)
out = duplicate_seam(out, seam)
### END YOUR CODE

if axis == 0:
Expand Down Expand Up @@ -392,7 +421,11 @@ def enlarge(image, size, axis=1, efunc=energy_function, cfunc=compute_cost, dfun
assert size <= 2 * W, "size must be smaller than %d" % (2 * W)

### YOUR CODE HERE
pass
seams = find_seams(out, size - W)
seams = np.expand_dims(seams, axis=2)
for i in range(size - W):
out = duplicate_seam(out, np.where(seams == i+1)[1])
seams = duplicate_seam(seams, np.where(seams == i+1)[1])
### END YOUR CODE

if axis == 0:
Expand Down

0 comments on commit 8236bf0

Please sign in to comment.