Sketch · 2026-11-18
Seam carving, one cheap path at a time.
Scaling an image squeezes everything equally. Cropping throws away the edges whatever they hold. Seam carving looks for a connected path of pixels, top to bottom, that nobody would miss, removes it, and repeats. Drag the width slider below and compare the three.
- width
- —
- last seam cost
- —
- cheapest / dearest so far
- —
- erase marks left
- —
The test image is drawn by code on load. Uploaded images are reduced to at most 360 by 260 pixels and never leave your browser.
Note
A vertical seam is one pixel per row, and each pixel touches the one above it or one of its two diagonal neighbours. Give every pixel an energy, and the cheapest seam falls out of a single pass of dynamic programming. Row by row, the cost to reach a pixel is its own energy plus the smallest of the three costs above it. The minimum on the bottom row is where the best seam ends, and following the choices back up recovers it. That is one pass over the image per seam, so removing k columns from a W×H image costs k·W·H steps. Here that is under twenty million and finishes in a fraction of a second. The animation is slowed down so you can see it.
Avidan and Shamir's 2007 paper used the gradient magnitude as the energy: flat sky is cheap, edges are dear. This is called backward energy because it scores what is taken out. It ignores what the removal creates. When two pixels on either side of a seam become neighbours, they can form an edge that was not there before. In 2008 Rubinstein, Shamir and Avidan proposed forward energy, which charges each step of the seam for the new differences it would introduce between the pixels that end up touching. Narrow the test image by half under each setting and look at the slope of the hill and the mast of the boat.
The slider is instant because the carving only runs once. Each pixel is stamped with the step at which it was removed, and any width is rendered by keeping the pixels whose stamp is at least the number of columns removed. The original paper calls this a multi-size image and stores the same map. The “Removed pixels” view draws those stamps on the source: early seams in yellow, late seams in blue.
The chart shows what each seam cost. On the test image it climbs by about half under backward energy and nearly doubles under forward energy by the time sixty percent of the columns are gone. The cheap seams go first, through the flat sky and the grain of the sea, and every later seam has less empty space to choose from. On a photograph with a busy background the curve starts high and stays there, and the carved result looks no better than a plain scale. The method is greedy as well: each seam is the best single seam given the ones already removed, which is not the best set of k seams.
The brush adds a large constant to the energy of marked pixels. Protect pushes seams around a region. Erase subtracts the same amount, so seams prefer to pass through it, and narrowing until no marks are left removes the object. The paper then inserts seams to get back to the original width. This page stops at the removal.
Sources
- Avidan, S., Shamir, A. (2007). Seam carving for content-aware image resizing. ACM Transactions on Graphics, 26(3), article 10 (SIGGRAPH 2007).
- Rubinstein, M., Shamir, A., Avidan, S. (2008). Improved seam carving for video retargeting. ACM Transactions on Graphics, 27(3), article 16 (SIGGRAPH 2008).