Lec10 Image Segmentation 2
Lec10 Image Segmentation 2
COMP4421
Hao CHEN, 陳浩
Dept. of CSE and CBE
HKUST
Image Segmentation
• Goal: identify groups of pixels that go together
Image Segmentation
A process that partitions R into subregions R1, R2 ,..., Rn
• Thresholding
• Region Segmentation
5
Image Segmentation
• Fundamentals
• Thresholding
• Region Segmentation
6
Fundamentals
Preceding conditions to satisfy for image segmentation:
7
Fundamentals
• Segmentation algorithms for monochrome images generally are
based on one of two basic categories dealing with properties of
intensity values:
1. Discontinuity: edge-based algorithm
2. Similarity: region-based algorithm
8
Fundamentals
Edge detection algorithm
(gradient) a b c
(a) Image of a constant intensity d e f
region.
(b) Boundary based on intensity
discontinuities.
(c) Result of segmentation. Edge based
computations.
(f) Result of segmentation based on
region properties.
For each pixel, calculate value that’s based on Q Calculate the standard deviation
(predicate) => ∞
(>0: white)
• Thresholding
• Region Segmentation
10
Point, Line, and Edge Detection
Image characteristics with sharp, local changes in intensity:
• Edge pixels: pixels at which the intensity of an image changes abruptly.
• Edges: sets of connected edge pixels.
• Line: thin edge segment in which the intensity of the background on either side of
the line is either much higher or much lower than the intensity of the line pixels.
• Isolated point: a foreground (background) pixel surrounded by background
(foreground) pixels.
11
Background
Finite differences: derivatives of a digital function.
12
Background
Set ∆x=1, then
13
Background
Set ∆x=-1, then
14
Background
Subtracting backward difference from forward difference, we can get central
difference with first-order:
15
Background
Higher order derivatives based on a central difference:
Second:
Third:
Fourth:
16
Background
First four central digital derivatives (finite differences)
For two variables, we apply the derivative to each variable independently, e.g.,
(a) Image.
Forward difference
Central difference
Digital Image Processing, by Gonzalez and Woods, Pearson, 2018. 18
Background
Observations: a b
c
(1) First-order derivatives generally
produce thicker edges.
(2) Second-order derivatives have a stronger
response to fine detail, such as thin lines,
isolated points, and noise.
(3) Second-order derivatives produce a double-
edge response at ramp and step transitions in
intensity.
(4) The sign of the second derivative can be used
to determine whether a transition into an edge
is from light to dark or dark to light.
Forward difference
Central difference
Digital Image Processing, by Gonzalez and Woods, Pearson, 2018. 19
Background
Computing the first and second derivatives at
every pixel using spatial convolution:
Laplacian:
21
Detection of Isolated Points
a b c
Insensitive to directions!
Line detection kernels with different directions. The preferred direction of each kernel is
weighted with a larger coefficient (i.e., 2) than other possible directions.
Digital Image Processing, by Gonzalez and Woods, Pearson, 2018. 24
Line Detection with Specific Directions
a b c
d e f
(a) Image of a wire-bond mask.
(b) Result of processing with +45°
line detection kernel.
(c) Zoomed view of top-left region
of (b).
(d) Zoomed view of bottom-right
region of (b).
(e) Negative values in (b) set to zero.
(f) The points in (e) that are larger
than T=254.
Digital Image Processing, by Gonzalez and Woods, Pearson, 2018. Ringe???? also detected 25
Edge Models
Three types:
26
Edge Models
Step
28
But, in practice …. always need to do smoothing
σ=1
σ=0
σ=10
σ=0.1
30
Basic Edge Detection
Gradient:
(Vector image)
Magnitude:
(Gradient image)
Direction of gradient:
31
Basic Edge Detection
Sobel Operator:
Digital Image Processing, by Gonzalez and Woods, Pearson, 2018. Gradient magnitude: strong response along horizontal and
35 vertical
Basic Edge Detection
Areas of constant intensity in this
image indicate that the direction
of the gradient vector is the same
at all the pixel locations in those
regions.
Thicker edge
Digital Image Processing, by Gonzalez and Woods, Pearson, 2018. 37
Combining Thresholding
Edge detection + Thresholding Smoothing + Edge detection + Thresholding
thicker eage
eage
broken :
(1) Low error rate. All edges should be found, and there should be no spurious responses.
(2) Edge points should be well localized. The edges located must be as close as possible to
the true edges.
(3) Single edge point response. The detector should return only one point for each true
edge point. That is, the number of local maxima around the true edge should be minimum.
Canny edge detector: expressing the three criteria mathematically, then find
optimal solution.
39
The Canny Edge Detector
Gaussian smoothing
Gradient magnitude
Angle
40
The Canny Edge Detector
a b
Nonmaxima Suppression to thin ridges. c
(a) Locate the next unvisited edge pixel, p, in 𝑔𝑁𝐻 (𝑥, 𝑦).
(b) Mark as valid edge pixels all the weak pixels in 𝑔𝑁𝐿 (𝑥, 𝑦) that are connected to
Repeat, p using, say, 8-connectivity. P is from strong edge pixels
(c) If all nonzero pixels in 𝑔𝑁𝐻 𝑥, 𝑦 have been visited go to step (d). Else, return to
step (a).
(d) Set to zero all pixels in 𝑔𝑁𝐿 (𝑥, 𝑦) that were not marked as valid edge pixels.
Finally, append all the nonzero pixels from 𝑔𝑁𝐿 (𝑥, 𝑦) to 𝑔𝑁𝐻 (𝑥, 𝑦).
43
The Canny Edge Detector
44
The Canny Edge Detector
a
b c
45
The Canny Edge Detector
a
b c
• Thresholding
• Region Segmentation
47
Thresholding
a b
Intensity histograms that
can be partitioned (a) by a
single threshold, and (b) by
dual thresholds.
Segment with
thresholding:
49
Thresholding
a b c
The Effect of Noise d e f
(a)Noisy image.
(d)-(f) Corresponding
histograms.
52
Basic Global Thresholding
1. Select an initial estimate for the global
threshold, T.
2. Segment the image using T by producing two
groups of pixels
3. Compute the mean of these two groups of
pixels, say m1 and m2.
4. Update the threshold T=(m1 +m2)/2
5. Repeat Steps 2 through 4 until
convergence
m1 T m2
53
Basic Global Thresholding
1. Select an initial estimate for the global
threshold, T.
2. Segment the image using T by producing two
groups of pixels
3. Compute the mean of these two groups of
pixels, say m1 and m2.
4. Update the threshold T=(m1 +m2)/2
5. Repeat Steps 2 through 4 until
convergence
m1 T m2
54
Basic Global Thresholding
56
Optimum Global Thresholding using Otsu’s Method
Some variables:
{0,1,2,…,L-1}: set of L distinct integer intensity levels
M x N: image size 𝑛𝑖 : number of pixels with intensity i
𝑝𝑖 = 1 𝑝𝑖 ≥ 0
𝑖=0
57
Optimum Global Thresholding using Otsu’s Method
Suppose we threshold an image into two classes, 𝑐1 and 𝑐2 with threshold
[0, k] [k+1, L-1]
• Then, probability of a pixel assigned to class 𝑐1 occurring:
• Compute mean
intensity value of
pixels in 𝑐2 : 58
Optimum Global Thresholding using Otsu’s Method
cumulative mean up global mean:
to level k:
From preceding and
results, we have: _
점
= I -
P
Pr
,
( 앞장 ,
• Calculate global m
: 금
m .
variance: 증명 직접 해볼 것
• Calculate between-class
variance:
'
Reintroduce k
59
Optimum Global Thresholding using Otsu’s Method
A normalized measure of
separability at level k: Constant
How to obtain 𝑘 ∗ ?
Just evaluate all integer values of k (subject to condition 0 < 𝑃1 𝑘 < 1).
60
Optimum Global Thresholding using Otsu’s Method
To summarize:
• Compute the normalized histogram of the input image. Denote the components
of the histogram by 𝑝𝑖 .
• Compute the cumulative sums, 𝑝1 (𝑘).
The farther apart the means the
• Compute the cumulative means, 𝑚(𝑘). 𝟐
• Compute the global mean, 𝑚𝐺 . larger will be 𝝈𝑩 (𝒌)
where and
So we optimize
Between-class variance:
where and
66
Multiple Thresholds
A general form:
Parameters computed using
A predicate
pixels in neighborhood 𝑆𝑥𝑦
69
Variable Thresholding
a b
Local Image Properties based c d
70
Variable Thresholding
Moving Averages based
Purpose: For fast processing during scanning by computing moving
averages along scan lines.
Moving average:
An example:
where c is positive scalar, 𝑚𝑥𝑦 is the moving average at point (x, y).
72
Variable Thresholding
Moving Averages based
Correct the shading pattern
• Thresholding
• Region Segmentation
75
Region Segmentation by Region
Growing
76
Region Segmentation by Region Growing
Segmentation by Region Growing: group pixels or subregions into larger regions based
on predefined criteria for growth. 𝑓(𝑥, 𝑦): input image
S(𝑥, 𝑦): a seed array containing 1’s at the locations of
A basic pipeline based on 8-connectivity: seed points and 0’s elsewhere 𝑄 : a predicate
78
Region Segmentation by Region
Splitting and Merging
79
Region Segmentation by Splitting and Merging
Segmentation by Region Splitting and Merging
The pipeline:
1. Split into four disjoint quadrants any
region 𝑅𝑖 for which 𝑄(𝑅𝑖 ) = FALSE.
80
Region Segmentation by Splitting and Merging
a b
Segmentation by Region Splitting and c d
Merging
82
Region Segmentation by K-means Clustering
Segmentation using K-Means Clustering
Basic idea: To iteratively partition a set, Q, of observations into k clusters, and each
observation assigned to the cluster with the nearest mean (also called prototype).
Is there optimal solution? X
we can’t solve with linear time
where z is a vector observation (sample), 𝑚𝑖 is mean vector (or centroid), and 𝐶𝑖 is a cluster.
84
K-means Clustering: Step 2
85
K-means Clustering: Step 3
Re‐estimate the 𝑘
cluster centers, by
assuming the
memberships found
in the previous step
are correct.
86
K-means Clustering: Step 4
87
K-means Clustering: Step 5
Divide and Conquer
Re‐estimate the 𝑘
cluster centers, by
assuming the
memberships found
in the previous step
are correct.
88
K-means Clustering: Step …
Iterate between
step 4 and step 5
until convergence.
89
Region Segmentation by K-means Clustering
Segmentation using K-Means Clustering: A heuristic algorithm
92
Region Segmentation Using Graph Cuts
Segmentation using Graph Cuts
Basic idea: Pixels as nodes of a graph, then find an optimum partition (cut) of the
graph, where members within a group have high similarities and the ones across
groups have low similarities.
93
Region Segmentation Using Graph Cuts
Segmentation using Graph Cuts
Set 𝑉 can be partitioned into subsets by cutting the graph 𝐺.
A cut of a graph is a partition of 𝑉 into two subsets A and B by removing the edges
connecting subgraphs A and B, where
94
Region Segmentation Using Graph Cuts
Segmentation using Graph Cuts: A simple example
For example,
𝑤 𝑖, 𝑗 = 1/(|𝐼 𝑛𝑖 − 𝐼(𝑛𝑗 )| + 𝑐),
where 𝐼 𝑛𝑖 and 𝐼(𝑛𝑗 ) are intensities of nodes i and j,
respectively, and c is a constant to avoid division by zero.
Thus, the closer the intensities, the higher the similarities.
Minimum cut: The optimum partition of a graph is the one that minimizes this
cut value.
Nodes: pixels
2 1 1 9
5 4 4
3 3 3 6
1 1 1
Region Segmentation Using Graph Cuts
Graph-view of intelligent scissors:
3 3 3 6
1 1 1
End
Region Segmentation Using Graph Cuts
Graph-view of intelligent scissors:
1. Assign weights (costs) to edges
1 2 1
Start
2. Select the seed nodes
3 3 3 6
1 1 1
End
Region Segmentation Using Graph Cuts
Graph-view of intelligent scissors:
1. Assign weights (costs) to edges
1 2 1
Start
2. Select the seed nodes
where
Assoc(A, V) is the sum of the weights of all the edges from the nodes of
subgraph A to the nodes of the entire graph, and similarly for Assoc(B, V).
Ncut(A, B) will no longer have small values for the cut that partitions
isolated points.
Shi J, Malik J. Normalized cuts and image segmentation. IEEE Transactions on pattern analysis and machine intelligence. 2000 Aug;22(8):888-905. 106
Region Segmentation Using Graph Cuts
Specifying weights for graph cut segmentation
As we mentioned, intensity values or distances can be used to specify
edge weights. A more practical approach is to use both:
where 𝑛𝑖 and 𝑛𝑗 denote two nodes, 𝐼 𝑛𝑖 is the intensity of 𝑛𝑖 , 𝜎𝐼2 and 𝜎𝑑2
are constants determining the spread of the two Gaussian-like functions,
𝑑𝑖𝑠𝑡(𝑛𝑖 , 𝑛𝑗 ) is the distance between the two nodes, and r is a radical
constant that establishes how far away we are willing to consider similarity.
107
Region Segmentation Using Graph Cuts
Segmentation using Graph Cuts Actually min cut
• Thresholding
• Region Segmentation
110