PIXELBANKv8.2.1
Menu

Contour Smoothing via Dynamic Programming (1D)

Problem Statement

Active Contour Models (Snakes) or Dynamic Time Warping techniques often use Dynamic Programming (DP) to minimize an energy function across a sequence of points. This is equivalent to finding the minimum cost path in a chain graph.

Problem Setup

We consider a simplified 1D contour segmentation problem represented by a chain of pixels w1,w2,,wnw_1, w_2, \ldots, w_n, where wn{1,0,1}w_n \in \{-1, 0, 1\} represents the relative vertical shift of the contour at position nn.

The total cost SS of a contour assignment ww is:

S(w)=nUn(wn)+nPn,n1(wn,wn1)S(w) = \sum_n U_n(w_n) + \sum_n P_{n,n-1}(w_n, w_{n-1})

where:

  • UnU_n is the unary cost (e.g., closeness to an edge/data likelihood)
  • Pn,n1P_{n,n-1} is the pairwise cost (smoothness)

Your Task

Implement the core recurrence relation of the DP solution (Viterbi algorithm) to find the minimum cumulative cost Sn,kS_{n,k} of reaching pixel nn with label kk:

Sn,k=Un(wn=k)+minl[Sn1,l+Pn,n1(wn=k,wn1=l)]S_{n,k} = U_n(w_n = k) + \min_l [S_{n-1,l} + P_{n,n-1}(w_n = k, w_{n-1} = l)]

Given the cumulative costs Sn1S_{n-1} for the previous column and the costs UnU_n, calculate the minimum cumulative costs SnS_n for the current column.

Pairwise Cost

The smoothness cost Pn,n1P_{n,n-1} is defined by the ℓ₁ norm (Manhattan distance) between adjacent labels:

Pn,n1(k,l)=klP_{n,n-1}(k, l) = |k - l|

Example:

Input:
Unary: [2.0, 1.0, 5.0], Cumulative: [3.0, 2.0, 4.0]
Output:
[5.000, 3.000, 8.000]
Reasoning:
  • For each target label k:

  • Sₙ(-1) = U(-1) + min{S_{n-1}(-1)+0, S_{n-1}(0)+1, S_{n-1}(1)+2}

    • = 2.0 + min{3.0, 3.0, 6.0} = 2.0 + 3.0 = 5.0
  • Sₙ(0) = U(0) + min{S_{n-1}(-1)+1, S_{n-1}(0)+0, S_{n-1}(1)+1}

    • = 1.0 + min{4.0, 2.0, 5.0} = 1.0 + 2.0 = 3.0
  • Sₙ(1) = U(1) + min{S_{n-1}(-1)+2, S_{n-1}(0)+1, S_{n-1}(1)+0}

    • = 5.0 + min{5.0, 3.0, 4.0} = 5.0 + 3.0 = 8.0

Constraints:

  • The possible labels are k ∈ {-1, 0, 1}
  • The smoothness cost P_{n,n-1} is defined by |k - l|
  • Uₙ and S_{n-1} are provided as lists indexed by [-1, 0, 1]
  • Output costs must be rounded to 3 decimal places
Editor

Test Results

0/0
Run code to see test results.