Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

Department of Mechatronics

Image Acquisition and Pre-processing


MACHINE VISION AND IMAGE PROCESSING
(MTE 4075)

Year/ Semester : IVth / VII th

Name of Faculty:
Dr. Umesh Sahu
(Assistant Professor)
Manipal Institute of Technology, Manipal, India
−1−
Syllabus
Image acquisition and pre-processing: [15]
Vision system components, Image acquisition and analysis, Image
formation - Sampling & Quantization, simple operations on image,
Image enhancement Spatial Domain and frequency domain methods,
Image noise, image restoration, morphological operations,
Segmentation, image analysis, representation of regions, Feature
extraction, fundamentals of color image processing, conversion of
color spaces.

Manipal Institute of Technology, Manipal, India


−2−
Content:
▪Morphological Image Processing
–Introduction
–Set Theory Concepts
–Dilation and Erosion
–Opening and Closing
–Some Basic Morphological Algorithm (Self Learning)
–Summary of Morphological Operation on Binary Image

Manipal Institute of Technology, Manipal, India


−3−
Introduction
• “Morphological” = shape, form, structure
• Notes
– Useful for extracting and describing image component regions
– Usually applied to binary (black & white, or 0 & 1) images
– Based on set theory
• Key topics
– Set theory
– Binary operations: dilation, erosion, opening, closing
– Connected components
– Morphological algorithms
– Gray scale morphology
Manipal Institute of Technology, Manipal, India
−4−
Morphological Operations
▪ Neighborhood operations carried out in spatial domain.

▪ Originally for binary images


– extended for gray scale images

▪ Mathematical morphology used as a tool for extracting image components that are useful in
the representation and description of region shape, such as
– boundaries extraction
– skeletons
– convex hull
– morphological filtering
– thinning
– pruning

Manipal Institute of Technology, Manipal, India


−5−
Typical applications
–Objects on a conveyor belt
–Characters and text, maps
–Chromosomes
–Fingerprints
–Circuit boards
–Overhead aerial images

Manipal Institute of Technology, Manipal, India


−6−
Set Theory Concepts
• For 2D binary images, A is the (unordered) set of pairs (x,y) such that the
image value at (x,y) is equal to 1

A = (x, y) | I A (x, y) =" 1


The vertical bar means “such that”

• “Union” of two sets A and B


– The set of elements belonging to A, B, or both
A B
• “Intersection” of two sets A and B
– The set of elements belonging to both A and B
A B
• w “is an element of” set A
w A
Manipal Institute of Technology, Manipal, India
−7−
Set Theory Concepts
• “Complement”
– The set of elements that are not in A

Ac = w | w A

• “Difference” of two sets A and B


– The set of elements belonging to A, but not to B
A − B = w| w A, w B
Comma means “and”

• “Subset”
– A is a subset of B if every element of A is also in B
A B

• The empty set { } 

Manipal Institute of Technology, Manipal, India


−8−
Set Operations by Example
A = (x, y) | I A (x, y) = 1, B = (x, y) | I B (x, y) =1

Manipal Institute of Technology, Manipal, India


−9−
Translation and Reflection by examples:
Need for a reference point.
❖ Translation of A by z=(z1,z2), denoted by (A)x is defined as:
(A)z = {c| c=a+z, for a∈A}
❖ Reflection of B:
B̂ = {z|z=-b, for b∈B}
Reference point

Manipal Institute of Technology, Manipal, India


− 10 −
Basic Logic Operations:

https://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm

Manipal Institute of Technology, Manipal, India


− 11 −
Morphological Processing
▪Consists essentially of two steps:
–Probe a given object in x[ m, n] with a structuring element (SE)
–Find how the SE fits with the object

▪Choice of SE
–Different size & shape of SE yields different kinds of info about the object;
–shapes the regions in different ways

https://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm

Manipal Institute of Technology, Manipal, India


− 12 −
Structuring Element
• Morphological operations are defined based on “structuring elements”
• A structuring element is a small set or subimage, used to probe for structure

Manipal Institute of Technology, Manipal, India


− 13 −
Structuring Element
A morphological operation is based on the use of a filter like binary pattern
called the structuring element of the operation.

