Seam carving is an algorithm for resizing an image by removing or inserting low-cost pixel paths called seams (Avidan and Shamir 2007).
To reduce the width of an -by-
image with
,
a vertical seam chooses one pixel
in each row
with
and
for
. Given a cost
at each pixel, the objective is to minimize
.
This is a weighted shortest path problem on an acyclic digraph. Each row is a layer, and
an edge from a pixel in row to an allowed neighbor in row
carries the cost of the destination pixel. A source connects
to the first row with the first-row costs, and the last-row pixels connect to a sink
at zero cost.
Let be the least cost of a partial
seam ending at
, with
. For
,
The recurrence follows because the final step must come from one of the listed predecessors. Evaluating it row by row is dynamic programming.
A minimizing column in the last row has the optimal seam cost, and following the
stored minimizing predecessors recovers a seam. Each pixel examines at most three
predecessors, so finding a seam takes time (3Blue1Brown 2020). Removing its pixels shortens
each row by one. Further removals operate on the resized image, with costs recomputed
as needed. Interchanging rows and columns gives the horizontal version (Avidan and
Shamir 2007).