0% found this document useful (0 votes)
13 views

Dip - 03 - 04 Morphology

The document discusses various image thresholding techniques used in digital image processing. It begins with an overview of thresholding and how it is used to convert grayscale images to binary images by separating foreground and background pixels. It then provides examples of different thresholding methods such as binary, truncated, and to-zero thresholding. Otsu's thresholding, an automatic threshold selection method, is also covered. Adaptive thresholding, which uses different thresholds for different areas of an image, is described as well. Examples are given to demonstrate how each thresholding technique can be applied to an input image.

Uploaded by

viethoang251004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Dip - 03 - 04 Morphology

The document discusses various image thresholding techniques used in digital image processing. It begins with an overview of thresholding and how it is used to convert grayscale images to binary images by separating foreground and background pixels. It then provides examples of different thresholding methods such as binary, truncated, and to-zero thresholding. Otsu's thresholding, an automatic threshold selection method, is also covered. Adaptive thresholding, which uses different thresholds for different areas of an image, is described as well. Examples are given to demonstrate how each thresholding technique can be applied to an input image.

Uploaded by

viethoang251004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

Faculty of Information Technology

Ton Duc Thang University

August 2023
Morphological Operations
 Image thresholding (binarization)
 Example: Foreground extraction

 Connected-components
 Pixel neighborhoods
 Region labeling Python
OpenCV
 Region properties
 Application: Blob-based motion detection

 Morphological operations
 Erosion/dilation/opening/closing
 Applications

@2023 Pham Van Huy and Trinh Hung Cuong 2


Binary image processing
 Binary images are common
 Intermediate abstraction in a gray-scale/color image analysis system
 Thresholding/segmentation
 Presence/absence of some image property
 Text and line graphics, document image processing

 Representation of individual pixels as 0 or 1, convention:


 foreground, object = 1 (white)
 background = 0 (black)

 Processing by logical functions is fast and simple


 Shift-invariant logical operations on binary images: “morphological” image
processing

@2023 Pham Van Huy and Trinh Hung Cuong 3


What is Thresholding?
 The simplest binarization method
 Application example:
 Separate out regions of an image corresponding to objects which we want to
analyze. This separation is based on the variation of intensity between the object
pixels and the background pixels.

 To differentiate the pixels we are interested in from the rest


(which will eventually be rejected),
 we perform a comparison of each pixel intensity value with respect to
a threshold (determined according to the problem to solve).

 Once we have separated properly the important pixels,


 we can set them with a determined value to identify them (i.e. we can assign them a
value of (black), (white) or any value that suits your needs).

@2023 Pham Van Huy and Trinh Hung Cuong 4


Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 5


Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 6


@2023 Pham Van Huy and Trinh Hung Cuong 7
Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 8


Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 9


Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 10


Thresholding
 Sometimes, a global threshold does not work

@2023 Pham Van Huy and Trinh Hung Cuong 11


Image thresholding
 Binary Thresholding
 If pixel intensity is greater than the threshold,
value set to maxval, else set to 0 (black)

 Inverted Binary Thresholding


 Inverted case of Binary Thresholding

 Truncated Thresholding
 If pixel intensity value is greater than
threshold, it is truncated to the threshold. All
other values remain the same.

@2023 Pham Van Huy and Trinh Hung Cuong 12


Image thresholding
 To-zero Thresholding
 Pixel intensity is set to 0, for all the pixels
intensity, less than the threshold value.

 Inverted To-zero Thresholding


 Inverted case of To-zero Thresholding.

@2023 Pham Van Huy and Trinh Hung Cuong 13


@2023 Pham Van Huy and Trinh Hung Cuong 14
@2023 Pham Van Huy and Trinh Hung Cuong 15
https://docs.opencv.org/3.4/db/d8e/tutorial_threshold.html
@2023 Pham Van Huy and Trinh Hung Cuong 16
https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html

@2023 Pham Van Huy and Trinh Hung Cuong 17


Thresholding examples
 threshold = 127
 maxVal = 255

@2023 Pham Van Huy and Trinh Hung Cuong 18


Input 1

 The input image contains numbers written with intensity equal to the number itself.
For ex., the pixel intensity of the number „200‟ is 200, and the intensity of the
number „32‟ is 32. No wonder „32‟ appears much darker than „200‟.

@2023 Pham Van Huy and Trinh Hung Cuong 19


