You are on page 1of 67

Lecture 6: Clustering and 

Segmentation – Part 2
Professor Fei‐Fei Li
Stanford Vision Lab

Fei-Fei Li Lecture 6 - 1 11‐Jan‐11


Recap: Gestalt Theory
• Gestalt: whole or group
– Whole is greater than sum of its parts
– Relationships among parts can yield new properties/features

• Psychologists identified series of factors that predispose set of 
elements to be grouped (by human visual system)
“I stand at the window and see a house, trees, sky.
Theoretically I might say there were 327 brightnesses
and nuances of colour. Do I have "327"? No. I have sky, house,
and trees.”
Max Wertheimer
(1880-1943)

Untersuchungen zur Lehre von der Gestalt,


Psychologische Forschung, Vol. 4, pp. 301-350, 1923
http://psy.ed.asu.edu/~classics/Wertheimer/Forms/forms.htm

Fei-Fei Li Lecture 6 - 2 11‐Jan‐11


Recap: Gestalt Factors

• These factors make intuitive sense, but are very difficult to translate into algorithms.

Fei-Fei Li Lecture 6 - 3 11‐Jan‐11


Recap: Image Segmentation
• Goal: identify groups of pixels that go together

Fei-Fei Li Lecture 6 - 4 11‐Jan‐11


Recap: K‐Means Clustering
• Basic idea: randomly initialize the k cluster centers, and 
iterate between the two steps we just saw.
1. Randomly initialize the cluster centers, c1, ..., cK
2. Given cluster centers, determine points in each cluster
• For each point p, find the closest ci.  Put p into cluster i
3. Given points in each cluster, solve for ci
• Set ci to be the mean of points in cluster i
4. If ci have changed, repeat Step 2

• Properties
– Will always converge to some solution
– Can be a “local minimum”
• Does not always find the global minimum of objective function:

Fei-Fei Li Lecture 6 - 5 11‐Jan‐11


Recap: Expectation Maximization (EM)

• Goal
– Find blob parameters θ that maximize the likelihood function:

• Approach:
1. E‐step:  given current guess of blobs, compute ownership of each point
2. M‐step:  given ownership probabilities, update blobs to maximize likelihood 
function
3. Repeat until convergence

Fei-Fei Li Lecture 6 - 6 11‐Jan‐11


Recap: Mean‐Shift Algorithm

• Iterative Mode Search
1. Initialize random seed, and window W
2. Calculate center of gravity (the “mean”) of W:
3. Shift the search window to the mean
4. Repeat Step 2 until convergence

Fei-Fei Li Lecture 6 - 7 11‐Jan‐11


Recap: Mean‐Shift Clustering
• Cluster: all data points in the attraction basin of a mode
• Attraction basin: the region for which all trajectories lead to 
the same mode

Fei-Fei Li Lecture 6 - 8 11‐Jan‐11


Recap: Mean‐Shift Segmentation
• Find features (color, gradients, texture, etc)
• Initialize windows at individual pixel locations
• Perform mean shift for each window until convergence
• Merge windows that end up near the same “peak” or mode

Fei-Fei Li Lecture 6 - 9 11‐Jan‐11


Back to the Image Segmentation Problem…
• Goal: identify groups of pixels that go together

• Up to now, we have focused on ways to group pixels into image 
segments based on their appearance…
– Segmentation as clustering.
• We also want to enforce region constraints.
– Spatial consistency
– Smooth borders

Fei-Fei Li Lecture 6 - 10 11‐Jan‐11


What we will learn today?
• Graph theoretic segmentation
– Normalized Cuts
– Using texture features
• Segmentation as Energy Minimization
– Markov Random Fields
– Graph cuts for image segmentation
– s‐t mincut algorithm
– Extension to non‐binary case
– Applications
(Midterm materials)
Fei-Fei Li Lecture 6 - 11 11‐Jan‐11
What we will learn today?
• Graph theoretic segmentation
– Normalized Cuts
– Using texture features
• Segmentation as Energy Minimization
– Markov Random Fields
– Graph cuts for image segmentation
– s‐t mincut algorithm
– Extension to non‐binary case
– Applications

