v9.1.1
Menu

Binary MRF Optimization using Min-Cut Setup

mediumFREEGraphsGraphs, Image ProcessingCV - DSA

Problem Statement

In image processing, minimizing the energy function of a Binary Markov Random Field (MRF) is a common method for tasks like image denoising or segmentation. The goal is to assign a binary label (wn∈{0,1}w_n \in \{0,1\}) to every pixel nn to minimize the total energy:

E(w)=∑nUn(wn)+∑(m,n)∈CPmn(wm,wn)E(w) = \sum_n U_n(w_n) + \sum_{(m,n) \in C} P_{mn}(w_m, w_n)

where Un(wn)U_n(w_n) is the unary cost (fitting the label to local data) and Pmn(wm,wn)P_{mn}(w_m, w_n) is the pairwise cost (enforcing smoothness/prior constraints).

A fundamental technique for solving this optimization problem exactly when the pairwise costs are submodular is by transforming it into a Minimum Cut (Min-Cut) problem on a constructed graph.

Your Task

Implement the graph construction step for a simplified 2×1 image (two pixels, w1w_1 and w2w_2) where w1w_1 is adjacent to w2w_2. Given the unary costs and the fixed pairwise costs, calculate the capacities of the required links in the Max-Flow/Min-Cut graph:

  1. Source-to-w₁ link capacity: C(s,w1)C(s, w_1)
  2. w₁-to-Sink link capacity: C(w1,t)C(w_1, t)
  3. Source-to-w₂ link capacity: C(s,w2)C(s, w_2)
  4. w₂-to-Sink link capacity: C(w2,t)C(w_2, t)
  5. w₁-to-w₂ directional capacity: C(w1,w2)C(w_1, w_2)
  6. w₂-to-w₁ directional capacity: C(w2,w1)C(w_2, w_1)

Pairwise Costs

Assume the pairwise costs are fixed as:

  • P12(0,1)=4.0P_{12}(0,1) = 4.0 (cost if w₁=0, w₂=1)
  • P12(1,0)=4.0P_{12}(1,0) = 4.0 (cost if w₁=1, w₂=0)
  • P12(0,0)=0P_{12}(0,0) = 0 and P12(1,1)=0P_{12}(1,1) = 0 (zero-diagonal form)

Graph Construction Rules

Using the standard convention:

  • C(s,wn)=Un(1)C(s, w_n) = U_n(1) — Cost if wₙ is separated from source (assigned wₙ=1)
  • C(wn,t)=Un(0)C(w_n, t) = U_n(0) — Cost if wₙ is separated from sink (assigned wₙ=0)
  • C(w₁, w₂) = C(w₂, w₁) = P(0,1) = P(1,0) — Symmetric capacity for disagreement cost

Example

Input
U1(0)=5.0, U1(1)=1.0, U2(0)=2.0, U2(1)=6.0
Output
[1.0, 5.0, 6.0, 2.0, 4.0, 4.0]
Reasoning
  • Inputs: four unary costs, two per pixel. U1(0)=5.0U_1(0)=5.0 and U1(1)=1.0U_1(1)=1.0 say pixel w1w_1 is much cheaper as label 1; U2(0)=2.0U_2(0)=2.0 and U2(1)=6.0U_2(1)=6.0 say w2w_2 prefers label 0. The pairwise disagreement cost is fixed at 4.04.0.
  • Terminal links for w1w_1: C(s,w1)=U1(1)=1.0C(s,w_1)=U_1(1)=1.0 and C(w1,t)=U1(0)=5.0C(w_1,t)=U_1(0)=5.0. Cutting s→w1s\to w_1 puts w1w_1 on the sink side (label 1), so that link carries U1(1)U_1(1); cutting w1→tw_1\to t keeps it on the source side (label 0), so it carries U1(0)U_1(0). That is why the two values swap places relative to the input order.
  • Terminal links for w2w_2: likewise C(s,w2)=U2(1)=6.0C(s,w_2)=U_2(1)=6.0 and C(w2,t)=U2(0)=2.0C(w_2,t)=U_2(0)=2.0.
  • Neighbour links: both directions get the disagreement cost, C(w1,w2)=C(w2,w1)=4.0C(w_1,w_2)=C(w_2,w_1)=4.0.
  • Output: [1.0, 5.0, 6.0, 2.0, 4.0, 4.0], in the order source/sink for w1w_1, source/sink for w2w_2, then the two neighbour links.
  • Sanity check: with the source side meaning label 0, the labeling w1=1,w2=0w_1=1, w_2=0 cuts s→w1s\to w_1, w2→tw_2\to t and w2→w1w_2\to w_1, costing 1+2+4=71+2+4=7, exactly E(w)=U1(1)+U2(0)+P12(1,0)E(w)=U_1(1)+U_2(0)+P_{12}(1,0). Notice that the cut cost equals the MRF energy for every labeling, which is why the min-cut gives the optimal labels.

Constraints

  • Uₙ(wₙ) inputs are floats ≥ 0
  • Output capacities must be floats
  • The pairwise costs P₁₂(0,0) = 0 and P₁₂(1,1) = 0 are fixed
  • P₁₂(0,1) = P₁₂(1,0) = 4.0 (representing the smoothness cost for disagreement)

More from CV - DSA

solution.py

Test results

Sign in to run your code against the test cases.