Ex .1a
 For the “Input 1” image, apply thresholding methods to:
 change all numbers to WHITE color (intensity = 255).
Binary thresholding with thresh = 0 and maxVal = 255

@2023 Pham Van Huy and Trinh Hung Cuong 20


Ex .1b
 change all numbers whose values are greater than or equal to 128 into WHITE color

@2023 Pham Van Huy and Trinh Hung Cuong 21


Ex .1c
 change all numbers into the Gray color (intensity = 128)

@2023 Pham Van Huy and Trinh Hung Cuong 22


Ex .1d
 change all numbers whose values are greater than or equal to 128 into BLACK
color, and also change the background color to WHITE.

@2023 Pham Van Huy and Trinh Hung Cuong 23


Ex .1e
 change all numbers whose values are greater than 127 into the Gray color (intensity
= 127), and other numbers are unchanged.

@2023 Pham Van Huy and Trinh Hung Cuong 24


Ex .1f
 Remove all numbers whose values are less than 128, and other numbers are
remained.

@2023 Pham Van Huy and Trinh Hung Cuong 25


Ex .1g
 Remove all numbers whose values are greater than 127, and other numbers are
remained.

@2023 Pham Van Huy and Trinh Hung Cuong 26


Ex .1h
 Change numbers whose values are less than 180 into WHITE color.
 Hints: Using different thresholding techniques.

@2023 Pham Van Huy and Trinh Hung Cuong 27


Ex .1i
 Extract each number in the input image as in separated images.

@2023 Pham Van Huy and Trinh Hung Cuong 28


Otsu’s Thresholding
 determines an optimal global threshold value from the image histogram
 produces the appropriate results for bimodal images (histogram contains two clearly
expressed peaks, which represent different ranges of intensity values)

@2023 Pham Van Huy and Trinh Hung Cuong 29


Otsu’s Thresholding (ct)
https://www.scribd.com/document/4071
05529/ME5286-Lecture9-pdf

@2023 Pham Van Huy and Trinh Hung Cuong 30


Otsu’s Thresholding (ct)
https://www.scribd.com/document/4071
05529/ME5286-Lecture9-pdf

@2023 Pham Van Huy and Trinh Hung Cuong 31


Otsu’s Thresholding (ct)
https://www.scribd.com/document/4071
05529/ME5286-Lecture9-pdf

@2023 Pham Van Huy and Trinh Hung Cuong 32


Otsu’s Thresholding (ct)

https://www.scribd.com/do
cument/407105529/ME52
86-Lecture9-pdf

@2023 Pham Van Huy and Trinh Hung Cuong 33


Otsu’s Thresholding (ct)

https://www.scribd.com/docume
nt/407105529/ME5286-
Lecture9-pdf

@2023 Pham Van Huy and Trinh Hung Cuong 34


Otsu’s Thresholding (ct)

https://learnopencv.com/otsu-thresholding-with-opencv/

@2023 Pham Van Huy and Trinh Hung Cuong 35


Otsu’s Thresholding (ct)

@2023 Pham Van Huy and Trinh Hung Cuong 36


Otsu’s Thresholding (ct)

Ex. 2: Reproduce the


resulted table.

https://www.youtube.com/watch?v=Ofi1Fn18YLc

@2023 Pham Van Huy and Trinh Hung Cuong 37


Otsu’s Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 38


Ex. 3
 Let The Following Image Be Given:

 Determine The Threshold For Image Binarization Using The Otsu Method.

@2023 Pham Van Huy and Trinh Hung Cuong 39


Adaptive Thresholding
 A global threshold might not be good in all cases, e.g. if an image has
different lighting conditions in different areas
 Adaptive thresholding determines the threshold for a pixel based on a small
region around it
 get different thresholds for different regions of the same image which gives better
results for images with varying illumination

@2023 Pham Van Huy and Trinh Hung Cuong 40


Adaptive Thresholding (ct)

https://docs.opencv.org/4.x/d7/d4d/tutorial_p
y_thresholding.html

@2023 Pham Van Huy and Trinh Hung Cuong 41


Adaptive Thresholding (ct)
 Parameters:
 Threshold type: Must be either “Binary thresholding” or “inverted Binary thresholding”. This
tells us what value to assign to pixels greater/less than the threshold.
 maxValue: This is the value assigned to the pixels after thresholding. This depends on the