Fei-Fei Li Lecture 6 - 12 11‐Jan‐11


Images as Graphs

q
wpq
w
p

• Fully‐connected graph
– Node (vertex) for every pixel
– Link between every pair of pixels, (p,q)
– Affinity weight wpq for each link (edge)
• wpq measures similarity
• Similarity is inversely proportional to difference 
(in color and position…)

Slide credit: Steve Seitz

Fei-Fei Li Lecture 6 - 13 11‐Jan‐11


Segmentation by Graph Cuts

A B C

• Break Graph into Segments
– Delete links that cross between segments
– Easiest to break links that have low similarity (low weight)
• Similar pixels should be in the same segments
• Dissimilar pixels should be in different segments

Slide credit: Steve Seitz

Fei-Fei Li Lecture 6 - 14 11‐Jan‐11


Measuring Affinity
• Distance 
aff ( x, y )  exp  2 2 x  y
1
d
2

• Intensity 
aff ( x, y )  exp  2 2 I ( x)  I ( y )
1
d
2

• Color 
aff ( x, y )  exp  2 2 dist  c( x), c( y ) 
1
d
2

(some suitable color space distance)

Source: Forsyth & Ponce


• Texture 
aff ( x, y )  exp  2 2 f ( x)  f ( y )
1
d
2

(vectors of filter outputs)

Fei-Fei Li Lecture 6 - 15 11‐Jan‐11


Scale Affects Affinity
• Small σ: group only nearby points
• Large σ: group far‐away points

Slide credit: Svetlana Lazebnik Small  Medium  Large 


Fei-Fei Li Lecture 6 - 16 11‐Jan‐11
Graph Cut: using Eigenvalues
• Extract a single good cluster
– Where elements have high affinity values with each other

Fei-Fei Li Lecture 6 -
Graph Cut: using Eigenvalues

points

eigenvector

matrix

Fei-Fei Li Lecture 6 -
Graph Cut: using Eigenvalues
• Extract a single good cluster
• Extract weights for a set of clusters

Fei-Fei Li Lecture 6 -
Graph Cut: using Eigenvalues

Fei-Fei Li Lecture 6 -
Fei-Fei Li Lecture 6 -
Graph Cut

B
A

• Set of edges whose removal makes a graph 
disconnected
• Cost of a cut 
– Sum of weights of cut edges: cut ( A, B)  w
p A, qB
p ,q

• A graph cut gives us a segmentation
– What is a “good” graph cut and how do we find one?

Slide credit: Steve Seitz

Fei-Fei Li Lecture 6 - 22 11‐Jan‐11


Graph Cut

Here, the cut is nicely


defined by the block-diagonal
structure of the affinity matrix.

 How can this be generalized?

Image Source: Forsyth & Ponce

Fei-Fei Li Lecture 6 - 23 11‐Jan‐11


Minimum Cut
• We can do segmentation by finding the minimum cut in a graph
– a minimum cut of a graph is a cut whose cutset has the smallest number 
of elements (unweighted case) or smallest sum of weights possible.
– Efficient algorithms exist for doing this
• Drawback: 
– Weight of cut proportional to number of edges in the cut

Slide credit: Khurram Hassan-Shafique


– Minimum cut tends to cut off very small, isolated components

Cuts with 
lesser weight
than the 
ideal cut

Ideal Cut

Fei-Fei Li Lecture 6 - 24 11‐Jan‐11


Normalized Cut (NCut)
• A minimum cut penalizes large segments
• This can be fixed by normalizing for size of segments
• The normalized cut cost is:
cut ( A, B) cut ( A, B)

assoc( A, V ) assoc( B, V )
assoc(A,V) = sum of weights of all edges in V that touch A

• The exact solution is NP‐hard but an approximation can be 
computed by solving a generalized eigenvalue problem.
J. Shi and J. Malik. Normalized cuts and image segmentation. PAMI 2000

