Segmentation
Segmentation
Nagarajan
Segmentation
Introduction
2
Thresholding
Presentation title 3
Thresholding
• Syntax:
cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)
• Parameters:
• -> source: Input Image array (must be in Grayscale).
• -> thresholdValue: Value of Threshold below and above which pixel
values will change accordingly.
• -> maxVal: Maximum value that can be assigned to a pixel.
• -> thresholdingTechnique: The type of thresholding to be applied.
Presentation title 4
Thresholding
The different Simple Thresholding Techniques are:
6
Adaptive Thresholding
• Simple Thresholding-not good in all cases
e.g. if an image has different lighting conditions in
different areas.
• The algorithm determines the threshold for a pixel
based on a small region around it.
• we get different thresholds for different regions of
the same image which gives better results for
images with varying illumination.
Presentation title 7
Adaptive Thresholding
adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize,
C)
• src − An object of the class Mat representing the source (input) image.
• dst − An object of the class Mat representing the destination (output)
image.
• maxValue − A variable of double type representing the value that is to be
given if pixel value is more than the threshold value.
• adaptiveMethod − A variable of integer the type representing the adaptive
method to be used. This will be either of the following two values
• ADAPTIVE_THRESH_MEAN_C − threshold value is the mean of neighborhood area.
• ADAPTIVE_THRESH_GAUSSIAN_C − threshold value is the weighted sum of
neighborhood values where weights are a Gaussian window.
• thresholdType − A variable of integer type representing the type of threshold
to be used.
• blockSize − A variable of the integer type representing size of the
pixelneighborhood used to calculate the threshold value.
Presentation C − A variable of double type representing the constant used in the both
• title 8
methods (subtracted from the mean or weighted mean).
Otsu's Binarization
Presentation title 9
K-Means Algorithm
• K-means is a clustering algorithm that is used to group data points into
clusters such that data points lying in the same group are very similar
to each other in characteristics.
• K-means algorithm can be used to find subgroups in the image and
assign the image pixel to that subgroup which results in image
segmentation.
Presentation title 10
Kmeans funcation parameters.
• Understanding Parameters
• Input parameters
1. samples : It should be of np.float32 data type, and each feature should be put in a single column.
2. nclusters(K) : Number of clusters required at end
3. criteria : It is the iteration termination criteria. When this criteria is satisfied, algorithm iteration stops.
Actually, it should be a tuple of 3 parameters. They are `( type, max_iter, epsilon )`:
1. type of termination criteria. It has 3 flags as below:
1. cv.TERM_CRITERIA_EPS - stop the algorithm iteration if specified accuracy, epsilon, is reached.
2. cv.TERM_CRITERIA_MAX_ITER - stop the algorithm after the specified number of iterations, max_iter.
3. cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER - stop the iteration when any of the above condition is met.
2. max_iter - An integer specifying maximum number of iterations.
3. epsilon - Required accuracy
4. attempts : Flag to specify the number of times the algorithm is executed using different initial labellings.
The algorithm returns the labels that yield the best compactness. This compactness is returned as output.
5. flags : This flag is used to specify how initial centers are taken. Normally two flags are used for this :
cv.KMEANS_PP_CENTERS and cv.KMEANS_RANDOM_CENTERS.
• Output parameters
1. compactness : It is the sum of squared distance from each point to their corresponding centers.
2. labels : This is the label array (same as 'code' in previous article) where each element marked '0', '1'.....
3. centers : This is array of centers of clusters.
Presentation title 11
K-Means Algorithm
• Importing libraries and Images
We start by importing the required libraries and loading the sample image.
Since OpenCV reads the image in BGR format, we convert it into RGB and
display the image.
import matplotlib.pyplot as plt
import numpy as np
import cv2
sample_image = cv2.imread('image.jpg')
img = cv2.cvtColor(sample_image,cv2.COLOR_BGR2RGB)
plt.imshow(img)
Presentation title 12
K-Means Algorithm
• Preprocessing the Image
• Next, we reshape the image into a 2D vector i.e. if the image is of the
shape (100,100,3) (width, height, channels) then it will be converted to
(10000,3). Next, convert it into the float datatype.
twoDimage = img.reshape((-1,3))
twoDimage = np.float32(twoDimage)
Presentation title 13
K-Means Algorithm
• Defining Parameters
• Now we define the criteria by which the K-means algorithm is
supposed to cluster pixels.
• The ‘K’ variable defines the no of clusters/groups that a pixel can
belong to (You can increase this value to increase the degree of
segmentation).
criteria = (cv2.TERM_CRITERIA_EPS +
cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K=2
attempts=10
Presentation title 14
K-Means Algorithm
• Applying K-Means for Image Segmentation
• The K variable randomly initiates K different clusters and the ‘center’ variable
defines the center of these clusters. The distance of each point from these centers is
computed and then they are assigned to one of the clusters. Then they are divided
into different segments according to the value of their ‘label variable’.
ret,label,center=cv2.kmeans(twoDimage,K,None,criteria,attempts,cv2.KMEANS_PP_CENTE
RS)
center = np.uint8(center)
res = center[label.flatten()]
result_image = res.reshape((img.shape))
plt.axis('off')
plt.imshow(result_image)
Presentation title 15
Image Segmentation using Contour Detection
• Importing libraries and Images
• We start by importing the required libraries and loading the sample image.
Since OpenCV reads the image in BGR format, we convert it into RGB and
display the image. For our convenience, we also resize the image to 256×256
because we will create the mask of the same size in the subsequent steps.
import matplotlib.pyplot as plt
import numpy as np
import cv2
sample_image = cv2.imread('ball.jpg')
img = cv2.cvtColor(sample_image,cv2.COLOR_BGR2RGB)
img = cv2.resize(img,(256,256))
plt.axis('off');
plt.imshow(img)
Presentation title 16
Image Segmentation using Contour Detection
• Applying Image Thresholding
• Now we convert the image to grayscale and then apply
thresholding, such that the pixel above the threshold is assigned
255 otherwise 0. The threshold value is kept as the mean of all
pixel values of the gray image. The output image shows the result
of this step.
gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
_,thresh=cv2.threshold(gray, np.mean(gray), 255,
cv2.THRESH_BINARY_INV)
plt.axis('off')
plt.imshow(thresh)
Presentation title 17
Image Segmentation using Contour Detection
• Detecting Edges
• Next, we apply canny edge detection to the thresholded image
before using the ‘cv2.dilate’ function to dilate edges detected.
edges = cv2.dilate(cv2.Canny(thresh,0,255),None)
plt.axis('off')
plt.imshow(edges)
Presentation title 18
Image Segmentation using Contour Detection
• Detecting Contours To Create Mask
• Use the OpenCV find contour function to find all the open/closed regions in
the image and store (cnt). Use the -1 subscript since the function returns a
two-element tuple.
• Pass them through the sorted function to access the largest contours first.
• Create a zero-pixel mask that has equal shape and size to the original image.
• Draw the detected contours to create the mask.
cnt= sorted(cv2.findContours(edges, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)[-2], key=cv2.contourArea)[-1]
mask = np.zeros((256,256), np.uint8)
masked = cv2.drawContours(mask, [cnt],-1, 255, -1)
plt.axis('off')
plt.imshow(masked)
Presentation title 19
Digital topology deals with properties and features of two-
dimensional (2D) or three-dimensional (3D) digital images that
cv2.findContou correspond to topological properties (e.g., connectedness) or
topological features (e.g., boundaries) of objects.
rs parameters Concepts and results of digital topology are used to specify and
justify important (low-level) image analysis algorithms, including
algorithms for thinning, border or surface tracing, counting of
components or tunnels, or region-filling.
Presentation title 20
ContourApproximationModes
CHAIN_APPROX_NONE
Python: cv.CHAIN_APPROX_NONE
stores absolutely all the contour points. That is, any 2 subsequent points
(x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal
neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1.
CHAIN_APPROX_SIMPLE
Python: cv.CHAIN_APPROX_SIMPLE
compresses horizontal, vertical, and diagonal segments and leaves only their
end points. For example, an up-right rectangular contour is encoded with 4
points.
CHAIN_APPROX_TC89_L1
Python: cv.CHAIN_APPROX_TC89_L1
applies one of the flavors of the Teh-Chin chain approximation algorithm [209]
CHAIN_APPROX_TC89_KCOS
Python: cv.CHAIN_APPROX_TC89_KCOS
applies one of the flavors of the Teh-Chin chain approximation algorithm [209]
21
Segmenting the Regions
Finally, Convert the image back to RGB to see it segmented (while being
comparable to the original image).
22
Image Segmentation using Color Masking
Presentation title 23
Image Segmentation using Color Masking
Presentation title 25
https://www.geeksforgeeks.org/python-
opencv-morphological-operations/