TOPICS
Search

Seam Carving


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 m-by-n image with n>1, a vertical seam chooses one pixel (r,c_r) in each row r=1,...,m with 1<=c_r<=n and |c_r-c_(r-1)|<=1 for r>=2. Given a cost e(r,c) at each pixel, the objective is to minimize sum_(r=1)^(m)e(r,c_r).

This is a weighted shortest path problem on an acyclic digraph. Each row is a layer, and an edge from a pixel in row r-1 to an allowed neighbor in row r 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 D(r,c) be the least cost of a partial seam ending at (r,c), with D(1,c)=e(1,c). For r>=2,

 D(r,c)=e(r,c)+min_(1<=j<=n; |j-c|<=1)D(r-1,j).

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 O(mn) 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).


See also

Acyclic Digraph, Dynamic Programming, Shortest Path Problem

Explore with Wolfram|Alpha

References

3Blue1Brown. "Seam Carving | Week 2, Lecture 7 | 18.S191 MIT Fall 2020" YouTube. Sep. 8, 2020. https://www.youtube.com/watch?v=rpB6zQNsbQU.Avidan, S. and Shamir, A. "Seam Carving for Content-Aware Image Resizing." ACM Trans. Graph. 26, Article 10, 2007. https://www.merl.com/publications/TR2007-087.

Cite this as:

Weisstein, Eric W. "Seam Carving." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/SeamCarving.html

Subject classifications