v9.1.1
Menu

Contour Smoothing via Dynamic Programming (1D)

mediumPROGraphsDynamic Programming, Image ProcessingCV - DSA

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,n−1(wn,wn−1)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,n−1P_{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)+min⁡l[Sn−1,l+Pn,n−1(wn=k,wn−1=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 Sn−1S_{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,n−1P_{n,n-1} is defined by the ℓ₁ norm (Manhattan distance) between adjacent labels:

Pn,n−1(k,l)=∣k−l∣P_{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
  • Inputs: unary_costs_un is Un=(2,1,5)U_n = (2, 1, 5), the data cost of label −1-1, 00 or 11; cumulative_costs_sn_1 is Sn−1=(3,2,4)S_{n-1} = (3, 2, 4), the cheapest contour cost so far ending in each label at the previous pixel.
  • Label k=−1k=-1: the candidates Sn−1,l+∣k−l∣S_{n-1,l} + |k-l| are 3+0, 2+1, 4+2=3,3,63+0,\ 2+1,\ 4+2 = 3, 3, 6. The best is 33 (a tie between staying at −1-1 and arriving from 00), so Sn,−1=2+3=5S_{n,-1} = 2 + 3 = 5.
  • Label k=0k=0: 3+1, 2+0, 4+1=4,2,53+1,\ 2+0,\ 4+1 = 4, 2, 5. The best is 22, so Sn,0=1+2=3S_{n,0} = 1 + 2 = 3.
  • Label k=1k=1: 3+2, 2+1, 4+0=5,3,43+2,\ 2+1,\ 4+0 = 5, 3, 4. The best is 33, so Sn,1=5+3=8S_{n,1} = 5 + 3 = 8.
  • Output: [5.000, 3.000, 8.000], one cumulative cost per label in the order −1,0,1-1, 0, 1, rounded to 3 decimal places. Notice label 11 prefers to arrive from label 00: a smoothness penalty of 11 is worth paying because the previous cost there is 22 lower than staying put.

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

More from CV - DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.