GPU Seam Carving

GitHub Repository

GPU Seam Carving

Content-aware image resizing in CUDA. Seam carving shrinks an image by repeatedly removing vertical seams, connected top-to-bottom paths of low-energy pixels, so flat regions disappear before important detail does.

Overview

This project implements content-aware image resizing using the seam carving algorithm on NVIDIA GPUs. The entire carve pipeline runs on the GPU with no per-seam copy back to the host. Each algorithmic stage has several kernel variants, all validated against a CPU reference and profiled with Nsight Compute.

Result

BeforeAfter
OriginalCarved

How It Works

The algorithm runs in four stages, all on-device:

  1. Energy — Per-pixel gradient magnitude. High where color changes sharply (edges, detail), low in smooth regions.
  2. Cumulative Energy — Dynamic programming from top to bottom: each cell is its own energy plus the cheapest of its three parents in the row above.
  3. Backtrack — Start at the minimum of the bottom row and walk up, picking the cheapest parent each step, to recover the seam.
  4. Removal — Shift every row left past the removed column, reducing width by 1.

Implementation Highlights

  • Multiple kernel variants for each stage to compare approaches
  • Grid-stride loops for work distribution
  • Cooperative kernels using grid.sync() for efficient row-wise barriers in DP
  • Validated against CPU reference — energy, cumulative maps, seam, and output image all compared
  • Profiled with Nsight Compute — memory throughput and compute metrics analyzed

Performance Results

Benchmarks run on RGBA input with Nsight Compute profiling (% of hardware peak):

KernelDurationMemory %Compute %Registers
energy v10.04 ms76.176.137
energy v20.03 ms77.653.218
energy v30.03 ms64.465.422
DP v2 (cooperative)1.2 ms1.70.720
seam removal0.04 ms57.836.326

Key insight: Energy is memory-bound. Loading each pixel as uchar4 instead of byte-by-byte improves throughput and reduces register pressure, yielding a significant win in v2.