- The MRF smoothness prior, sampled with Gibbs sampling.
- Exact belief propagation on a tree, reproducing the book’s numerical example (Figure 29.15) and checking it against brute-force marginalization.
- Loopy belief propagation on the image grid, for binary denoising.
- Segmentation of a photograph by loopy BP (the book’s version is Figure 29.4).
- Stereo along one scanline of a real stereo pair by BP (Figure 29.14), checked against ground truth.
Images replaced for licensing. The book is published under a CC BY-NC-ND license, which covers only the book as a whole and not its individual images, so this page does not republish the book’s photographs. License-free images stand in for them:
- The coins image (scikit-image) stands in for a photograph of a leaf (Figure 29.4).
- The Middlebury motorcycle stereo pair (scikit-image) stands in for a stereo pair of canoes (Figures 29.12 to 29.14).
The MRF prior: neighboring pixels agree
An undirected graphical model (Markov random field) puts a potential on every edge. The workhorse prior for images is the Ising / Potts model, which rewards neighboring pixels for taking the same label: There is no data here: this is purely the prior. To see what it believes, draw samples with Gibbs sampling: repeatedly replace each pixel by a draw from its conditional given its four neighbors. Because the grid is bipartite, all black-square pixels are conditionally independent given the white-square ones, so a whole color can be updated at once (checkerboard sweeps). As the coupling grows, samples go from white noise to ever-larger smooth regions: exactly the ‘images are piecewise smooth’ assumption the rest of the chapter exploits.
Exact belief propagation on a tree
On a graph without loops, belief propagation computes the exact marginals. This part reproduces the book’s worked example (Fig 29.15): a chain with an observed node hanging off . Every variable is binary and the potentials are exactly those printed in the figure: Sum-product rule: a node collects the incoming messages, and the message it sends across an edge is . A node’s marginal is the (normalized) product of all its incoming messages times its own evidence. Because this graph is a tree, BP must agree with brute-force marginalization over all joint states: the code checks that it does.
Loopy belief propagation on the image grid
The image grid has loops, so BP is no longer exact: but loopy BP (just keep passing messages) works remarkably well in practice. For binary labels every message is a two-vector, which the code track by its log-odds . Passing a log-odds belief through an Ising edge with coupling gives the closed form which is applied to all edges of one orientation at once with array shifts. The first test is denoising: a clean binary image is corrupted by flipping 20% of the pixels; the per-pixel likelihood gives each node a data log-odds , and the Ising prior glues neighbors together.
Segmentation as a two-label MRF
The same machinery segments a photograph: label each pixel object or background. The book segments a leaf by its color; here the evidence is brightness, on an image of coins against a darker background. The local evidence is how much brighter each pixel is than the median, turned into log-odds. On its own it gives a ragged, speckled mask; the smoothness prior cleans it into solid regions with short boundaries.
Stereo along a scanline
Stereo makes the graphical-model picture concrete (Fig 29.14). Take one scanline from the rectified left and right views of a rectified stereo pair (the book uses a canoe; here, the Middlebury motorcycle). Each pixel position is a node whose label is a disparity (depth); the local evidence is how well the left patch matches the right patch shifted by that disparity, and the chain of nodes is tied by a smoothness prior. The code runs sum-product BP along the 1-D chain: a forward (left-to-right) sweep and a backward (right-to-left) sweep, then multiply them with the evidence to get the marginal posterior at every position. How to read the panels below (they mirror the book):- (a), (b) the same row of pixels seen by the right and left cameras.
- (c) to (f) are position × depth images: the horizontal axis is position along the scanline (lined up with a,b) and the vertical axis is candidate depth (small disparity = far, large = near). Brighter = more probable. So each vertical slice is a probability-over-depth for one pixel.
- (c) local evidence: how well the left patch matches the right patch at each depth. Textured regions give a sharp bright spot (confident); smooth surfaces are ambiguous (diffuse).
- (d), (e) messages: belief passed left→right and right→left along the chain, carrying confident estimates into the ambiguous regions.
- (f) posterior = evidence × both messages. The bright ridge is the recovered depth profile; it is overlaid as a line.

Summary
- A graphical model splits an image problem into local evidence (data terms) and a smoothness prior (edge potentials); inference combines them.
- On a tree, sum-product BP is exact: it matched brute force to machine precision (29.15). On the looped image grid, loopy BP is approximate but effective for denoising, segmentation, and stereo.
- BP is message passing: each node’s belief is the product of its neighbors’ messages and its own evidence. Smoothness lets confident, textured regions propagate into ambiguous, textureless ones.
- These MRF and BP energies are the classical ancestors of today’s dense-prediction networks. The priors are now learned, but the evidence-plus-smoothness structure remains.