MATLAB: SE = strel(‘shape', r)
Creates a shape: diamond/line/square
structuring element, where r specifies
the distance from the structuring element
origin to the points of the diamond.

Manipal Institute of Technology, Manipal, India


− 14 −
eBW = imerode(oBW, se)
Erosion
▪ Erosion is used to shrink or thin objects in binary images.
▪ The erosion of a binary image A by a structuring element B is defined as:

▪ The erosion of A by B is the set of all structuring element origin locations where
the translated B does not overlap with the background of A.

▪ The basic effect of the operator on a binary image is to erode away the
boundaries of regions of foreground pixels (i.e. white pixels, typically).

▪ Thus areas of foreground pixels shrink in size, and holes within those areas
become larger.
Manipal Institute of Technology, Manipal, India
− 15 −
Erosion: Example
▪ Suppose that the structuring element is a 3×3 square .
▪ Note that in subsequent diagrams, foreground pixels are represented by 1's and background pixels
by 0's.
▪ The structuring element is now superimposed over each foreground pixel (input pixel ) in the
image.
– If all the pixels below the structuring element are foreground pixels then the input pixel retains
it’s value.
– But if any of the pixels is a background pixel then the input pixel gets the background pixel
value.

1 1 1
1 1 1
1 1 1
Structuring element

Manipal Institute of Technology, Manipal, India


− 16 −
Erosion: Example

Manipal Institute of Technology, Manipal, India


− 17 −
Erosion: Example
w w/2
w
AӨB

Radius r r
AӨB

w
w
w AӨB

Manipal Institute of Technology, Manipal, India


− 18 −
Erosion: Exercise

Manipal Institute of Technology, Manipal, India


− 19 −
Erosion: Exercise
• The erosion of set A by set (i.e., structuring element) B is

AӨB = z | (B)z  A 
• Interpretation: shift B by z, if it is completely inside A, output a 1

Origin of B
Write the
output…
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1
B
1
1

A AӨB

Manipal Institute of Technology, Manipal, India


− 20 −
Erosion: Example

https://homepages.inf.ed.ac.uk/rbf/HIPR2/erode.htm

Manipal Institute of Technology, Manipal, India


− 21 −
Erosion: Example A AӨB
B

• Erosion enlarges holes,


breaks thin parts, shrinks
object
• Is not commutative
AӨB ≠ BӨA

Example 9.1

Manipal Institute of Technology, Manipal, India


− 22 −
Dilation BW2 = imdilate(BW, SE)

▪ Dilation is an operation used to grow or thicken objects in binary images. The


dilation of a binary image A by a structuring element B is defined as:

▪ This equation is based on obtaining the reflection of B about its origin and
translation (shifting) this reflection by z. Then, the dilation of A by B is the set
of all structuring element origin locations where the reflected and
translated B overlaps with A at least one element.

Manipal Institute of Technology, Manipal, India


− 23 −
Dilation : Example
▪ Suppose that the structuring element is a 3×3 square .
▪ Note that in subsequent diagrams, foreground pixels are represented by 1's and background pixels by
0's.
▪ To compute the dilation of a binary input image by this structuring element, we superimpose the
structuring element on top of the input image so that the origin of the structuring element coincides with
the input pixel position.
▪ If the center pixel in the structuring element coincides with a foreground pixel in the image underneath,
then the input pixel is set to the foreground value.

1 1 1
1 1 1
1 1 1
Structuring element

Manipal Institute of Technology, Manipal, India


− 24 −
Dilation : Example

Manipal Institute of Technology, Manipal, India


− 25 −
Dilation : Example

Manipal Institute of Technology, Manipal, India


− 26 −
Dilation : Example
w w/2
w
AB

Radius r r

AB

w
w AB

Manipal Institute of Technology, Manipal, India


− 27 −
Dilation : Example
A AB
• Dilation fills in holes, B

thickens thin parts, grows


object

Example 9.2

Manipal Institute of Technology, Manipal, India


− 28 −
Dilation: Exercise

Manipal Institute of Technology, Manipal, India


− 29 −
Dilation: Exercise
Origin of B

1 1
1 1
B
1
1 1
1 1
1 1

A A B

Manipal Institute of Technology, Manipal, India


− 30 −
Dilation: Exercise

Origin of B

1 1
1
1

1
B B̂
1 1
1

A
A B

Manipal Institute of Technology, Manipal, India


− 31 −
Dilation: Exercise
A B
AB

Manipal Institute of Technology, Manipal, India


− 32 −
Example:

Manipal Institute of Technology, Manipal, India


− 33 −
Summary effects of dilation:

•Expand/enlarge objects in the image


•Fill gaps or bays of insufficient width
•Fill small holes of sufficiently small size
•Connects objects separated by a distance less
than the size of the window

Manipal Institute of Technology, Manipal, India


− 34 −
Erosion and Dilation in OpenCV/ MATLAB
• MATLAB
• Make a structuring element
– SE = strel()
• Dilation and erosion
– imdilate(IM, SE)
– imerode(IM, SE)

• OpenCV Python
• cv2.erode()
• cv2.dilate()
• https://www.geeksforgeeks.org/erosion-dilation-images-using-opencv-
python/

Manipal Institute of Technology, Manipal, India


− 35 −
Opening
An erosion followed by a dilation using the same structuring
element for both operations.

The opening of the dark-blue


square by a disk, resulting in
the light-blue square with
round corners.

Manipal Institute of Technology, Manipal, India


− 36 −
Opening
• Intuitive description
• Let B be a disk
• The boundary of the opening is the points in B that reach the farthest into A as
B is rolled around inside of A

Manipal Institute of Technology, Manipal, India


− 37 −
Closings
A dilation followed by an erosion
– Use the same structuring element B for both

• Take the union of all the translates of B that do not intersect A;


the closing is the complement of that

The closing of the dark-blue shape


(union of two squares) by a disk,
resulting in the union of the dark-blue
shape and the light-blue areas.

http://en.wikipedia. org/wiki/Mathematical_morphology

Manipal Institute of Technology, Manipal, India


− 38 −
Closings
• Intuitive description
– Let B be a disk
– We roll B around the outside of A
– The boundary of the closing is the points of B that just touch A

Manipal Institute of Technology, Manipal, India


− 39 −
Opening: Example

Example 9.3

Manipal Institute of Technology, Manipal, India


− 40 −
Closing: Example

Example 9.3

Manipal Institute of Technology, Manipal, India


− 41 −
Example

Example 9.4

Manipal Institute of Technology, Manipal, India


− 42 −
Exercises
▪ Apply opening to using square structuring
elements of increasing size. Compare the results
obtained with the different sizes. If your
implementation of the operator does not support
graylevel opening, threshold the input image.

▪ How can you detect the small cells in the above


example while removing the large cells Use the
closing operator with structuring elements at
different sizes in combination with some logical
operator.

Manipal Institute of Technology, Manipal, India


− 43 −
Exercises
▪ Describe two 2-D object shapes (different from the ones
shown in the image below) between which simple
opening could distinguish, when the two are mixed
together in a loose flat pile. What would be the
appropriate structuring elements to use?
▪ Can you explain why the position of the origin within the
structuring element does not affect the result of the
opening, when it does make a difference for both
erosion and dilation?

Manipal Institute of Technology, Manipal, India


− 44 −
Exercises

▪ Use closing to remove the lines from whereas


the circles should remain. Do you manage to
remove all the lines? Now use closing to
remove the circles while keeping the lines. Is
it possible to achieve this with only
one structuring element?

Manipal Institute of Technology, Manipal, India


− 45 −
Exercises

▪ Can you use closing to remove certain features (e.g. the


diagonal lines) from image
▪ Combine closing and opening to remove
the `salt'n'pepper' noise from image

Manipal Institute of Technology, Manipal, India


− 46 −
Some Basic Morphological Algorithm
• Boundary Extraction
• Hole filling
• Extraction of Connected Components
• Convex hull
• Thinning
• Thickening
• Skeletons
• Pruning
• Morphological Reconstruction
Reference of Self Learning Topic:
Gonzalez, Rafael C., and Richard E. Woods. "Digital Image Processing, New Jersey.", 2007. 3rd edition. (Chapter-9, Page-642)

Manipal Institute of Technology, Manipal, India


− 47 −
Summary of Morphological Operation on Binary
Image

Manipal Institute of Technology, Manipal, India


− 48 −
Summary of Morphological Operation on Binary
Image

Manipal Institute of Technology, Manipal, India


− 49 −
Summary of Morphological Operation on Binary
Image

Manipal Institute of Technology, Manipal, India


− 50 −
Opening and Closing in OpenCV/ MATLAB
• MATLAB
• Opening and closing
– imopen(IM, SE)
– imclose(IM, SE)

• OpenCV Python
• cv2.morphologyEx()
• https://www.geeksforgeeks.org/python-morphological-operations-in-image-
processing-opening-set-1/
• https://www.geeksforgeeks.org/python-morphological-operations-in-image-
processing-closing-set-2/

Manipal Institute of Technology, Manipal, India


− 51 −
Practice Problem
Gonzalez, Rafael C., and Richard E. Woods. "Digital Image Processing,
New Jersey.", 2007. 3rd edition.

Example 9.1, 9.2, 9.3, 9.4


9.5, 9.6, 9.7, 9.8 (Optional)

Manipal Institute of Technology, Manipal, India


− 52 −
Summary
▪We have looked at:
❑Morphological Image Processing
❑Introduction
❑Set Theory Concepts
❑Dilation and Erosion
❑Opening and Closing
❑Summary of Morphological Operation on Binary Image

Manipal Institute of Technology, Manipal, India


− 53 −
Thank you
for your attention

Manipal Institute of Technology, Manipal, India


− 54 −

You might also like