-code is given, except I need 2 functions to be filled in resizeable_image.py (in 2 formats)
-so 2 resizeable_image.py should be submitted, with one in each format.
your code must be written in TWO FORMATS: both with and without dynamic programming. The sizes of inputs for the naive algorithm will by necessity be much smaller than for dynamic programming.
You are given an image, and your task is to calculate the best vertical seam to remove. A vertical seam is a connected path of pixels, with one pixel per row of the image. Two pixels are connected if they are vertically or diagonally adjacent.
The best vertical seam is the one that minimizes the total energy of pixels in the seam.
Remember, it is a convention in most computer graphics systems that (0, 0) is the upper-left pixel.
How might we compute the lowest-energy seam? Simply compute all seams and
choose the minimum.
You will need to implement a naive algorithm for benchmarking purposes, so think about this: for every pixel in the top row, how would you enumerate all seams starting from it? If you then built up a collection of (seam, energy) tuples, you could easily choose the one with the smallest energy
This will be a Dynamic Programming algorithm
Here is the algorithm from the paper:
Subproblems
For each pixel (i, j), what is the lower-energy seam that starts at the top row of the image, but ends at (i, j)? image, but ends at (i, j)?
Relation
Let dp[i,j] be the solution to subproblem (i, j). Then dp[i,j] = min(dp[i,j-1], dp[i-1,j-1], dp[i+1,j-1]) + energy(i,j)
You will need to install the Python Pillow library
In resizable_image.py, implement a function best_seam(self, dp=True) that returns a list of coordinates corresponding to the lowest-energy vertical seam to remove, e.g. [(5, 0), (5, 1), (4, 2), (5, 3), (6, 4)]. The class ResizeableImage inherits from ImageMatrix. You should use the
following components of ImageMatrix in your program:
self.energy(i,j) returns the energy of a pixel. This takes O(1) time, but the constant factor is large. If you call energy more than once on the same pixel, you should cache the result. This memoization is separate from the dynamic programming memoization (there are no subproblems
involved) and so you should still cache the energy for a pixel even if dp == False.
self.width and self.height are the width and height of the image.
Your implementation may be either bottom-up or top-down. But either way, it must respect the argument dp, which indicates whether or not dynamic programming should be used. If dp == True, then you should either use memoization
or store the subproblem values in a table for re-use. If dp == False, then you should use a completely naive solution, probably recursive, that computes all possible seams independently.
test your code using test_resizable_image.py
Do not change any other code except within resizeable_image.py
Since the default behavior of best_seam() is to perform dynamic programming, the test suite provided will only test that variant.
However, for small inputs, turning off dynamic programming should be possible.
your code must be written in TWO FORMATS: both with and without dynamic programming. The sizes of inputs for the naive algorithm will by necessity be much smaller than for dynamic programming.
The naive solution can (probably should) be recursive. (It will only work for very, very small inputs. Youll see why once you solve the recurrence relations.)
-Your Dynamic Programming solution should probably not be recursive (because python does
not support efficient or deep recursion).