Module 5
Module 5
Module 5
Image Segmentation and Representation
Region-based
Segmentation
• - Partition an image into
regions that are similar
according to a set of pre-
defined criteria.
Image Segmentation
[0 0 0 0 0 0 0 0 0]
Original Image Point Detection [0 0 0 0 1 0 0 0 0]
mask
[0 1 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0]
[0 1 0 0 1 0 0 0 0]
[0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 0]
T=120
Line detection
• A special mask is needed to detect a special type of line.
• Examples:
- Horizontal mask has high response when a line is passed through
the middle row of the mask.
• What is an edge?
- an edge is a set of connected pixels that lie on the boundary
between two regions.
- an edge is a “local” concept whereas a region boundary, owing
to the way, it is defined, is a more global idea.
Image Gradient (1st order derivatives)
Image Gradient Operator
Consider 3 x 3 neighbourhood with z5 as the point of consideration
z1 z2 z3
z4 z5 z6
z7 z8 z9
z1 z2 z3
z4 z5 z6
z7 z8 z9
Prewitt Operator
Sobel Operator
• Higher weights are assigned to the pixel close to the candidate pixels.
x-1 x x+1
y-1 z1 z2 z3
y z4 z5 z6
y+1 z7 z8 z9
Sobel Operator
• Higher weights are assigned to the pixel close to the candidate pixels.
x-1 x x+1
y-1 z1 z2 z3
y z4 z5 z6
y+1 z7 z8 z9
Gradient (1st order derivatives) Operators and Masks
Example
Detect edge in the following image using strength (magnitude) and direction of gradient.
Use Prewitt operator.
M(x, y) Find an edge in horizontal direction
M(x, y) Find an edge in vertical direction
• Gx =22
• 𝐺𝑦=171
• Magnitude ≈ 172.4 (strong edge)
• Direction ≈ 83° (almost vertical edge)
• Since Gy is much larger than 𝐺𝑥
• change is mostly left ↔ right
• so edge is vertical
Prewitt and Sobel masks for Diagonal edges
Example of use of Gradient
Second order derivative
• In first order derivative, edges are considered to be present when edge
magnitude is large compared to the threshold value
• In case of second derivative, edge is present at that location where the
second derivative is zero
• It is like zero crossing, which can be observed as a sign change
Zero crossing:
Zero crossing is a situation
20 40 20 where pixels in
neighbourhood differ from
each other pixel in sign
Laplacian operator
Laplacian (Second order derivative) for edge detection
LoG approximation kernel where σ = 1.4. This is just an example of one convolution kernel that can be
used.
apply line detection mask on following image
Example
1. If Gx=12 Gy=5, find the gradient magnitude.
2. If Gx=6, Gy=6, find the edge direction.
3. Apply sobel operator and interpret the result
10 20 30
20 30 40
30 40 50
4. Apply the Prewitt operator to the following image and find 𝐺𝑥, Gy,
and gradient magnitude.
10 10 10
20 20 20
30 30 30
Edge Linking
• An edge detection algorithm (Roberts, Sobel, Prewitt, LoG etc.) enhance
the edges. When implemented, there are normally breaks in lines. Due
to this reason, these are generally followed by linking procedures to
assemble edge pixels into meaningful edges.
Input Gradient Edge A good edge
Image operator Linking output image
Image Space
Hough transform x 1 3
• For Equation c = 2-m y 2 4
Equation of line c = 2-m c = 4-3m
• If c=0, m=2 and
m=0, c=2
• For Equation c = 4-3m 7
• If c=0, m=4/3= 1.33 and 6
5
m=0, c=4
c 4
3
• (m,c)=(1,1) 2
(1,1)
• y=mx+c 1
• When x=1 y=1*1+1 =2 1 2 3 4 5 6 7 8 9
m
• When x=3 y=1*3+1 =4
Hough Space
Hough transform
• Given 5 points use Hough transform to draw a line joining these
points. (1,4) (2,3) (3,1) (4,1) (5,0)
x 1 2 3 4 5
y 4 3 1 1 0
• y = mx + c
• c = y - mx
x 1 2 3 4 5
y 4 3 1 1 0
Equation
c = 4-m c = 3-2m c = 1-3m c = 1-4m c = 0-5m
of line
Hough transform
• Given 5 points use Hough transform to draw a line joining these
points. (1,4) (2,3) (3,1) (4,1) (5,0)
x 1 2 3 4 5
y 4 3 1 1 0
• y = mx + c
• c = y - mx
x 1 2 3 4 5
y 4 3 1 1 0
. . .
. . .
p q
. . .
Edge Element Costs
• where max(I) is the highest intensity value in the image, f(p) is the
intensity value of p, and f(q) is the intensity value of q.
q
• A cost (p, q) = max I - [f(p) –f(q))
• (5,6)= 7-[5-6]=7+1=8
5 6 1
• (6,7) = 7-[6-7] = 8 6 7 0
• (7,1) =7-(7-1)=1
• total cost A = 8+8+1=17 7 1 3
• B=16
5 6 1
• C=23, D=38,E=ll, F=36
• G=11 6 7 0
• H=4
• Least cost path is H... 7 1 3
• B=16, C=23, D=38, E=11 , F=36
• G=11, H=4
• Least cost path is H
• Using graph theoretic approach find the edge with least cost
1 2 0
2 6 4
4 3 3
Thresholding segmentation
Contrast stretching Thresholding
T function T function
Segmentation by thresholding
• Thresholding is the simplest segmentation method. Thresholding is used to
produce regions of similarity within the given image, based on some threshold
criteria T. Hence it partitions/segments an image into different objects.
• The pixels are partitioned depending on their intensity value.
• Single thresholding:
g(x, y) = 1, if f (x, y) > T
0, if f (x, y) ≤ T
• Multiple thresholding:
g(x, y) = a, if f (x, y) > T2
b, if T1 < f (x, y) ≤ T2
c, if f (x, y) ≤ T1
Thresholding types:
• Thresholding operation can be thought of as an operation, such that,
T=T[x, y, p(x, y), f(x, y)], where f(x, y) is the gray level of input pixel at (x, y) and
p(x, y) denotes some local property of this point (x, y), e.g. the average level of a
neighborhood centered on (x, y).
Thresholding
Global Local
T = T [ f(x, y) ] T=T [ p(x, y), f(x, y) ]
Global Thresholding
• If the thresholding operation depends only on the gray scale value, it is
called global thresholding.
• where P(Rk): a logical predicate defined over the points in set Rk.
• For example: P(Rk)=TRUE if all pixels in Rk have the same gray level.
Region-Growing
• Edges and thresholds sometimes do not give good results for segmentation.
Thresholding still produces isolated image.
• Region growing algorithms works on principle of similarity.
• It states that a region is coherent if all the pixels of that region are
homogeneous with respect to some characteristics such as colour, intensity,
texture, or other statistical properties.
• Thus idea is to pick a pixel inside a region of interest as a starting point (also
known as a seed point) and allowing it to grow.
• Seed point is compared with its neighbours, and if the properties match , they
are merged together.
• This process is repeated till the regions converge to an extent that no further
merging is possible.
Region-Growing Algorithm
• It is a process of grouping the pixels or subregions to get a bigger
region present in an image.
• Selection of the initial seed: Initial seed that represent the ROI
should be given typically by the user. Can be chosen automatically.
The seeds can be either single or multiple.
• Seed growing criteria: Similarity criterion denotes the minimum
difference in the grey levels or the average of the set of pixels. Thus,
the initial seed ‘grows’ by adding the neighbours if they share the
same properties as the initial seed.
• Terminate process: If further growing is not possible then terminate
region growing process.
• Threshold=3
Example
• Apply region growing on following image with initial point at (2,2)
and threshold value as 2. Use 4 connectivity
0 1 2 0
• 7-6=1 <=2
2 5 6 1
• 7-5 =2 <=2 b b b b 1 4 7 3
0 0 0 0 b a a b 0 2 5 1
0 1 1 0 b a a b
0 1 1 0 b b a b
0 0 1 0
Example
• Apply region growing on following image with pixel value 6 and
threshold value as 3.
5 6 6 7 6 7 6 6
a1 a1 a1 a1 a1 a1 a1 a1 6 7 6 7 5 5 4 7
a1 a1 a1 a1 a1 a1 a1 a1
1 1 1 1 0 0 1 1
6 6 4 4 3 2 5 6
a a a a b b a a
1 1 1 0 0 0 1 1 5 4 5 4 2 3 4 6
a a a b b b a a
0 0 0
b b b b b b a
0 0 0 1 1
a 0 3 2 3 3 2 4 7
0 0 0 0 0 0 1 1
b1 b0 b0 b0 b0 b0 a1 a1 0 0 0 0 2 2 5 6
a1 b0 b0 b0 b0 b0 a0 a0 1 1 0 1 0 3 4 4
a b b b b b b b 1 0 1 0 2 3 5 4
Region Splitting
• Entire image is assumed as a single region. Then the
homogeneity(similarity) test is applied, where pixels that are similar are
grouped together. If the conditions are not met, then the regions are split
into four quadrants, else leave the region as it is.
• Split and continue the subdivision process until some stopping criteria is
fulfilled. The stopping criteria often occur at a stage where no further
splitting is possible.
• This process is repeated for each quadrant until all the regions meet the
required homogeneity criteria. If the regions are too small, then the
division process is stopped.
Region Splitting
• To explain this in terms of graph theory, we call each region a node.
• This technique has a convenient representation in the form of a
quadtree structure.
• Quadtree: a tree in which nodes have exactly four descendants.
Region Merging
• Region merging is opposite to region splitting.
• Here we start from the pixel level and consider each of them as a
homogeneous region.
• At any level of merging, we check if the four adjacent regions satisfy the
homogeneity property. If yes, they are merged to form a bigger region,
otherwise the regions are left as they are.
• This is repeated until no further region exits that requires merging.
Region Splitting and Merging
• Splitting or merging might not produce good results when applied
separately. Better results can be obtained by interleaving merge and
split operations.
• The split and merge procedure is as follows:
• First there is a large region (possible the entire image).
a) Split into four disjoint quadrants any region Ri for which P(Ri) =
FALSE.
b) Merge any adjacent regions Rj and Rk for which P(Rj U Rk) =
TRUE. (the quadtree structure may not be preserved).
c) Stop when no further merging or splitting is possible.
• Apply splitting and merging on following image with threshold value
as 3. Region R
• Threshold =3
5 6 6 6 7 7 6 6
• Condition Max-Min<=T
• If condition is not satisfied then 6 7 6 7 5 5 4 7
split image in 4 quadrant 6 6 4 4 3 2 5 6
• Repeat the step
5 4 5 4 2 3 4 6
• Region R
• Max=7 0 3 2 3 3 2 4 7
• Min=0 0 0 0 0 2 2 5 6
• Difference= 7
1 1 0 1 0 3 4 4
• 7>3 then split
1 0 1 0 2 3 5 4
• Apply splitting and merging on following image with threshold value as 3.
• Threshold =3 Region R
Region R1 Region R2
• Condition Max-Min<=3
5 6 6 6 7 7 6 6
• R1 Region R21 Region R22
• Max=7 6 7 6 7 5 5 4 7
• Min=4
• Difference= 3 6 6 4 4 3 2 5 6
Region R23 Region R24
• 3<=3 5 4 5 4 2 3 4 6
• R2 R21:7-5=2
0 3 2 3 3 2 4 7
• Max=7 R22:7-4=3
• Min=2 R23:3-2=1 0 0 0 0 2 2 5 6
• Difference= 5 R24:6-4=1
• 5>=3 1 1 0 1 0 3 4 4
• Split 1 0 1 0 2 3 5 4
Region R3 Region R4
• Apply splitting and merging on following image with threshold value as 3.
• Threshold =3 Region R
Region R1 Region R2
• Condition Max-Min<=3
5 6 6 6 7 7 6 6
• R3 Region R21 Region R22
• Max=3 6 7 6 7 5 5 4 7
• Min=0
• Difference= 3 6 6 4 4 3 2 5 6
Region R23 Region R24
• 3<=3 5 4 5 4 2 3 4 6
• R4
0 3 2 3 3 2 4 7
• Max=7 R41:3-2=1
• Min=0 R42:7-4=3 0 0 0 0 2 2 5 6
• Difference= 7 R43:3-0=3
• 5>=3 1 1 0 1 0 3 4 4
R44:5-4=1
• Split 1 0 1 0 2 3 5 4
Region R3 Region R4
• Let us take region a and region b Merging
• If Condition: Max(a)- Min(b)<=T and Max(b)- Min(a)<=T then merge
• Check R1 and R3
• R1 : Max: 7 Min=4 Region R
• R3: Max: 3 Min=0 Region R1 Region R2
• Max(R1)-Min(R3)= 7-0>3
• Thus can not merge R1 and R3 5 6 6 6 7 7 6 6
Region R21 Region R22
• Check R1 and R21 6 7 6 7 5 5 4 7
• R1 : Max: 7 Min=4 Region Ra
• R21: Max: 7 Min=5
• Max(R1)-Min(R21)= 7-5=2<=3
6 6 4 4 3 2 5 6
Region R23 Region R24
• Max(R21)-Min(R1)= 7-4=3<=3
• Thus can merge R1 and R21 5 4 5 4 2 3 4 6
• Check Ra and R22 0 3 2 3 3 2 4 7
• R1 : Max: 7 Min=4
• R21: Max: 7 Min=4 0 0 0 0 2 2 5 6
• Max(R1)-Min(R21)= 7-4=3<=3
• Max(R21)-Min(R1)= 7-4=3<=3
• Thus can merge Ra and R22
1 1 0 1 0 3 4 4
1 0 1 0 2 3 5 4
Region R3 Region R4
Merging
Region R
Region R1 Region R2
5 6 6 6 7 7 6 6
Region R21 Region R22
6 7 6 7 5 5 4 7
Region Ra
6 6 4 4 3 2 5 6
Region R23 Region R24
5 4 5 4 2 3 4 6
0 3 2 3 3 2 4 7
0 0 0 0 2 2 5 6
Region Rb
1 1 0 1 0 3 4 4
1 0 1 0 2 3 5 4
Region R3 Region R4
Representation
• Representation: Make a decision whether the data should be
represented as a boundary or as a complete region. It
almost always follows the output of a segmentation stage.
- Boundary Representation: Focus on external shape
characteristics, such as corners and inflections.
- Region Representation: Focus on internal properties, such as texture
or skeleton shape.
Steps of the Boundary following process:
1) Select a Starting Point: The process begins by
selecting a starting point on the object's
boundary. This point can be any pixel
belonging to the object.
2) Tracing the Boundary: Starting from the
chosen starting point, the algorithm follows a
set of predefined rules to trace the boundary.
The rules determine the order in which
neighboring pixels are examined and selected
to continue tracing.
1) For the 4-connected boundary following,
the algorithm examines the four immediate
neighbors (top, bottom, left, right).
2) For the 8-connected boundary following,
the algorithm examines all eight neighbors,
including the diagonals.
Steps of the Boundary following process:
3) Decision Rules: At each step of the tracing
process, the algorithm makes decisions based on
the following rules:
• It selects the next boundary pixel from the
adjacent neighbors in a specific order (e.g.,
clockwise or counterclockwise).
• It ensures that the selected neighbor is part of
the object (i.e., its pixel intensity corresponds
to the object's intensity).
4) Tracing Completion: The process continues
until the algorithm returns to the starting point,
forming a closed loop and completing the
boundary trace.
5) Representation: As the boundary is traced, the
coordinates of the boundary pixels are recorded.
These coordinates can then be used to represent
the contour of the object.
Chain codes
• The boundary code formed as a sequence of such directional numbers is
referred to as a Freeman chain code.
• Different starting points gives different chain codes.
• To normalize the chain codes with respect to starting point, we have to treat
the chain codes as a circular sequence.
• Also, whichever number gives the smaller magnitude will be considered as
the chain code.
Advantages of Chain codes:
• Compactness: Chain codes can significantly reduce the storage space
required to represent a boundary compared to storing the individual
coordinates of boundary pixels.
• Rotation Invariance: Chain codes are rotation invariant since they represent
only the relative directions between boundary pixels, not their absolute
positions.