Resizing an image via traditional means usually changes or warps the features within the image, as every pixel is considered as important as every other, and the image is scaled accordingly. Retargeting with the seam carving algorithm, however, chooses a "seam" of pixels through an image such that it passes through the least "important" pixels first; removing this seam will result in a minimal importance loss, while making the image one pixel narrower or shorter. By continuing this process, we can make the image smaller while preserving the integrity of its original features.
Downsizing an image with this method is relatively straightforward, and is described in more detail below. For the purposes of this description, I'll only discuss making an image narrower, but making an image shorter (using horizontal seams) is as simple as transposing the image before processing, and then re-transposing the result.
gradient()
function (and summing the values for x and y over all three color channels), and this is indeed what I do in my implementation. These values could also be found by convolving a Laplacian filter with the image.Following are the results of my program as applied to the six test images, all resized to 200 pixels less than their original width, as well as two of my own photos resized in different ways. Two notable error cases are Mona Lisa's hair, and the bodies of the women in the second and last of the test images. These are examples of differing measures of importance: we are so accustomed to standard proportions of the human body that we implicitly assign these great importance, while the seam carving algorithm sees relatively untextured hair, legs that blend in with the background, or flat skin color as unimportant regions which should be removed.
One downfall of the seam-finding method displayed above is that straight lines are not always preserved, which can make retargeted images look especially unrealistic. The following three images feature many hard edges which the algorithm used above warps heavily, though by taking into account how removing a seam will contribute energy to a photo, we can try to avoid this. In simple terms, we just need to add the gradient difference created by removing a pixel to the cost of each seam going through that pixel, and this will favor seams which do not create sharp corners out of previously linear portions of the image, etc. As you can see in the following images, this adjustment does a lot to help preserve straight lines, or at least favor curved over jagged results. Also note the mug in the second image: it's shape is much better preserved using forward energy seam carving.
Enlarging photos is a similar task to making them smaller, except now it makes sense to duplicate seams in order of least importance, so that the least important areas of the image become larger, and don't disfigure the more important areas (which will ideally remain the same size). When implementing this, it turns out that I couldn't just order seams as they'd be found in the above algorithm, because seams ending on different pixels at the bottom of the image would often converge on the same unimportant areas closer to the top of the image (essentially using the same pixels in multiple seams). Thus, when calculating a set of seams for duplication, I assign extra cost to using duplicate pixels (pixels already included in seams calculated earlier), so that the algorithm is more likely to choose mostly independent seams. For many images, it turns out that the most unimportant seams are concentrated in one area, which can cause the sort of smearing you see a little in the first image below. However, for images such as the second and third where unimportant space isn't structurally or texturally important to the image, this procedure works very well.