thresholding type. If the type is “Binary thresholding”, all the pixels greater than the
threshold are assigned this maxValue.
 adaptive Method: This tells us how the threshold is calculated from the pixel neighborhood
 MEAN_C : The threshold value is the mean of the neighbourhood area minus the
constant C
 GAUSSIAN_C : The threshold value is a gaussian-weighted sum of the neighbourhood
values minus the constant C (read more in [1], [2], [3])
 blockSize determines the size of the neighbourhood area.
 C : Constant subtracted from the mean or weighted mean.

@2023 Pham Van Huy and Trinh Hung Cuong 42


Adaptive Thresholding (ct)
 Neighborhood size has to be large enough to cover sufficient foreground and
background pixels
 Choosing regions which are too large can violate the assumption of approximately
uniform illumination
 C constant is used to properly tune the threshold value
 There may be situations where the mean value alone is not discriminating enough
between the background and foreground — thus by adding or subtracting some
value C, we can improve the results of our threshold.

 For each pixel in the image:


 Calculate the statistics (such as mean, median, etc.) from its neighbourhood. Its
statistics minus the constant C will be the threshold value for that pixel.
 Change the pixel value using the threshold

@2023 Pham Van Huy and Trinh Hung Cuong 43


Adaptive Thresholding (ct)

@2023 Pham Van Huy and Trinh Hung Cuong 44


Ex. 4
 Apply the adaptive thresholding (𝑇𝑕𝑟𝑒𝑠𝑕𝑜𝑙𝑑 𝑡𝑦𝑝𝑒 = “𝐵𝑖𝑛𝑎𝑟𝑦 𝑡𝑕𝑟𝑒𝑠𝑕𝑜𝑙𝑑𝑖𝑛𝑔”,
𝑚𝑎𝑥𝑉𝑎𝑙𝑢𝑒 = 255, 𝑚𝑒𝑡𝑕𝑜𝑑 = 𝑀𝐸𝐴𝑁_𝐶, 𝐶 = 2, 𝑏𝑙𝑜𝑐𝑘𝑆𝑖𝑧𝑒 = 3) into the following
image:
Output image 6 × 9
Input image 6 × 9 0 255 ? ? ? ? ? 0 0
0 255 ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ?
0 255 ? ? ? ? ? ? ?

@2023 Pham Van Huy and Trinh Hung Cuong 45


Morphological Image Processing
 Binary dilation and erosion
 Set-theoretic interpretation
 Opening, closing, morphological edge detectors
 Hit-miss filter
 Morphological filters for gray-level images
 Cascading dilations and erosions
 Rank filters, median filters, majority filters

@2023 Pham Van Huy and Trinh Hung Cuong 46


@2023 Pham Van Huy and Trinh Hung Cuong 47
Morphological operations

@2023 Pham Van Huy and Trinh Hung Cuong 48


Structure elements

@2023 Pham Van Huy and Trinh Hung Cuong 49


@2023 Pham Van Huy and Trinh Hung Cuong 50
@2023 Pham Van Huy and Trinh Hung Cuong 51
HIT vs FIT
https://www.cs.auckland.ac.nz/courses/compsci773s1c/lectures/ImageProcessing-html/topic4.htm

The structuring element is said to fit the Similarly, a structuring element is said
image if, for each of its pixels set to 1, to hit, or intersect, an image if, at least for
the corresponding image pixel is also 1. one of its pixels set to 1 the corresponding
image pixel is also 1.

@2023 Pham Van Huy and Trinh Hung Cuong 52


Erosion example

@2023 Pham Van Huy and Trinh Hung Cuong 53


@2023 Pham Van Huy and Trinh Hung Cuong 54
Erosion

@2023 Pham Van Huy and Trinh Hung Cuong 55


@2023 Pham Van Huy and Trinh Hung Cuong 56
@2023 Pham Van Huy and Trinh Hung Cuong 57
Binary erosion with square structuring element

 Shrinks the size of


1-valued objects
 Smoothens object
boundaries
 Removes peninsulas,
fingers, and small
objects
Original erosion with erosion with
(701x781) 3x3 structuring element 7x7 structuring element

@2023 Pham Van Huy and Trinh Hung Cuong 58


Example: blob separation/detection by erosion

Original binary Erosion by Erosion by Erosion by


image 30x30 70x70 96x96
structuring structuring structuring
Circles (792x892) element element element

@2023 Pham Van Huy and Trinh Hung Cuong 59


Example: blob separation/detection by erosion

Original binary Erosion by disk- Erosion by disk- Erosion by disk-