Fei-Fei Li Lecture 6 - 25 11‐Jan‐11


Interpretation as a Dynamical System

Slide credit: Steve Seitz


• Treat the links as springs and shake the system
– Elasticity proportional to cost
– Vibration “modes” correspond to segments
• Can compute these by solving a generalized eigenvector problem

Fei-Fei Li Lecture 6 - 26 11‐Jan‐11


NCuts as a Generalized Eigenvector Problem

• Definitions
W : the affinity matrix, W (i, j )  wi , j ;
D : the diag. matrix, D (i, i )   j W (i, j );
x : a vector in {1, 1}N , x(i )  1  i  A.

• Rewriting Normalized Cut in matrix form:
cut (A,B) cut (A,B)
NCut (A,B)  
assoc(A,V) assoc(B,V)


(1  x)T ( D  W )(1  x) (1  x)T ( D  W )(1  x)
 ; k
 D(i, i)
xi  0
T
k1 D1 (1  k )1 D1
T
 D(i, i)
i

 ...
Slide credit: Jitendra Malik

Fei-Fei Li Lecture 6 - 27 11‐Jan‐11


Some More Math…

Slide credit: Jitendra Malik


Fei-Fei Li Lecture 6 - 28 11‐Jan‐11
NCuts as a Generalized Eigenvalue Problem

• After simplification, we get
yT ( D  W ) y This is hard,
NCut ( A, B)  , with y  {1, b}, y T
D1  0.
T
y Dy
i
as y is discrete!
• This is a Rayleigh Quotient
– Solution given by the “generalized” eigenvalue problem

‫ ܦ‬െ ܹ ‫ ݕ‬ൌ ߣ‫ݕܦ‬


– Solved by converting to standard eigenvalue problem
1 1 1
 
D (D  W)D z  λz , where z  D y
2 2 2
Relaxation:
• Subtleties continuous y.
– Optimal solution is second smallest eigenvector
– Gives continuous result—must convert into discrete values of y

Slide credit: Alyosha Efros

Fei-Fei Li Lecture 6 - 29 11‐Jan‐11


NCuts Example

Smallest eigenvectors

NCuts segments Image source: Shi & Malik

Fei-Fei Li Lecture 6 - 30 11‐Jan‐11


Discretization
• Problem: eigenvectors take on continuous values
– How to choose the splitting point to binarize the image?

Image Eigenvector NCut scores

• Possible procedures 
a) Pick a constant value (0, or 0.5).
b) Pick the median value as splitting point.
c) Look for the splitting point that has the minimum NCut value:
1. Choose n possible splitting points.
2. Compute NCut value.
3. Pick minimum.

Fei-Fei Li Lecture 6 - 31 11‐Jan‐11


NCuts: Overall Procedure
1. Construct a weighted graph G=(V,E) from an image.
2. Connect each pair of pixels, and assign graph edge weights,
W (i, j )  Prob. that i and j belong to the same region.

( D  W ) y   Dy
3. Solve                           for the smallest few eigenvectors. This 
yields a continuous solution.
4. Threshold eigenvectors to get a discrete cut

Slide credit: Jitendra Malik


– This is where the approximation is made (we’re not solving NP).
5. Recursively subdivide if NCut value is below a pre‐specified 
value.
NCuts Matlab code available at
http://www.cis.upenn.edu/~jshi/software/

Fei-Fei Li Lecture 6 - 32 11‐Jan‐11


Color Image Segmentation with NCuts

Image Source: Shi & Malik


Fei-Fei Li Lecture 6 - 33 11‐Jan‐11
Using Texture Features for Segmentation
• Texture descriptor is vector of filter bank outputs

Slide credit: Svetlana Lazebnik


J. Malik, S. Belongie, T. Leung and J. Shi. "Contour and Texture Analysis for Image Segmentation". 
IJCV 43(1),7‐27,2001. 

Fei-Fei Li Lecture 6 - 34 11‐Jan‐11


Using Texture Features for Segmentation

