CHAP 5 Morphological Image Processing
CHAP 5 Morphological 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:
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.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:
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.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)
import numpy as np
input_image = np.array((
[0, 0, 0, 0, 0, 0, 0, 0],
kernel = np.array((
[0, 1, 0],
rate = 50
kernel = np.uint8(kernel)
cv.imshow("kernel", kernel)
cv.moveWindow("kernel", 0, 0)
cv.imshow("Original", input_image)
cv.moveWindow("Original", 0, 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
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.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
# 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
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
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:
with
Where B is the structuring element and indicates k
successive erosions of A:
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
•Learning Point: Describe what you learned about how you learn?
102
Bich.Le