0% found this document useful (0 votes)
15 views55 pages

CHAP 6 Image Segmentation

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)
15 views55 pages

CHAP 6 Image Segmentation

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/ 55

MEDICAL IMAGE PROCESSING

Bich.Le
School of Biomedical Engineering,
International University
Image Segmentation
Chapter 6

Bich.Le
Chapter learning outcomes
By the end of this subject student should be able to:

1. Present the definition of image segmentation

2. Apply the image segmentation algorithms

3. Write Python code for some basic image segmentation applications


Pre-Class discussion

Why do we need image segmentation?


What is image segmentation?
A typical image analysis pipeline.

 Image segmentation is an aspect of image processing.


 Image segmentation is a computer vision process.
 Image segmentation is the first step in image analysis.
Image Segmentation Defined

• In computer vision, Image Segmentation is the


process of subdividing a digital image into
multiple segments(sets of pixels, also known as
superpixels)-Wikipedia,2002
• Segmentation is a process of grouping together
pixels that have similar attributes-Efford,2000
• Image Segmentation is the process of partitioning
an image into non-intersecting regions such that
each region is homogeneous and the union of no
two adjacent regions is homogeneous-Pal,1994
Simple example:
Medical imaging
Computer-guided surgery
 Da Vinci robot heart surgery
Automated inspection

BOTTLING PLANT AUTOMATION


Principal approaches
 Segmentation algorithms generally are based
on one of 2 basis properties of intensity
values
 discontinuity : to partition an image based
on sharp changes in intensity (such as edges
in an image)
 similarity : to partition an image into
regions that are similar according to a set
of predefined criteria; this includes
thresholding,region growing, region splitting
and merging.
Detection of Similarities-
Thresholding
 Thresholding is the simplest, powerful and
most frequently/widely used technique for
image segmentation
 It is useful in discriminating foreground from
the background.
 Thresholding operation is used to convert a
multilevel/gray scale image into binary image
 The advantage of obtaining first a binary
image is that it reduces the complexity of the
data and simplifies the process of recognition
and classifiction.
Thresholding
 The most common way to convert a gray-level image
into a binary image is to select a single threshold
value(T).Then all the gray level values below T will be
classified as black(0) i.e. background and those above
T will be white(1) i.e. objects.
 The Thresholding operation is a grey value remapping
operation g defined by:
0 if f(x,y) < T
g(x,y)= Where (x,y) represents a gray value/
are the coordinates of the threshold value p
1 if f(x,y) ≥ T, T represent threshold value
g(x,y) represents threshold image
f(x,y) represents gray level image pixels/
input image
32
Detection of Similarities-
Thresholding
Thresholding
 Thresholds are either global or local i.e.., they can
be constant throughout the image, or spatially
varying.
 Thresholding methods include:

 Conventional thresholding method

(supervised process)
 Otsu global optimal thresholding

Method (unsupervised process)


 Adaptive local thresholding

Threshold: valley between two adjacent peaks


(i) Axial slice of MRI brain image and (ii) automatic segmentation into five classes,
including a tumor. The segmentation was done in three dimensions. (Courtesy:
Professor Guido Gerig, Department of Computer Science, University of North
Carolina at Chapel Hill.)
(i) Original (256 × 256) image and (ii) its histogram;
(iii) segmented image of (i) using local adaptive
thresholding (with n = 45, C = 3); (iv) segmented
image of (i) using Otsu thresholding.
!"'-

. .
Threshold Selection
 Interactive selection of a threshold by the user-
possibly with the aid of the image histogram.
 Automatic methods: Automatically selected
threshold value for each image by the system
without human intervention. Automatic
methods often make use of the image
histogram to find a suitable threshold
 Advantages: simple to implement
 Disadvantages: It is sensitive to noise
Difficult to set threshold
Demonstration of thresholding
operation with Python