• Texture descriptor is 
vector of filter bank 
outputs.
• Textons are found by 
clustering.

Slide credit: Svetlana Lazebnik

Fei-Fei Li Lecture 6 - 35 11‐Jan‐11


Using Texture Features for Segmentation

• Texture descriptor is 
vector of filter bank 
outputs.
• Textons are found by 
clustering.
• Affinities are given by 
similarities of texton
histograms over 
windows given by the 
“local scale” of the 
texture .
Slide credit: Svetlana Lazebnik

Fei-Fei Li Lecture 6 - 36 11‐Jan‐11


Results with Color & Texture

Fei-Fei Li Lecture 6 - 37 11‐Jan‐11


Summary: Normalized Cuts
• Pros:
– Generic framework, flexible to choice of function that 
computes weights (“affinities”) between nodes
– Does not require any model of the data distribution

• Cons:
– Time and memory complexity can be high
• Dense, highly connected graphs  many affinity computations
• Solving eigenvalue problem for each cut
– Preference for balanced partitions
• If a region is uniform, NCuts will find the
modes of vibration of the image dimensions

Slide credit: Kristen Grauman

Fei-Fei Li Lecture 6 - 38 11‐Jan‐11


What we will learn today?
• Graph theoretic segmentation
– Normalized Cuts
– Using texture features
• Segmentation as Energy Minimization
– Markov Random Fields
– Graph cuts for image segmentation
– s‐t mincut algorithm
– Extension to non‐binary case
– Applications

Fei-Fei Li Lecture 6 - 39 11‐Jan‐11


Markov Random Fields
• Allow rich probabilistic models for images
• But built in a local, modular way
– Learn local effects, get global effects out

Observed evidence

Slide credit: William Freeman


Hidden “true states”

Neighborhood relations

Fei-Fei Li Lecture 6 - 40 11‐Jan‐11


MRF Nodes as Pixels

Original image Degraded image Reconstruction


from MRF modeling

Slide credit: Bastian Leibe


pixel neighborhood
statistics

Fei-Fei Li Lecture 6 - 41 11‐Jan‐11


MRF Nodes as Patches
Image patches

Scene patches
Image
 ( xi , yi )

Slide credit: William Freeman


 ( xi , x j )

Scene

Fei-Fei Li Lecture 6 - 42 11‐Jan‐11


Network Joint Probability

P( x, y )    ( xi , yi )  ( xi , x j )
i i, j

Scene Image-scene Scene-scene


compatibility compatibility
Image function function

Slide credit: William Freeman


Local Neighboring
observations scene nodes

Fei-Fei Li Lecture 6 - 43 11‐Jan‐11


Energy Formulation
• Joint probability
P( x, y )    ( xi , yi )  ( xi , x j )
i i, j
• Taking the log p(.) turns this into an Energy optimization 
problem

log P( x, y )   log  ( xi , yi )   log  ( xi , x j )


i i, j

E ( x, y )    ( xi , yi )   ( xi , x j )

Slide credit: Bastian Leibe


i i, j

• This is similar to free‐energy problems in statistical mechanics 
(spin glass theory). We therefore draw the analogy and call E
an energy function.
•  and  are called potentials.

Fei-Fei Li Lecture 6 - 44 11‐Jan‐11


Energy Formulation
• Energy function  ( xi , yi )

E ( x, y )    ( xi , yi )   ( xi , x j )  ( xi , x j )
i i, j
Single-node Pairwise
potentials potentials

• Single‐node potentials 
– Encode local information about the given pixel/patch
– How likely is a pixel/patch to belong to a certain class

Slide credit: Bastian Leibe


(e.g. foreground/background)?
• Pairwise potentials 
– Encode neighborhood information
– How different is a pixel/patch’s label from that of its neighbor? (e.g. 
based on intensity/color/texture difference, edges)

Fei-Fei Li Lecture 6 - 45 11‐Jan‐11


Energy Minimization
 ( xi , yi )
