Fluid Images 1
1 Introduction
Suppose that you are given an image and wish to shrink its width by a given number of pixels without changing its height. The simplest strategy is simply to scale the image, but this may introduce undesirable distortions. Another option is to crop the edges of the image, but this is unacceptable if the edges contain important information.
In this assignment, you will implement a more advanced algorithm to intelligently decide what parts of the image to remove.
2 Energy
Given an image we first compute the “energy” of each pixel, which measures how much that pixel stands out from its surroundings. This gives us a rough idea of its importance. In other words, an “unimportant” pixel blends in with its surroundings, and can thus be removed without distorting the image.
A B C |
D E F |
G H I |
use the formula
\[energy(E) = \sqrt{xenergy^2 + yenergy^2}\]
where
\[xenergy = a + 2d + g - c - 2f - i\]
\[yenergy = a + 2b + c - g - 2h - i\]
and each lowercase letter represents the brightness (sum of the red, blue, and green values) of the corresponding pixel.You are welcome to experiment with different energy functions, but the solution you hand in must use the formula above.
To compute the energy of edge pixels, you should pretend that the image is surrounded by a 1 pixel wide border of black pixels (with 0 brightness).
3 Seams
A vertical seam is a set of pixels, one on each row of the image, that are “connected” vertically. That is, any two pixels on adjacent rows are either vertically or diagonally adjacent. Note that this means seams are not always vertical lines. The energy of a seam is the sum of the energies of the pixels in the seam.
We will shrink the width of the image by removing vertical seams from the picture. Our algorithm consists of repeatedly finding the lowest-energy vertical seam and removing it from the image. Sometimes multiple seams may tie for the lowest energy. If this happens, remove the leftmost of those seams. The leftmost seam refers to the seam with the leftmost pixel in the top row. Note that once a seam has been removed, the image has changed, so the energies of the remaining pixels must be recomputed.Think about why we remove lowest-energy seams rather than simply removing the lowest-energy pixel from each row. Better still, try it out, and you’ll see the difference for yourself! But this isn’t part of the assignment.
4 Support Code
You will be able to access the following types and functions:
data Image
An image object. Has the properties width and height, which specify the dimensions of the image in pixels, and the property img, which returns the underlying image itself. The image object preserves only the red, green, and blue channels, ignoring the alpha channel (corresponding to transparency).
data Color:
| color(red :: Number, green :: Number, blue :: Number)
end
Images can be thought of as 2-dimensional lists of colors. The representation of color that you will be using for this assignment is defined above.
fun image-from-url(url :: String) -> Image
Given a string indicating a URL, produces the image at that url.
fun image-to-2d-color-list(image :: Image) -> List<List<Color>>
Given an image, returns a list of lists containing the Color corresponding to each pixel in the image. The output is in row-major order, so the first sublist corresponds to the top row of the image.
fun image-from-2d-color-list(image :: List<List<Color>>) -> Image
Consumes a two-dimensional list of Colors in row-major order (as the output of image-to-2d-color-list) and returns an image object where each pixel’s color corresponds to that location in the two-dimensional list. Note that when testing and using this function at the interaction prompt, it is best to bind the results to an identifier and access the fields individually. The image object will unfortunately not necessarily print properly in the prompt (sorry!).
5 The Assignment
For this assignment, implement the following function:
fun liquify(image-object :: Image, n :: Number)
that carves n seams from the given image.
Your solution must use memoization and should be reasonably efficient: it should not take exponential time! If you cannot liquify 10 seams in a few minutes from the test images we provide, you should rethink your implementation. You do not need to be this fast to receive full credit, but it is a good benchmark.
6 Built-Ins
You should be able to do this assignment with no use of mutation beyond that needed for memoization. Arrays may only be used as part of memoization.
7 Test Images
All images © Shriram.
8 Analysis
Give a big-O analysis of your solution. First find an upper bound on the total number of possible vertical seams for an image in terms of its width and height. Use this bound to find the time complexity of the naive solution, which computes the energy for every possible seam, and of your chosen solution.
In your analysis, you may assume that image-to-2d-color-list and image-from-2d-color-list are both, for an image with width \(w\) and height \(h\), in \(O([w, h \rightarrow wh])\).