Bich.Le
Image threshold

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('horse.jpg',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray',vmin=0,vmax=255)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

25
Bich.Le
Image threshold

26
Bich.Le
Adaptive threshold

• For situations when the lighting is non-uniform across the


image, having only a single value of T can seriously hurt our
thresholding performance.
• The general assumption that underlies all adaptive and local
thresholding methods is that smaller regions of an image are
more likely to have approximately uniform illumination.
• Our goal in adaptive thresholding is to statistically examine
local regions of our image and determine an optimal value
of T for each region
• The optimal value of T may change for different parts of the
image.

27
Bich.Le
Adaptive threshold code

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('horse.jpg',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

28
Bich.Le
Adaptive threshold code

29
Bich.Le
Otsu thresholding

• The first is to minimize the within-class variance,


• The second is to maximize the between-class variance

30
Bich.Le
Otsu thresholding code

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('noise.jpg',0)
# global thresholding
ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]

31
Bich.Le
Otsu thresholding code

titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',


'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in range(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()

32
Bich.Le
Otsu thresholding

33
Bich.Le
 After segmenting the image, the objects can
be extracted using edge detection
techniques.
 Image segmentation techniques are
extensively used in Similarity Searches.
Comparing edge filter operators
Canny Edge Detector
1.Apply Gaussian filter to smooth the image in order to
remove the noise
2.Find the intensity gradients of the image
3.Apply gradient magnitude thresholding or lower bound
cut-off suppression to get rid of spurious response to edge
detection
4.Apply double threshold to determine potential edges
5.Track edge by hysteresis: Finalize the detection of edges
by suppressing all the other edges that are weak and not
connected to strong edges.
Canny Edge Detector Code
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('animalhuman.jpg',0)
edges = cv.Canny(img,100,200)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]),
plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap =
'gray')
plt.title('Edge Image'), plt.xticks([]),
plt.yticks([])
plt.show()
Canny Edge Track bar Code
import cv2 as cv2 while True:
import numpy as np # get Trackbar position
a = cv2.getTrackbarPos('MIN', 'Canny')
b = cv2.getTrackbarPos('MAX', 'Canny')
img = cv2.imread('gausian_noise.png', 0) # Canny Edge detection
img = cv2.resize(img, (450,300)) # arguments: image, min_val, max_val
def null(x): canny = cv2.Canny(img,a,b)
pass # display the images
# create trackbars to control threshold values cv2.imshow('Canny', canny)
cv2.namedWindow('Canny') k = cv2.waitKey(1) & 0xFF
cv2.resizeWindow('Canny', (450,300)) if k == ord('q'):
cv2.createTrackbar('MIN', 'Canny', 80,255, null) break
cv2.createTrackbar('MAX', 'Canny', 120,255, cv2.destroyAllWindows()
null)
Canny Edge Track bar Code
Image segmentation with a Watershed algorithm

Imagine that we start filling every isolated valley


with water. What will happen? Well, the rising
water from different valleys will start to merge. To
avoid that, we need to build barriers in the
locations where the water would merge.
These barriers we call watershed lines and
they are used to determine segment
boundaries. Then, we continue filling water and
building watershed until the water level reaches
the height of the highest peak. At the end of the
process, only watershed lines will be visible and
that will be the final segmentation result. So, we
can conclude that the goal of this algorithm is
to identify watershed lines.
Image segmentation with a Watershed algorithm
Image segmentation with a Watershed algorithm

We start with finding an


approximate estimate of the
coins. For that, we can use
the Otsu's binarization.
Image segmentation with a Watershed algorithm

Now we need to remove any small white noises in the image. For that we can
use morphological opening. To remove any small holes in the object, we can use
morphological closing. So, now we know for sure that region near to center of
objects are foreground and region much away from the object are background.
Only region we are not sure is the boundary region of coins.
So we need to extract the area which we are sure they are coins. Erosion
removes the boundary pixels. So whatever remaining, we can be sure it is coin.
That would work if objects were not touching each other. But since they are
touching each other, another good option would be to find the distance transform
and apply a proper threshold.
Next we need to find the area which we are sure they are not coins. For that,
we dilate the result. Dilation increases object boundary to background. This way,
we can make sure whatever region in background in result is really a background,
since boundary region is removed. See the image below.
Image segmentation with a Watershed algorithm
Image segmentation with a Watershed algorithm
Image segmentation with a Watershed algorithm
Topographical Watershed
Map for Medical Images;
(A–D) Gradient-Based
Watershed Segmentation
Outputs.
Summary

• What is segmentation?
• How to get the segmentation (principle of segmentation)?
• Applications of image segmentation.
• What is thresholding?
• How thresholding contribute to image segmentation?
• Present the thresholding selection?
• How edge detection is used with image segmentation?
• Segmentation methods and corresponding algorithm and codes.

Bich.Le
Homework

50
Bich.Le
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: ________________

54
Bich.Le

You might also like