• Goal:
– Infer the optimal labeling of the MRF.  ( xi , x j )

• Many inference algorithms are available, e.g.
– Gibbs sampling, simulated annealing
– Iterated conditional modes (ICM)
– Variational methods
– Belief propagation
– Graph cuts

Slide credit: Bastian Leibe


• Recently, Graph Cuts have become a popular tool
– Only suitable for a certain class of energy functions
– But the solution can be obtained very fast for typical vision 
problems (~1MPixel/sec).

Fei-Fei Li Lecture 6 - 46 11‐Jan‐11


What we will learn today?
• Graph theoretic segmentation
– Normalized Cuts
– Using texture features
– Extension: Multi‐level segmentation
• Segmentation as Energy Minimization
– Markov Random Fields
– Graph cuts for image segmentation
– s‐t mincut algorithm
– Extension to non‐binary case
– Applications

Fei-Fei Li Lecture 6 - 47 11‐Jan‐11


Graph Cuts for Optimal Boundary Detection
• Idea: convert MRF into source‐sink graph
hard 
t
n-links a cut
constraint

hard 
constraint
s
Minimum cost cut can be 
 I pq 
computed in polynomial time wpq  exp  2 
 2 
(max‐flow/min‐cut algorithms)


Slide credit: Yuri Boykov I pq

Fei-Fei Li Lecture 6 - 48 11‐Jan‐11


Simple Example of Energy
Regional term Boundary term
E ( L)  
p
Dp (Lp )  w
pq N
pq   ( L p  Lq )
t‐links n‐links

 I pq 
wpq  exp  2 
t a cut  2 
D p (t )

I pq

D p (s ) L p  {s, t}
s (binary object segmentation)
Slide credit: Yuri Boykov

Fei-Fei Li Lecture 6 - 49 11‐Jan‐11


Adding Regional Properties
t
n-links a cut
D p (t )

w pq

D p (s )
s
Regional bias example

I s and I t
Suppose                       are given 
“expected” intensities 

Dp (s)  exp  || I p  I s ||2 / 2 2 
of object and background D (t )  exp  || I
p p  I t 2
|| / 2 2

NOTE: hard constrains are not required, in general.
Slide credit: Yuri Boykov

Fei-Fei Li Lecture 6 - 50 11‐Jan‐11


Adding Regional Properties
t
n-links a cut
D p (t )

w pq

D p (s )
s

“expected” intensities of
object and background 
Dp (s)  exp  || I p  I s ||2 / 2 2 
I s and I t D (t )  exp  || I
p p  I t 2
|| / 2 2

can be re-estimated
EM‐style optimization
Slide credit: Yuri Boykov

Fei-Fei Li Lecture 6 - 51 11‐Jan‐11


Adding Regional Properties
• More generally, regional bias can be based on any 
intensity models of object and background

Dp (s)
t a cut Dp (Lp )   logPr(I p | Lp )

Pr(I p | t)

Pr(I p | s)
Dp (t) Ip I
s
given object and background intensity histograms 

Slide credit: Yuri Boykov

Fei-Fei Li Lecture 6 - 52 11‐Jan‐11


How to Set the Potentials? Some Examples
• Color potentials
– e.g. modeled with a Mixture of Gaussians
 ( xi , yi ; )  log   ( xi , k ) P(k | xi )N ( yi ; yk ,  k )
k

• Edge potentials
– e.g. a “contrast sensitive Potts model”
 ( xi , x j , gij ( y ); )  T gij ( y ) ( xi  x j )
where
 
2
  yi  y j 2
gij ( y )  e   2  avg yi  y j

• Parameters ,  need to be learned, too!

[Shotton & Winn, ECCV’06]

Fei-Fei Li Lecture 6 - 53 11‐Jan‐11


What we will learn today?
• Graph theoretic segmentation
– Normalized Cuts
– Using texture features
– Extension: Multi‐level segmentation
• Segmentation as Energy Minimization
– Markov Random Fields
– Graph cuts for image segmentation
– s‐t mincut algorithm
– Extension to non‐binary case
– Applications

