PIXELBANKv9.1.0
Menu

Binary MRF Optimization using Min-Cut Setup

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β‚™=0)
  • C(wn,t)=Un(0)C(w_n, t) = U_n(0) β€” Cost if wβ‚™ is separated from sink (assigned wβ‚™=1)
  • 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:

Using the graph construction rules:

  1. C(s, w₁) = U₁(1) = 1.0
  2. C(w₁, t) = U₁(0) = 5.0
  3. C(s, wβ‚‚) = Uβ‚‚(1) = 6.0
  4. C(wβ‚‚, t) = Uβ‚‚(0) = 2.0
  5. C(w₁, wβ‚‚) = 4.0 (disagreement cost)
  6. C(wβ‚‚, w₁) = 4.0 (disagreement cost)

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)
solution.py

Test Results

0/0
Run code to see test results.