Data Structures and Algorithms

Suppose that we 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 — seam carving — to intelligently decide what parts of the image to remove.

Given an image, we first compute the "energy" of each pixel, which measures how much that pixel stands out from its surroundings and gives us a rough idea of its importance.

To compute the energy of a pixel `E` surrounded by pixels
`A` through `I`,

ABCDEFGHI

use the formula

energy(E) = sqrt(xenergy^{2}+yenergy^{2})

where

xenergy=a+ 2d+g-c- 2f-iyenergy=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.

We will shrink the width of the image by removing vertical seams from the picture. A vertical seam is a set of pixels, one on each row of the image, which are connected vertically. That is, any two pixels on adjacent rows are either orthogonally or diagonally adjacent. The energy of a seam is the sum of the energies of the pixels in the seam.

Seam carving is the process 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!

Implement the following function:

`(seam-carve (img : Image) (n : number)) : Image`

Given an image, shrink the image's width by removing the number of vertical seams specified by the second argument.

Your solution should be reasonably efficient (do not use an exponential-time algorithm). If you cannot carve 10 seams in a few minutes from the test images we provide, you should rethink your implementation. Bear in mind that our fastest implementation carves seams at a rate of roughly one per second from those images. 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 use of mutation. In particular, can you do it with no more mutation than memoization offers?

You will need to require `plai-seam-carving-support.rkt`

for this assignment. By doing so, you will be able to use:

`(image->2d-color-list (img : Image)) : (listof (listof 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.`(2d-color-list->Image (listof (listof color))) : Image`

Consumes a two-dimensional list of colors in row-major order (as the output of`image->2d-color-list`

) and returns an image where each pixel's color corresponds to that location in the two-dimensional list.

In addition, you will want to require the following functions from 2htdp/image:

(color : (number number number -> Color)) (color-red : (Color -> number)) (color-blue : (Color -> number)) (color-green : (Color -> number)) (bitmap/url : (string -> Image))

`test`

to check your images, so you'll want to test via converting the images to color lists first.
We provide several images to test on (see below), but you should also use some of your own images. The bigger the image the slower your tests will run, but you should definitely run at least a couple larger tests.

bangalore-dancers.jpg

belur-carving.jpg

british-museum.jpg

edinburgh-grave.jpg

las-fallas-1.jpg

mainz-chagall-fenster.jpg

trafalgar-square.jpg

uetliberg-tower.jpg

uluru.jpg

valencia-ciencia.jpg

All images © Shriram.

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.

Implement a second different solution of reasonable efficiency, then use one as an oracle for testing the other. As always, this oracle will put your fears about your code at bay, as the chances of making the same mistake on both solutions is low. Keep in mind that this solution can be less efficient, but don't make it too naive!

- A Racket file,
`seam-carving.rkt`

, containing your first solution. - A Racket file,
`oracle.rkt`

, containing a second different solution to the problem and your oracle for testing. - A file,
`readme.pdf`

, containing short descriptions of your seam carving algorithm and the optimizations you used. Give a brief example of each optimization showing how it helped. You should also include a brief description of your second algorithm and a short explanation of your oracle, and your answers for Part 3.

Your analysis must be in a `pdf`

file on Letter-sized paper. Mathematical content
must be formatted appropriately. Please, no Arial.