Fei-Fei Li Lecture 6 - 54 11‐Jan‐11


GraphCut Applications: “GrabCut”
• Interactive Image Segmentation [Boykov & Jolly, ICCV’01]
– Rough region cues sufficient 
– Segmentation boundary can be extracted from edges
• Procedure
– User marks foreground and background regions with a brush.
– This is used to create an initial segmentation
which can then be corrected by additional brush strokes.

Additional 
segmentation
cues
User segmentation cues Slide credit: Matthieu Bray

Fei-Fei Li Lecture 6 - 55 11‐Jan‐11


GrabCut: Data Model
Foreground Background
color color

Global optimum of the 
energy 
• Obtained from interactive user input
– User marks foreground and background regions with a brush
– Alternatively, user can specify a bounding box
Slide credit: Carsten Rother

Fei-Fei Li Lecture 6 - 56 11‐Jan‐11


GrabCut: Coherence Model
• An object is a coherent set of pixels:

  x  xm  e
  ym  y n
2
 ( x, y )   n
( m , n )C

Error (%) over training set:


How to choose  ?  

25
Slide credit: Carsten Rother

Fei-Fei Li Lecture 6 - 57 11‐Jan‐11


Iterated Graph Cuts
R
Foreground &
Foreground
Background

Background
Background G
Color model
(Mixture of Gaussians)

Result
1 2 3 4
Energy after 
each iteration
Slide credit: Carsten Rother

Fei-Fei Li Lecture 6 - 58 11‐Jan‐11


GrabCut: live demo

Reported results

• Included in MS Office 2010 (let’s try it)

Fei-Fei Li Lecture 6 - 59 11‐Jan‐11


GrabCut: live demo

Reported results

• Included in MS Office 2010 (let’s try it)

Fei-Fei Li Lecture 6 - 60 11‐Jan‐11


GrabCut: live demo

• Included in MS Office 2010 (let’s try it)

Fei-Fei Li Lecture 6 - 61 11‐Jan‐11


GraphCut Image Synthesis Results

Source: Vivek Kwatra
Fei-Fei Li Lecture 6 - 62
11‐Jan‐11
Application: Texture Synthesis in the Media

• Currently, still done manually…
Slide credit: Kristen Grauman

Fei-Fei Li Lecture 6 - 63 11‐Jan‐11


Improving Efficiency of Segmentation

• Problem: Images contain many pixels
– Even with efficient graph cuts, an MRF
formulation has too many nodes for 
interactive results.
• Efficiency trick: Superpixels
– Group together similar‐looking 
pixels for efficiency of further 
processing.
– Cheap, local oversegmentation
– Important to ensure that superpixels 
do not cross boundaries
• Several different approaches possible
– Superpixel code available here
– http://www.cs.sfu.ca/~mori/research/superpixels/

Image source: Greg Mori

Fei-Fei Li Lecture 6 - 64 11‐Jan‐11


Superpixels for Pre‐Segmentation

Speedup
Graph structure
Fei-Fei Li Lecture 6 - 65 11‐Jan‐11
Summary: Graph Cuts Segmentation
• Pros
– Powerful technique, based on probabilistic model (MRF).
– Applicable for a wide range of problems.
– Very efficient algorithms available for vision problems.
– Becoming a de‐facto standard for many segmentation 
tasks.
• Cons/Issues
– Graph cuts can only solve a limited class of models
• Submodular energy functions
• Can capture only part of the expressiveness of MRFs
– Only approximate algorithms available for multi‐label case

Fei-Fei Li Lecture 6 - 66
11‐Jan‐11
What we have learned today?
• Graph theoretic segmentation
– Normalized Cuts
– Using texture features
• Segmentation as Energy Minimization
– Markov Random Fields
– Graph cuts for image segmentation
– s‐t mincut algorithm
– Extension to non‐binary case
– Applications
(Midterm materials)
Fei-Fei Li Lecture 6 - 67 11‐Jan‐11

You might also like