MEDICAL IMAGE PROCESSING
Bich.Le
School of Biomedical Engineering,
International University
Morphological image
processing
Chapter 5
Bich.Le
Chapter learning outcomes
By the end of this subject student should be able to:
1. Present the definition of morphological operations
2. Apply the morphological operations
3. Write Python code for basic morphological operations
PRECLASS DISCUSSION
Relation of morphological image processing with other processes
PRECLASS DISCUSSION
PRECLASS DISCUSSION
PRECLASS DISCUSSION
Preview
“Morphology “ – a branch in biology that deals with the form and
structure of animals and plants.
“Mathematical Morphology” – as a tool for extracting
image components, that are useful in the representation and
description of region shape
What are the applications of Morphological Image Filtering?
boundaries extraction
skeletons
convex hull
morphological filtering
thinning
Pruning
The language of mathematical morphology is – Set theory.
Unified and powerful approach to numerous image processing
problems
Sets in mathematical morphology represent objects in an
image:
binary image (0 = black, 1 = white) :
the element of the set is the coordinates (x,y) of pixel
belong to the object Z2
gray-scaled image :
the element of the set is the coordinates (x,y) of pixel
belong to the object and the gray levels Z3
Image Morphological Attributes Extracted from Image
Image
Processing
Basic Concepts in Set Theory
Subset
Basic Concepts in Set Theory
Union
Basic Concepts in Set Theory
Intersection
disjoint / mutually exclusive
Basic Concepts in Set Theory
Complement
Basic Concepts in Set Theory
Difference
Basic Concepts in Set Theory
Reflection
Basic Concepts in Set Theory
Translation
Logic Operations Involving Binary
Pixels and Images
The principal logic operations used in image processing
are: AND, OR, NOT (COMPLEMENT).
These operations are functionally complete.
Logic operations are preformed on a pixel by pixel basis
between corresponding pixels (bitwise).
Other important logic operations :
XOR (exclusive OR), NAND (NOT-AND)
Logic operations are just a private case for a binary set
operations, such : AND – Intersection , OR – Union,
NOT-Complement.
Logic Operations
Structuring Elements (SE)
Structuring Elements(SE): small sets or subimages used to probe
an image under study for properties of interest.
The origin of the SE is indicated by a black dot.
When SE is symmetric and no dot is show the origin is at the center
of symmetry.
When SE is symmetric B = (ref lection of B)
When working with images
SE must be in a Rectangular Arrays (padding with the smallest
possible number of background elements).
Images must be in a Rectangular Arrays (padding with the
smallest possible number of background elements).
For Images a background border is provided to accommodate
the entire SE when its origin is on the border.
Structuring Elements Examples
DILATION
It is an addition (expansion) of bright pixels of the object
in a given image. It is illustrated in the image below.
Dilation
Dilation is used for expanding an element A by using structuring
element B
Dilation of A by B and is defined by the following equation:
This equation is based 0n obtaining the reflection 0f B
about its origin and shifting this reflection by z.
The dilation of A by B is the set of all displacements z,
such that and A overlap by at least one element. Based
On this interpretation the equation of (9.2-1) can be
rewritten as:
Relation to Convolution mask:
- Flipping
- Overlapping
DILATION
We have a binary image on the left, and 3X3 kernel in the middle with the
anchor point at the central pixel. We moved our kernel across the image and
obtained the output image on the right. In each position of the kernel we
calculated a local maximum. In other words, if there was at least one value
of 1 (white pixels) of the kernel that landed on the 1 (white pixel) in the
input image, the result was 1. On the other hand, if the 1s of the kernel are
overlapped only with 0s, our output was 0. We can clearly see that for dilation
the white area in the output image became larger.
a white area is 1, and a black area is 0
DILATION
We can also use a different, let’s say 3X3 kernel with only 1s. The boundaries
of the object in the output image will grow by a certain additional layer. In the
image below you can see an example of performing dilation on the original
image with 3X3 kernel that consists of all 1s. In addition, you can see the
difference between original and dilated image. The difference explains how the
newly created white pixels are distributed. Once, again, note that if we have
applied a different kernel, our result would be different.
1 1 1
1 1 1
1 1 1
Kernel
Dilation – Demonstration
Dilation – Demonstration
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
Dilation – A More interesting
Example (bridging gaps)
import cv2
import numpy as np
img = cv2.imread('j.png',0)
kernel = np.ones((3,3),np.uint8)
dilation = cv2.dilate(img,kernel,iterations
= 1)
print(kernel)
cv2.imshow('Original Image', img)
cv2.imshow('Dilated Image', dilation)
cv2.waitKey(0)
Result:
3x3 structuring element 5x5 structuring element
Erosion
In addition to dilation, we also have a complementary operation that is called an
erosion. This operation is complete inversion of the dilation. The kernel is
scanning the image, and it looks for the overlapping interval between the kernel
and the image pixel. However, opposed to the dilation, here we are computing
the local minimum. That means that only if all 1 pixels of the kernel are
overlapped with the 1 pixels of the image, the result will be 1 (white pixel).
On the other hand, in all other cases the local minimum will be equal to 0 (black
pixel). Once we process our original image with the kernel, the white area will
shrink.
Erosion
Erosion is used for shrinking of element A by using element B
Erosion for Sets A and B in Z2, is defined by the
following equation:
This equation indicates that the erosion of A by B is the set of
all points z such that B, translated by z, is contained in A.
Erosion can be used to
Shrinks or thins objects in binary images
Remove image components(how?)
Erosion is a morphological filtering operation in which
image details smaller than the structuring elements are
filtered(removed)
Erosion – Demonstrations
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
import cv2
import numpy as np
img = cv2.imread('j.png',0)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations
= 1)
print(kernel)
cv2.imshow('Original Image', img)
cv2.imshow('Eroded Image', erosion)
cv2.waitKey(0)
Duality between dilation and erosion
Dilation and erosion are duals of each other with respect to set
complementation and reflection. That,
Prove of (9.2-5)
One of the simplest uses of erosion is for eliminating irrelevant details
(in terms of size) from a binary image
Erosion vs. Dilation
Erosion:
Shrinks or thins objects in binary images
Remove image components(how?)
Erode away the boundaries of regions of foreground
pixels
Areas of foreground pixels shrink in size, and holes
within those areas become larger
Dilation:
Grows or thickens object in a binary image
Bridging gaps
Fill small holes of sufficiently small size
Opening And Closing
Opening – smoothes contours , eliminates protrusions
Closing – smoothes sections of contours, fuses narrow
breaks and long thin gulfs, eliminates small holes and
fills gaps in contours
These operations are dual to each other
These operations can be applied few times, but has
effect only once
Opening And Closing
Opening And Closing
Opening –
First – erode A by B, and then dilate the result by B
In other words, opening is the unification of all B objects
Entirely Contained in A
Morphological opening demonstration
Opening And Closing
Closing –
First – dilate A by B, and then erode the result by B
In other words, closing is the group of points, which the
intersection of object B around them with object A – is
not empty
Morphological Closing demonstration
Morphological Closing example
Use of opening and closing for morphological filtering
The Hit-or-Miss Transformation
A basic morphological tool for shape detection.
Let the origin of each shape be located at its center of
gravity.
If we want to find the location of a shape , say – X ,
at (larger) image, say – A :
Let X be enclosed by a small window, say – W.
The local background of X with respect to W is defined as
the set difference (W - X).
Apply erosion operator of A by X, will get us the set of
locations of the origin of X, such that X is completely
contained in A.
It may be also view geometrically as the set of all locations of
the origin of X at which X found a match (hit) in A.
The Hit-or-Miss Transformation
Cont.
Apply erosion operator on the complement of A by the local
background set (W – X).
Notice, that the set of locations for which X exactly fits inside
A is the intersection of these two last operators above.
This intersection is precisely the location sought.
Formally:
If B denotes the set composed of X and it’s background –
B = (B1,B2) ; B1 = X , B2 = (W-X).
The match (or set of matches) of B in A, denoted is:
The Hit-or-Miss Transformation
The Hit-or-Miss Transformation
The Hit-or-Miss Transformation
Hit-or-Miss operation
A⊛B=(A⊖B1)∩(Ac⊖B2)
Consequently, the hit-or-miss operation consists of three distinct steps:
1. Erode image A with structuring element B1.
2. Erode the complement of image A ( Ac) with structuring element B2.
3. AND results from step 1 and step 2.
Structuring elements (kernels). Left: kernel to 'hit'. Middle: kernel to 'miss'.
Right: final combined kernel
Hit-or-Miss demonstration
Hit-or-Miss demonstration
Step 1: Set Complement: Complement the binary image in Figure 6.26 by
flipping the foreground and background pixels.
Complemented Image:
Hit-or-Miss demonstration
Step 2: Erosion with the Hit Structuring Element: Perform erosion on the input
image using the Hit structuring element (B1).
Erosion Result (Hit):
Hit-or-Miss demonstration
Step 3: Erosion with the Miss Structuring Element: Perform erosion on the
complemented image using the Miss structuring element (B2).
Erosion Result (Miss):
Hit-or-Miss demonstration
Step 4: Hit-Miss-Fit Transformation: Obtain the result of the Hit-Miss-Fit
transformation by taking the intersection (logical AND) of the erosion
results (Hit and Miss).
Hit-Miss-Fit Result:
Solution
Application of Hit-Miss-Fit in feature matching
import cv2 as cv
import numpy as np
input_image = np.array((
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 0, 0, 0, 255],
[0, 255, 255, 255, 0, 0, 0, 0],
[0, 255, 255, 255, 0, 255, 0, 0],
[0, 0, 255, 0, 0, 0, 0, 0],
[0, 0, 255, 0, 0, 255, 255, 0],
[0,255, 0, 255, 0, 0, 255, 0],
[0, 255, 255, 255, 0, 0, 0, 0]), dtype="uint8")
kernel = np.array((
[0, 1, 0],
[1, -1, 1],
[0, 1, 0]), dtype="int")
output_image = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel)
rate = 50
kernel = (kernel + 1) * 127
kernel = np.uint8(kernel)
kernel = cv.resize(kernel, None, fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
cv.imshow("kernel", kernel)
cv.moveWindow("kernel", 0, 0)
input_image = cv.resize(input_image, None, fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
cv.imshow("Original", input_image)
cv.moveWindow("Original", 0, 200)
output_image = cv.resize(output_image, None , fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
cv.imshow("Hit or Miss", output_image)
cv.moveWindow("Hit or Miss", 500, 200)
cv.waitKey(0)
cv.destroyAllWindows()
Basic Morphological Algorithms
(Applications)
1 – Boundary Extraction
2 – Region Filling
3 – Extraction of Connected Components
4 – Convex Hull
5 – Thinning
6 – Thickening
7 – Skeletons
8 – Pruning
Boundary Extraction
First, erode A by B, then make set difference between
A and the erosion
The thickness of the contour depends on the size of
constructing object – B
Boundary Extraction
Boundary Extraction
import cv2
import numpy as np
img = cv2.imread('j.png',0)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
dilation = cv2.dilate(img,kernel,iterations = 1)
Boundary = dilation - erosion
print(kernel)
cv2.imshow('Original Image', img)
cv2.imshow('Eroded Image', erosion)
cv2.imshow('Dilated Image', erosion)
cv2.imshow('Boundary Image', Boundary)
cv2.waitKey(0)
Boundary Extraction
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('lung_xray.png',0)
edges = cv2.Canny(img,50,100)
cv2.imshow('Original Image', img)
cv2.imshow('Egde Image', edges)
plt.show()
cv2.waitKey(0)
Region Filling
This algorithm is based on a set of dilations,
complementation and intersections
p is the point inside the boundary(given) , with the value of 1
The process stops when
The result that given by union of A and X(k), is a set contains
the filled set and the boundary
Region Filling
import numpy as np
from PIL import Image
import cv2
def flood_fill(image, x, y, fill_value):
"""Flood fill algorithm."""
height, width = image.shape
orig_value = image[x, y]
stack = set(((x, y),))
if fill_value == orig_value:
return image
while stack:
x, y = stack.pop()
if image[x, y] == orig_value:
image[x, y] = fill_value
if x > 0:
stack.add((x - 1, y))
if x < height - 1:
stack.add((x + 1, y))
if y > 0:
stack.add((x, y - 1))
if y < width - 1:
stack.add((x, y + 1))
return image
# Example usage:
image = np.array(Image.open("j.png").convert("L")) # Open grayscale image
image_filled = flood_fill(image, x=10, y=10, fill_value=255) # Fill region starting at (10, 10) with white color
Image.fromarray(image_filled).save("filled.png") # Save filled image
#cv2.imshow('original',image)
cv2.imshow('filled',image_filled)
cv2.waitKey(0)
Extraction of Connected
Components
This algorithm extracts a component by selecting a
point on a binary object A
Works similar to region filling, but this time we use in
the conjunction the object A, instead of it’s
complement
Extraction of Connected
Components
Extraction of Connected
Components
Binary input image, (b) labelling result using 4-connectivity, and (b)
labelling result using 8-connectivity
This shows automated
inspection of chicken-
breast, that contains
bone fragment
The original image is
thresholded
We can get by using
this algorithm the
number of pixels in
each of the connected
components
Now we could know if
this food contains big
enough bones and
prevent hazards
Convex Hull
The convex hull is the shape formed by a tight rubber band
that surrounds all the nails.
Convex Hull
A is said to be convex if a straight line segment joining any
two points in A lies entirely within A
The convex hull H of set S is the smallest convex set
containing S
Convex deficiency is the set difference H-S
Useful for object description
This algorithm iteratively:
Apply the hit-or-miss transform to A with the first B element
Unions it with A
Repeat with second B element
Let The convex hull is :
https://learnopencv.com/convex-hull-using-
opencv-in-python-and-c/
Thinning
The thinning of a set A by a structuring element B, can
be defined by terms of the hit-and-miss transform:
9.5-6
A more useful expression for thinning A symmetrically
is based on a sequence of structuring elements:
{B}={B1, B2, B3, …, Bn}
Where Bi is a rotated version of Bi-1. Using this concept
we define thinning by a sequence of structuring
elements:
Thinning cont
The process is to thin by one pass with B1 , then thin
the result with one pass with B2, and so on until A is
thinned with one pass with Bn.
The entire process is repeated until no further changes
occur.
Each pass is preformed using the equation:
Thinning
Thinning
Thinning
#pip install opencv-contrib-python
import cv2
img = cv2.imread('lung_xray.png')
thinned = cv2.ximgproc.thinning(cv2.cvtColor(img,
cv2.COLOR_RGB2GRAY))
cv2.imshow('Original Image', img)
cv2.imshow('Thinned Image', thinned)
cv2.waitKey(0)
Thickening
Thickening is a morphological dual of thinning.
Definition of thickening .
As in thinning, thickening can be defined as a
sequential operation:
The structuring elements used for thickening have the
same form as in thinning, but with all 1’s and 0’s
interchanged.
Thickening
A separate algorithm for thickening is often used in
practice, Instead the usual procedure is to thin the
background of the set in question and then complement
the result.
In other words, to thicken a set A, we form C=Ac , thin C
and than form Cc.
depending on the nature of A, this procedure may result in
some disconnected points. Therefore thickening by this
procedure usually require a simple post-processing step to
remove disconnected points.
Thickening example preview
We will notice in the next example 9.22(c) that the
thinned background forms a boundary for the
thickening process, this feature does not occur in the
direct implementation of thickening
This is one of the reasons for using background
thinning to accomplish thickening.
Thickening example
Skeleton
The notion of a skeleton of a set A :S(A) is intuitively
defined, we deduce from this figure that:
a) If z is a point of S(A) and (D)z is the largest disk
centered at z and contained in A (one cannot find a
larger disk that fulfils this terms) – this disk is called
“maximum disk”.
b) The disk (D)z touches the boundary of A at two or
more different places.
Skeleton
Skeleton
The skeleton of A is defined by terms of erosions and
openings:
with
Where B is the structuring element and indicates k
successive erosions of A:
k times, and K is the last iterative step before A erodes to an empty
set, in other words:
in conclusion S(A) can be obtained as the union of skeleton
subsets Sk(A).
Skeleton Example
Skeleton
A can be also reconstructed from subsets Sk(A) by
using the equation:
Where denotes k successive dilations of
Sk(A) that is:
# Import the necessary libraries
import cv2
import numpy as np
# Read the image as a grayscale image
img = cv2.imread('lung_xray.png', 0)
# Threshold the image
ret,img = cv2.threshold(img, 127, 255, 0)
# Step 1: Create an empty skeleton
size = np.size(img)
skel = np.zeros(img.shape, np.uint8)
# Get a Cross Shaped Kernel
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
# Repeat steps 2-4
while True:
#Step 2: Open the image
open = cv2.morphologyEx(img, cv2.MORPH_OPEN, element)
#Step 3: Substract open from the original image
temp = cv2.subtract(img, open)
#Step 4: Erode the original image and refine the skeleton
eroded = cv2.erode(img, element)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
# Step 5: If there are no white pixels left ie.. the image has been completely
eroded, quit the loop
if cv2.countNonZero(img)==0:
break
# Displaying the final skeleton
cv2.imshow("Skeleton",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
Pruning
Removing parasitic component
Complement to thinning and skeletonizing by successive
elimination its end point.
Prune the outermost branches:
Prune branches that are no longer than 35 pixels:
Review
Present the definition and applications of Morphological
image processing.
Present the definition, equation, applications and process of
Morphological image processing techniques.
Present some application of Morphological image processing
techniques.
Python library for image processing
Scikit-image:
https://scikit-image.org/docs/dev/api/skimage.restoration.html
OpenCV:
Morphological operation: http://datahacker.rs/006-morphological-
transformations-with-opencv-in-python/
https://docs.opencv.org/4.5.2/d2/d96/tutorial_py_table_of_contents_imgpro
c.html
https://analyticsindiamag.com/image-processing-with-opencv-in-python/
https://likegeeks.com/python-image-processing/
https://stackabuse.com/introduction-to-image-processing-in-python-with-
opencv
Python image processing with OpenCV - Tutorial
https://www.youtube.com/watch?v=WQeoO7MI0Bs
Image databases
www.aylward.org/notes/open-access-medical-image-repositories
brain-development.org/ixi-dataset/
Points of Reflection on Today’s Class
Please briefly describe your insights on the following points from today’s
class.
•Point of Interest: Describe what you found most interesting in today’s
class.
How Interesting? (circle) Little Bit 1 2 3 4 5 Very
Much
•Muddiest Point: Describe what was confusing or needed more detail.
How Muddy? (circle) Little Bit 1 2 3 4 5 Very Much
•Learning Point: Describe what you learned about how you learn?
Letter + 4 digit number ______________ F M
Class Topic: _______________________Date: ________________
102
Bich.Le