← Discover
ASTERXOR / Discover

An image that loses width without simply shrinking

Removing selected paths of pixels can preserve a subject, or quietly damage what the algorithm fails to value.

A synthetic circle and vertical bar beside a thin highlighted removal seam.
Original generated artwork and one computed vertical seam, with no personal photographs.
DiscoverASTERXOR / LEON LEUNG

Can a picture become narrower without squeezing everything inside it?

Ordinary resizing changes the spacing of the whole image. Cropping discards a region near an edge. Seam carving takes a third route: remove a connected path containing one pixel in every row, then close the gap. Repeating the operation changes width selectively. The interesting question is how the algorithm decides which pixels the picture can afford to lose.

Low numerical cost is not the same as low importance.
THE COST MAP

Give every pixel a cost

Our synthetic image is 240 pixels wide and 150 high. It contains a gold circle and a blue vertical bar on a gently changing dark background. To score pixels, the script first takes the arithmetic mean of their three colour channels, then adds the absolute horizontal and vertical gradients of that scalar image.

This score is often called energy, but it is not physical energy or a calibrated luminance measurement. It is a local contrast proxy. Strong boundaries receive higher costs; nearly uniform areas receive lower costs. The name should not conceal the choice: the algorithm is being asked to avoid changes in this particular numerical field, not to understand the depicted objects.

The channel average has a specific colour blind spot. A pure red pixel with channels (1, 0, 0) and a pure green pixel (0, 1, 0) both become one third. An edge between those two colours can disappear completely from this scalar score even though the original colours differ strongly. This does not invalidate the arithmetic; it identifies information thrown away before the path search begins. Changing the search algorithm alone cannot recover an edge missing from its input costs.

A minimum-cost vertical seam and the gradient-based cost map of the same generated image.
FIG 01A minimum-cost vertical seam and the gradient-based cost map of the same generated image.
A CONNECTED PATH

Find a connected route through the costs

A vertical seam contains exactly one pixel per row. Between consecutive rows, it can move left by one column, stay in the same column or move right by one. Those constraints make the path connected while allowing it to bend around expensive regions instead of behaving like a rigid column cut.

Dynamic programming finds the minimum-total-cost seam for the current energy image. At each pixel, it adds the local cost to the smallest accumulated cost among its valid predecessors in the row above. Backtracking from the cheapest final-row endpoint recovers a complete path. A separate tiny test compares this answer with exhaustive enumeration of all allowed paths.

Original, uniform resize and 70-seam removal; the carved fixture retains all 2,809 circle pixels.
FIG 02Original, uniform resize and 70-seam removal; the carved fixture retains all 2,809 circle pixels.
REPEAT THE DECISION

Removing a seam changes the next problem

After deleting the chosen pixels, the remaining pixels in each row close the gap. The width decreases by one while height stays fixed. We recompute the gradient field and find another seam on the changed image, repeating until 70 seams have been removed. The final dimensions are 170 by 150.

Each individual seam is optimal for the current cost field under the stated rule. That does not prove that the entire sequence of 70 removals is globally optimal for every possible final image. Earlier choices change later neighbours and costs. This simple implementation is a useful demonstration of repeated local decisions, not a claim to reproduce every modern content-aware resizing technique.

Closing a gap also creates new neighbours. Two pixels that were separated by the deleted seam become adjacent, potentially making a conspicuous new edge. The backward-energy score used here looks at contrast in the current image before removal; it does not explicitly charge for all such newly created relationships. Recomputing afterward is useful, but it does not undo a damaging choice already made. This is why evaluating the edited image matters in addition to checking that the chosen path had a low cost.

A flat foreground loses 6,450 pixels under the same contrast-based rule after 90 removals.
FIG 03A flat foreground loses 6,450 pixels under the same contrast-based rule after 90 removals.
A FAVOURABLE CASE

The favourable example keeps the circle

In this particular fixture, all 2,809 pixels identified as belonging to the gold circle remain after carving. The background absorbs the removals, and the circle retains its pixel shape. A conventional resize to the same output dimensions instead compresses the circle horizontally, making it narrower relative to its height.

That success is deliberately limited to the constructed picture. The objects have strong edges and plenty of low-cost space around them. The algorithm was not tested on faces, lettering, architecture or natural photography. Even in the successful case, spacing and composition change, so preserving an object’s pixels does not mean preserving the whole meaning of the image.

THE FAILURE CASE

A flat subject exposes the blind spot

The failure fixture replaces the scene with a broad gold foreground region and subtle repeated stripes. Its interior can be cheap under the gradient score even though it is visually part of the main subject. After 90 seam removals, the foreground pixel count falls from 21,000 to 14,550, visibly narrowing the region.

The algorithm has followed its instructions. It has no semantic reason to protect a smooth face, a plain sign or the quiet centre of a graphic. Protection masks, different costs and methods that consider newly created edges can improve particular cases, but they introduce additional judgments. Automatic resizing still requires checking the resulting relationships and shapes.

Protection can be expressed as an extra penalty on pixels the user wants to keep. That makes the objective more informed, not infallible: a finite penalty can still be outweighed by other costs, and a completely forbidden region can leave no connected route. A protected subject may also keep its pixels while surrounding spacing changes. It helps to specify both the object that must survive and the visual relationships that must remain believable, then inspect the actual output at a common display scale.

WHAT IS VALUABLE

The objective is part of the image edit

A seam overlay is useful because it makes a hidden choice visible. Before accepting the narrower picture, inspect what was removed, which lines bent and whether familiar proportions changed. A small numerical cost can hide a large visual or factual consequence, especially when an image is used as evidence rather than decoration.

Seam carving is compelling because it shows that image dimensions are not the only thing an edit can optimize. It also shows why the objective must be chosen carefully. A picture can become smaller in a clever way while becoming less faithful to what the viewer needed it to preserve.

A SMALL SEARCH YOU CAN CHECK

Why the cheapest next pixel is not enough

The added four-row, five-column grid is a small cost map that can be checked by hand. A greedy walk starts at the cheapest top-row cell and repeatedly chooses the cheapest available neighbour below. With ties resolved toward the left, it stays in column one and pays 1 + 1 + 9 + 9 = 20. Starting with a slightly more expensive cell can be better: columns 2, 3, 3, 3 cost 2 + 2 + 1 + 1 = 6.

Dynamic programming retains the best cost of reaching every candidate, rather than committing to the cheapest visible next step. In the accumulated-cost panel, each number includes all earlier rows on its best route. The bottom-row minimum is six, and backtracking finds the better path. Exhaustively checking all 95 admissible connected paths confirms that it is uniquely optimal for this grid. This small calculation makes the distinction between an optimal current seam and a greedy pixel-by-pixel walk concrete.

Add a protection penalty of 100 to the cell in row three, column three. The best path changes to columns 1, 1, 2, 3 and costs 12, avoiding that cell; exhaustive enumeration confirms the revised answer too. The extra penalty is an explicit preference, not something the algorithm learned about the picture. This toy example removes no real image pixels. It demonstrates how changing the objective changes the solution, while the full image examples retain their original unprotected contrast score.

Two numbered 4×5 grids show local and accumulated costs. Gold traces the optimal path and dashed blue traces the greedy path.
FIG 04A hand-checkable cost grid compares a greedy path costing 20 with the dynamic-programming optimum costing 6; the second panel contains accumulated costs, not a second image.
THE THOUGHT TO TAKE AWAY

The algorithm can find the cheapest path. Deciding what should remain valuable is a different task.