Simply scaling an image down will blur important areas just as much as unimportant areas (e.g. a huge sky background with little data will be blurred just the same as someone's face in the foreground). The seam carving algorithm is a "content aware image resizer"; that is, it tries to do a better job than just uniform scale by choosing to erase areas of the image that are "less noticeable." It removes a "seam" from an image, which is a line of connected pixels from top to bottom or left to right whsoe presence is the least noticeable if removed. This "least noticeable" effect is determined by applying an objective function to every possible seam. The choice of objective function in this assignment is the absolute sum of both gradient components. Minimizing this ensures that regions with the least energy/detail get removed first.
The first step in the algorithm is to calculate the gradient of the image. This is done with a gaussian just as in the canny edge detector in the previous assignment. Then, an "energy image" (the objective function at each pixel) is created by taking abs(Fx) + abs(Fy). This image is then normalized.
The next step of the algorithm is to take this image and to find the path of minimum cost in the energy image either from top to bottom or from left to right (the seam). Each pixel in this path must be connected to a pixel either directly below it, directly below and to the left, or directly below and to the right. This means a method of dynamic programming can be used instead of BFS or DFS. That is, for each pixel in each row, the min cost path to get to that pixel is that pixel's weight plus the minimum of the three pixels above it. Back tracing can then be done starting at the minimum weight pixel at the bottom, to determine what the seam path was.
I first wrote the code to calculate a vertical seam with dynamic programming. After this, all I had to do to find horizontal seams was to send the transpose of my image to the vertical seam function.
When retargeting an image, I calculate the min horizontal seam AND the min vertical seam at each step, and I compare their weights. I always remove the min cost seam between the two so that the most energy is preserved
NOTE ALSO: The seam calculation process is extremely expensive in matlab since it requires making nested for loops (matlab doesn't deal well with for loops in general). On larger images, it can take over a second per step since it has to calculate both the vertical and horizontal seams.
Once the min seam has been found, the final step is to remove it. This requires shifting all pixels to the right of it over one to the left, and then deleting the final row. I do this to the grayscale image and the color image in parallel
First, here is an example of the energy image and the minimum vertical and horizontal seams for an image:
Original Image (Dimension 640x480,)
Objective energy function
Here is an example of the image sized by 25% down in both the horizontal and vertical directions
Sized down to 75% horizontally (new resolution 480x480). Note how well all of the important features and sizes are preserved. My face has barely changed in size, and all of the pictures in the background are still in tact, since the seams mostly only removed sections of the couch and the blank wall in the background that are unimportant.
Sized down to 75% in vertical direction (new resolution 640 x 360). NOTE that my hair is a bit distorted here since there are many fewer horizontal seams than vertical seams (so distortion is inevitable). But the sizes of all of the rest of the features turned out well.
Here is another example:
Original image (672x420)
Retargeted image (500x400)
NOTE: The seam carving techique did a great job at retaining the important parts of the image here; notice how the items on the desk (the computer, keyboard, mouse, and clipboard) have moved closer together, but that they have barely changed in size. Also note how the specks on the wall have moved closer together as a result of moving minimum cost seams, and how the calendar has been preserved between the two images.
Here is another example:
Original image (615x372)
Retargeted image (450x350)
I was starting to really push the boundaries here, but it still worked without too many artifacts
One last example:
Original image (576x432)
Retargeted image (400x400)
This one worked extremely well, and it's perhaps one of the best. In general, it seems that the nature scenese work out the best since there is so much detail to begin with, moving seams in areas of less detail is that much less noticeable in the overall effect.
One trick that can be played with seam carving is to intentionally zero regions of the objective energy function so certain regions of the image get removed first. I extended my seam carver so that the user can supply an extra black/white image, where black regions will be marked to zero in the energy image. Here are a few examples with this extension:
House marked for removal
Result of resizing
This result shows one of the dangers of using this trick. Note that since there is a lot of variation in the clouds, a noticeable artifact has been introduced there (not the case with the sand since it is more homogenous throughout the image). If there are regions of high variation like that and the objective energy function is tweaked, then the chances of distorting the image will increase.
Here is a slightly better result:
Person marked for removal
Result of resizing
This example worked much better, since the sky, the water, and the sand are all very homogeneous and removing seams that go through the child standing there is much less noticeable.
Calculates the objective function for a grayscale image, using the gradient method discussed above
Finds the minimum cost vertical seam using dynamic programming
Finds the minimum cost horizontal seam by calling the vertical seam function with the transpose of the grayscale image
Marks a horizontal or vertical seam in red in the color image
Removes a horizontal seam from an image and shifts all of the pixels below it up, then removes the last row
Removes a vertical seam from an image and shifts all of the pixels to the right of it to the left, then removes the last column
Takes a filename for the image, a filename for the regions to be removed (the extra step I did), and a target number of rows and columns. This will resize the image to the target size by removing horizontal and vertical seams one by one
An overloaded version of the seam carver that doesn't take a remove image.