Fluid Images
1 Introduction
2 Energy
3 Seams
4 Support Code
5 The Assignment
6 Test Images
7 Analysis
8 Template Files
9 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.

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.

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).

You are welcome to experiment with different energy functions, but the solution you hand in must use the formula above.

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. 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 energies 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!

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.

You should be able to do this assignment with minimal or no use of mutation. In particular, can you do it with no more mutation than memoization offers?

6 Test Images

All images © Shriram.

7 Analysis

Give a big-O analysis of your solution. 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.

8 Template Files

Final Tests

Implementation Code

9 Handing In

Remember that this assignment requires you to hand in sweeps.

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.