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 , where represents the relative vertical shift of the contour at position .
The total cost of a contour assignment is:
where:
- is the unary cost (e.g., closeness to an edge/data likelihood)
- is the pairwise cost (smoothness)
Your Task
Implement the core recurrence relation of the DP solution (Viterbi algorithm) to find the minimum cumulative cost of reaching pixel with label :
Given the cumulative costs for the previous column and the costs , calculate the minimum cumulative costs for the current column.
Pairwise Cost
The smoothness cost is defined by the ℓ₁ norm (Manhattan distance) between adjacent labels:
Example
Unary: [2.0, 1.0, 5.0], Cumulative: [3.0, 2.0, 4.0]
[5.000, 3.000, 8.000]
- Inputs:
unary_costs_unis , the data cost of label , or ;cumulative_costs_sn_1is , the cheapest contour cost so far ending in each label at the previous pixel. - Label : the candidates are . The best is (a tie between staying at and arriving from ), so .
- Label : . The best is , so .
- Label : . The best is , so .
- Output:
[5.000, 3.000, 8.000], one cumulative cost per label in the order , rounded to 3 decimal places. Notice label prefers to arrive from label : a smoothness penalty of is worth paying because the previous cost there is 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