Chapter 10
Chapter 10
Image Processing
Chapter 10
Image Segmentation
Image Segmentation
• Image analysis: techniques for extracting information from
an image
• The first step in image analysis is to segment the image.
• Segmentation: subdivides the image into its constituent parts
or objects.
• Autonomous segmentation is one of the most difficult tasks
in image processing.
• Segmentation algorithms for monochrome images generally
are based on two basic properties of gray-level values:
1. Discontinuity
2. Similarity
2
Image Segmentation
• Discontinuity: the image is partitioned based on abrupt
changes in gray level
• Similarity: partition an image into regions that are similar
– Main approaches are thresholding, region growing and region splitting
and merging.
3
Detection of Discontinuities
• There are 3 basic types of discontinuities in digital images:
1. Point
2. Line
3. Edges.
4
Point Detection
5
Background
• Abrupt, local changes can be detected by spatial
differentiation
• Since images are digital we should use digital differentiation
operators.
• First and second order derivatives are commonly used.
• We consider 1-D case first and then expand the results to
images.
6
7
Background
• Comparing first and second order derivatives:
1. First-order derivatives generally produce thicker edges in an
image
2. Second order derivatives have a stronger response to fine
details such as thin lines and isolated points
3. Second order derivates produce a double response at step
changes in gray level
8
Laplacian
• How to obtain 2-D second order derivative and find a mask
corresponding to it?
• We would like our filter to be isotropic: response of the filter
is independent of the direction of the discontinuity in the
image
• Simplest isotropic second order derivative is the Laplacian:
9
Line Detection
10
Line Detection
• Line detection: If each mask is moved around an image it
would respond more strongly to lines in the mentioned
direction.
11
12
Edge detection
• Edge: boundary between two regions with relatively distinct
gray levels.
• Basic idea: computation of a local derivative operator.
13
Edge detection
• The magnitude of the first derivative can be used to detect an
edge
• The sign (zero crossing) of the second derivative can be used
to detect an edge.
• The same idea can be extended into 2-D. 2-D derivatives
should be used.
• The magnitude of the gradient and sign of the Laplacian are
used.
14
15
Gradient Operator
16
Gradient Operator
• If we compute the magnitude of gradient of an image and
display it, it will be the edges in the image.
• Direction of edge at every point is perpendicular to the
direction of the gradient vector (given by α (x,y)).
• How to approximate the gradient operator?
17
18
19
20
21
22
23
Laplacian of Gaussian
• Laplacian in its original form is not used for edge detection
because:
1. It is very sensitive to noise
2. It’s magnitude produces double edges
3. Unable to detect the direction of an edge.
• To solve the first problem, the image is low-pass filtered
before using the Laplacian operator.
g(x,y)
f(x,y) h(x,y)
24
Laplacian of Gaussian (LoG)
25
LoG
• To solve the problem of double edges, zero crossing of the
output of Laplacian operator is used.
26
27
Difference of Gaussian (DoG)
• LoG can be approximated by a difference of Gaussians:
x2 +y 2 x2 +y 2
1 2 1 2
DoG(x, y) = e 2 1
e 2 2
2⇡ 12 2⇡ 22
2 2 2
2
= 2
1 2
2 ln
1
2
1 2 2
28
29
Canny edge detector
• 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 must be as
close as possible to true edges.
• 3) Single edge point response. The detector should not
identify multiple edge pixels where only a single edge point
exist
30
Canny edge detector
• Input image f(x,y) is smoothed using a Gaussian filter and
then the gradient magnitude and direction are calculated
x2 +y 2
G(x, y) = e 2 2
gy
↵(x, y) = arctan
gx
31
Canny edge detector
• M(x,y) typically contains wide ridges around local maxima
• Thinning those ridges: nonmaxima suppression
• Let d1,d2,d3 and d4 denote the 4 basic edge directions.
• In every 3x3-region:
– Find the direction dk that is closest to α(x,y).
– If the value of M(x,y) is less than one of its two neighbors along dk,
suppress edge.
32
Canny edge detector
33
Canny edge detector
• Final step: reduce false edge points.
• Reduce them using hysteresis (or double) thresholding.
• Let 𝑇𝐿 ,𝑇𝐻 be low and high thresholds. The suggested ratio is
1:3 or 2:3.
• Define 𝑔𝑁𝐻(𝑥,𝑦)) and 𝑔𝑁𝐿(𝑥,𝑦)) to be 𝑔𝑁(𝑥,𝑦)) after thresholding
with 𝑇𝐿 ,𝑇𝐻.
• It is clear that 𝑔𝑁𝐿(𝑥,𝑦)) contains all the point located in
𝑔𝑁𝐻(𝑥,𝑦))
• We will separate them by subtraction: 𝑔𝑁𝐿(𝑥,𝑦)= )= 𝑔𝑁𝐻(𝑥,𝑦)) −
𝑔𝑁𝐿(𝑥,𝑦))
• Now 𝑔𝑁𝐿(𝑥,𝑦)) and 𝑔𝑁𝐻(𝑥,𝑦)) are the “weak” and the “strong”
edge pixels.
34
Canny edge detector
• The algorithm for building the edges is:
• 1. Locate the next unvisited pixel 𝑝 in 𝑔𝑁𝐻(𝑥,𝑦).).
• 2. Mark as valid pixels all the weak pixels in 𝑔𝑁𝐿(𝑥,𝑦)) that are
connected to 𝑝
• 3. If not all non-zero pixels in 𝑔𝑁𝐻(𝑥,𝑦)) have been visited
return to step 1.
• 4. Set all the non-marked pixels in 𝑔𝑁𝐿(𝑥,𝑦)) to 0.
• 5. Return 𝑔𝑁𝐻(𝑥,𝑦)) + 𝑔𝑁𝐿(𝑥,𝑦). ).
35
Canny edge detector
36
Edge linking and Boundary Detection
• The output of discontinuity detection stage seldom
characterizes a boundary completely.
• This is because of noise, breaks in the boundary and other
effects.
• Edge detection algorithms typically are followed by linking
and boundary detection procedures.
• These procedures assemble edge pixels into meaningful
boundaries.
37
Edge linking and Boundary Detection
• A simple approach for linking edge points is to analyze the
pixels in a small neighborhood about every point (x,y).
• All the points that are similar are linked forming a boundary
of pixels.
• Two principle properties used for establishing similarity of
edge pixels in this kind of analysis are:
1. The strength of the response of the gradient operator used to produce
the edge pixels
2. The direction of the gradient
38
Local Processing
• 1.Compute the gradient magnitude and angle arrays, M(x,y)
and α(x,y), of the input image, f(x,y) .
• 2.Form a binary image, g, whose value at any pair of
coordinates (x,y) is given by:
39
Local Processing
• 3. Scan the rows of g and fill (set to 1) all gaps (sets of 0s) in
each row that do not exceed a specified length, K. Note that,
by definition, a gap is bounded at both ends by one or more
1s. The rows are processed individually with no memory
between them.
• 4. To detect gaps in any other direction, θ, rotate g by this
angle and apply the horizontal scanning procedure in step 3.
Rotate the result back by -θ.
40
Edge linking and Boundary Detection
41
Edge linking and Boundary Detection
• The other technique that can be used to link points is to
determine whether they lie on a cure of specified shape (e.g.,
line, circle).
• We first consider straight lines.
• For n points in an image we want to find subsets of these
points that lie on straight lines.
• n points:
42
Edge linking and Boundary Detection
• Alternative way: Hough transform
• (xi,yi): all the lines passing this point yi=axi+b
• b= yi-axi : is a single line in ab plane.
• Another point (xj,yj) also has a single line in ab plane b= yj-axj
• a’ and b’ are the slope and intercept of the line containing both (xi,yi) and
(xj,yj).
b’
(xi,yi) y b
a’
(xj,yj)
x a
43
Hough transform
• (amin,amax): expected range of slope
• (bmin,bmax): expected range of intercepts
• A(i,j): the number of points in the cell at coordinates (i,j) in
the ab plane.
bmin 0 bmax
For every point in the image a
plane, we let the value of a min b
equal each of the allowed
subdivisions and find the 0
corresponding b from b= yi-axi
If for ap we get bq the A(p,q) a
max
is incremented.
a
44
Hough transform
• The cell with the largest value shows the parameters of the
line that contains the maximum number of points.
• Problem with this method: a approaches infinity as the line
gets perpendicular to the x axis.
• Solution: use the representation of the line as:
45
Hough transform
• A point in xy plane is mapped into a sinusoidal curve in
plane.
• M point on the line will generate M
sinusoidal curves intersecting at
• For increments of between –90 and 90, find the value of
for every point in xy plane. Increment the corresponding cell
counter.
y
46
47
Hough transform
• Hough transform is applicable to any function of the form g(v,c)=0,
where v is the vector of coordinates and c is the vector of coefficients.
• Exp:
• 3 parameters (c1,c2,c3), 3-D parameter space, cube like cells,
accumulators of the form A(i,j,k).
• Procedure:
1. Increment c1 and c2
2. Solve for c3
3. Update the accumulator associated with (c1,c2,c3)
48
49
Thresholding
• Thresholding is one of the most important approaches to
image segmentation.
• Gray level histogram of an image f(x,y) composed of a
light object on a dark background.
• To extract the object: select a threshold T that separates
the gray levels of the background and the object.
• Any point with f(x,y)>T belongs to object.
50
Thresholding
51
52
Thresholding
• Threshold in general can be calculated as:
T=T(x,y,p(x,y),f(x,y))
f(x,y): gray level at (x,y)
p(x,y): some local property of the point (x,y) (e.g., the average gray level
of a neighborhood centered on (x,y).
53
Thresholding
• Poor or non-uniform illumination can affect the image formed
(because of f(x,y)=r(x,y).i(x,y)) and make the segmentation
difficult.
• Solution: if we can determine i(x,y) then we can compensate f
for the effects of i.
• Technique: Project the illumination source onto a constant,
white reflective surface. The image obtained is
g(x,y)=K.i(x,y) (K is a constant).
• Divide f(x,y) by g(x,y): h(x,y)=f(x,y)/g(x,y)=r(x,y)/K
• If r(x,y) can be segmented by thresholding, then h(x,y) can
also be segmented by thresholding.
54
Thresholding
• How to find a proper value for T?
1. Select an initial estimate for T
2. Segment the image using T. This will produce two group of
pixels G1 (with gray level greater than T) and G2 (with gray
level less than T)
3. Compute average gray level values µ1 and µ2 for pixels in
regions G1 and G2
4. Compute a new threshold: T=(µ1+ µ2)/2
5. Repeat step 2 through 4 until the difference in T in
successive iterations is smaller than a predefined value
55
Optimal Global Thresholding (Otsu’s method)
• The method is optimum in the sense that it maximizes the
between-class variance, a well-known measure used in the
statistical discriminant analysis.
• Well-thresholded classes should be distinct with respect to the
intensity values of their pixels
56
Otsu’s method
• Improve the histogram shape by considering only those pixels
laying on or near the edges between objects and the
backgrounds
• Histogram would be less dependent on the relative sizes of
objects and backgrounds
• Using pixels that satisfy some simple measures based on the
gradient and Laplacian operators has a tendency to deepen the
valley between histogram peaks (i.e., pixels are selected by a
gradient/Laplacian criterion)
57
Otsu’s method
58
Region Based Segmentation
• Let R represent the entire image. Segmentation is a process
that partitions R into n subregions R1,R2,…,Rn such that:
59
Region growing by Pixel Aggregation
• Pixel aggregation: starts with a set of seed point and from
these grows regions by appending to each seed point those
neighboring pixels that have similar properties (e.g., gray-
level, texture, color).
0 0 5 6 7 A A B B B
1 1 5 8 7 A A B B B
0 1 6 7 7 A A B B B
2 0 7 6 6 A A B B B
0 1 5 6 5 A A B B B
T=3
60
Region growing by Pixel Aggregation
• Problems:
1. Selection of initial seeds that properly represent regions of interest
2. Selection of suitable properties
3. Formulation of a stopping rule.
61
Region Splitting and Merging
• Sub-divide an image into a set of disjoint regions and then
merge and/or split the regions in an attempt to satisfy the
condition (P).
• Procedure:
1. Split into 4 disjoint quadrants any region Ri where
P(Ri)= False
2. Merge any adjacent regions Rj and Rk for which P(Rj U Rk)=
True.
3. Stop when no further merging or splitting is possible.
62
Region Splitting and Merging
63