image
shaped shaped shaped
Circles (792x892) structuring structuring structuring
element element element
Diameter=15 Diameter=35 Diameter=48

@2023 Pham Van Huy and Trinh Hung Cuong 60


Example: chain link fence hole detection

Original grayscale Fence thresholded Erosion with


image using Otsu‟s method 151x151 “cross”
structuring element
Fence (1023 x 1173)

@2023 Pham Van Huy and Trinh Hung Cuong 61


Ex. 5
 Apply the erosion into the following image:
Input image 6 × 9 Output image 6 × 9
0 1 1 1 0 0 1 1 1 0 0 1 0 ? ? ? ? ?
0 1 1 1 0 0 1 1 0 0 0 1 0 ? ? ? ? ?
1 1 1 0 0 0 1 1 1 ? ? ? ? ? ? ? ? ?
1 1 1 0 0 1 1 1 0 ? ? ? ? ? ? ? ? ?
1 1 1 0 0 1 1 1 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 1 0 ? ? ? ? ? ? ? ? ?
0 1 0
Kernel (S) = 1 𝟏 1
0 1 0

@2023 Pham Van Huy and Trinh Hung Cuong 62


Dilation example

@2023 Pham Van Huy and Trinh Hung Cuong 63


@2023 Pham Van Huy and Trinh Hung Cuong 64
Dilation

@2023 Pham Van Huy and Trinh Hung Cuong 65


@2023 Pham Van Huy and Trinh Hung Cuong 66
@2023 Pham Van Huy and Trinh Hung Cuong 67
Binary dilation with square structuring element

 Expands the size of


1-valued objects
 Smoothes object
boundaries
 Closes holes and gaps

dilation with dilation with


Original
3x3 structuring element 7x7 structuring element
(701x781)

@2023 Pham Van Huy and Trinh Hung Cuong 68


Ex. 6
 Apply the dilation into the following image:
Input image 6 × 9 Output image 6 × 9
0 0 1 1 0 0 1 1 1 0 1 1 1 ? ? ? ? ?
0 0 0 1 0 0 1 1 0 1 0 1 1 ? ? ? ? ?
1 0 1 0 0 0 1 0 1 ? ? ? ? ? ? ? ? ?
0 0 1 0 0 0 1 0 0 ? ? ? ? ? ? ? ? ?
0 0 1 0 0 0 1 0 0 ? ? ? ? ? ? ? ? ?
0 0 1 1 0 0 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 0
Kernel (S) = 1 𝟏 1
0 1 0

@2023 Pham Van Huy and Trinh Hung Cuong 69


Relationship between dilation and erosion

@2023 Pham Van Huy and Trinh Hung Cuong 70


Opening and closing
 Goal: smoothing without size change
 Open filter: 𝑜𝑝𝑒𝑛(𝑓 , 𝑊) = 𝑑𝑖𝑙𝑎𝑡𝑒(𝑒𝑟𝑜𝑑𝑒( 𝑓 , 𝑊), 𝑊)
 Close filter: 𝑐𝑙𝑜𝑠𝑒(𝑓 , 𝑊) = 𝑒𝑟𝑜𝑑𝑒(𝑑𝑖𝑙𝑎𝑡𝑒( 𝑓 , 𝑊), 𝑊)
 Open filter and close filter are biased
 Open filter removes small 1-regions
 Close filter removes small 0-regions

 Unbiased size-preserving smoothers


     
 close  open f ,W  close open f ,W ,W

 open  close f ,W  openclose f ,W ,W 


 close-open and open-close are duals, but not inverses of each other.
@2023 Pham Van Huy and Trinh Hung Cuong 71
@2023 Pham Van Huy and Trinh Hung Cuong 72
Small hole removal by closing

Original binary Dilation Closing Difference to


mask 10x10 10x10 original mask

@2023 Pham Van Huy and Trinh Hung Cuong 73


Ex. 7
 Apply the opening into the following image:
Input image 9 × 9 Output image 9 × 9
0 0 0 0 0 0 0 0 0 Kernel (S): ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 0
0 1 1 1 1 1 1 0 0 1 𝟏 1 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 0 1 0 ? ? ? ? ? ? ? ? ?
0 1 0 1 0 1 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?

@2023 Pham Van Huy and Trinh Hung Cuong 74


Ex. 8
 Apply the closing into the following image:
