CHAP 6 Image Segmentation
CHAP 6 Image Segmentation
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:
(supervised process)
Otsu global optimal thresholding
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
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
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
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
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
•Learning Point: Describe what you learned about how you learn?
54
Bich.Le