On this page:
1 Introduction
2 Energy
3 Seams
4 Support Code
5 The Assignment
6 Built-Ins
7 Test Images
8 Analysis
9 Template Files
10 Handing In

Fluid Images

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.

Each pixel is represented as a Color, which is defined by its red, blue, and green values. To compute the energy of a pixel E surrounded by pixels A through I, arranged as follows,

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:

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 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 minimal or no use of mutation (certainly no more than memoization uses). In addition, 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, for an image with width \(w\) and height \(h\), \(O([w, h \rightarrow wh])\).

9 Template Files

Final Tests

Implementation Code

10 Handing In

Remember that this assignment requires you to hand in sweeps. In these sweeps, you should create custom images by defining them in terms of pixels, then testing the results of carving seams from these images.

Like the last assignment, you will be submitting a separate test file.

To submit, return to the Captain Teach assignments page:

https://www.captain-teach.org/brown-cs019/assignments/

and click “Next Step” again. Upload a zip file containing your implementation, named fluid-images.arr, and your final-tests.arr. Then on the next step upload your analysis.