Input image 9 × 9 Output image 9 × 9
0 0 0 0 0 0 0 0 0 Kernel (S): ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 0
0 1 1 1 1 1 1 0 0 1 𝟏 1 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 0 1 0 ? ? ? ? ? ? ? ? ?
0 1 0 1 0 1 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?

@2023 Pham Van Huy and Trinh Hung Cuong 75


Ex. 9
 Apply the opening and then closing into the following image:
Input image 9 × 9 Output image 9 × 9
0 0 0 0 0 0 0 0 0 Kernel (S): ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 0
0 1 1 1 1 1 1 0 0 1 𝟏 1 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 0 1 0 ? ? ? ? ? ? ? ? ?
0 1 0 1 0 1 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 0 0 0 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?
0 1 1 1 1 1 1 0 0 ? ? ? ? ? ? ? ? ?

@2023 Pham Van Huy and Trinh Hung Cuong 76


Morphological edge detectors

@2023 Pham Van Huy and Trinh Hung Cuong 77


Recognition by erosion
Binary image f
1400

2000

Structuring

44
element W
34

@2023 Pham Van Huy and Trinh Hung Cuong 78


Recognition by erosion

Structuring

44
element W
34

@2023 Pham Van Huy and Trinh Hung Cuong 79


Recognition by erosion
Binary image f
1400

2000

Structuring

62
element W
18

@2023 Pham Van Huy and Trinh Hung Cuong 80


Recognition by erosion

Structuring

62
element W
18

@2023 Pham Van Huy and Trinh Hung Cuong 81


Hit-miss filter
Binary image f
1400

2000

Structuring Structuring

62
62

element V element W
18 18

@2023 Pham Van Huy and Trinh Hung Cuong 82


Hit-miss filter

Structuring

62
element V
18

Structuring

62
element W
18

@2023 Pham Van Huy and Trinh Hung Cuong 83


 Set operations on binary images: (from left to right )
(1) a binary image f,
(2) a binary image g,
(3) the complement fc of f,
(4) the intersection f ∩ g, and
(5) the union f ∪ g

@2023 Pham Van Huy and Trinh Hung Cuong 84


Dilation/erosion for gray-level images
 Explicit decomposition into threshold sets not required in practice

@2023 Pham Van Huy and Trinh Hung Cuong 85


Dilation in Grascale image – Ex. 10

@2023 Pham Van Huy and Trinh Hung Cuong 86


1-d illustration of erosion and dilation
Structuring element length =

——Dilation
——Erosion
——Original

Structuring element:
horizontal line
Amplitude

Sample no.
@2023 Pham Van Huy and Trinh Hung Cuong 87
Image example

Original 394 x 305 Dilation 10x10 square Erosion 10x10 square

@2023 Pham Van Huy and Trinh Hung Cuong 88


Flat dilation with different structuring elements

Original Diamond Disk 20 degree line

9 points 2 horizontal lines


@2023 Pham Van Huy and Trinh Hung Cuong 89
Example: counting coins

thresholded 1 connected component

Original 20 connected
components

dilation thresholded after dilation


@2023 Pham Van Huy and Trinh Hung Cuong 90
Example: chain link fence hole detection

Original grayscale Flat erosion with Binarized by


image 151x151 “cross”
Fence (1023 x 1173) structuring element Thresholding

@2023 Pham Van Huy and Trinh Hung Cuong 91


Morphological edge detector

original f dilation g gf g  f| thresholded


|g-f

@2023 Pham Van Huy and Trinh Hung Cuong 92


Cascaded dilations

f 
dilate f , w1  
dilate f , w1, w2  dilate f , w1, w2 , w3 

@2023 Pham Van Huy and Trinh Hung Cuong 93


Cascaded erosions
 Cascaded erosions can be lumped into single erosion

 New structuring element (SE) is not the erosion of one SE by the other, but dilation.

@2023 Pham Van Huy and Trinh Hung Cuong 94


References
 https://docs.opencv.org/4.x/d7/d1b/group__imgproc__misc.html#gaa42a3e6e
f26247da787bf34030ed772c
 https://www.geeksforgeeks.org/apply-a-gauss-filter-to-an-image-with-python/
 https://theailearner.com/tag/adaptive-thresholding/
 https://dsp.stackexchange.com/questions/54375/how-to-approximate-
gaussian-kernel-for-image-blur
 https://docs.opencv.org/

@2023 Pham Van Huy and Trinh Hung Cuong 95

You might also like