0% found this document useful (0 votes)
14 views

Segmentation

Thresholding is a technique in OpenCV used to segment images by assigning pixel values to either 0 or 255 based on whether they are above or below a threshold value. There are different thresholding techniques like binary, binary inverse, truncate, to zero, and to zero inverse. Adaptive thresholding determines different thresholds for different image regions to handle varying illumination better. Otsu's method automatically determines a threshold by choosing the value that maximizes the separability of the bimodal histogram. K-means clustering can also be used for image segmentation by grouping similar pixels into clusters.

Uploaded by

orsangobiruk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Segmentation

Thresholding is a technique in OpenCV used to segment images by assigning pixel values to either 0 or 255 based on whether they are above or below a threshold value. There are different thresholding techniques like binary, binary inverse, truncate, to zero, and to zero inverse. Adaptive thresholding determines different thresholds for different image regions to handle varying illumination better. Otsu's method automatically determines a threshold by choosing the value that maximizes the separability of the bimodal histogram. K-means clustering can also be used for image segmentation by grouping similar pixels into clusters.

Uploaded by

orsangobiruk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Dr.S.

Nagarajan

Segmentation
Introduction

2
Thresholding

• Thresholding is a technique in OpenCV, which is the assignment of


pixel values in relation to the threshold value provided. In thresholding,
each pixel value is compared with the threshold value. If the pixel value
is smaller than the threshold, it is set to 0, otherwise, it is set to a
maximum value (generally 255). Thresholding is a very popular
segmentation technique, used for separating an object considered as a
foreground from its background. A threshold is a value which has two
regions on its either side i.e. below the threshold or above the threshold.
• In Computer Vision, this technique of thresholding is done on grayscale
images. So initially, the image has to be converted in grayscale color
space.

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:

cv2.THRESH_BINARY: If pixel intensity is greater than the set threshold, value


set to 255, else set to 0 (black).
cv2.THRESH_BINARY_INV: Inverted or Opposite case of
cv2.THRESH_BINARY.
cv.THRESH_TRUNC: If pixel intensity value is greater than threshold, it is
truncated to the threshold. The pixel values are set to be the same as the
threshold. All other values remain the same.
cv.THRESH_TOZERO: Pixel intensity is set to 0, for all the pixels intensity, less
than the threshold value.
cv.THRESH_TOZERO_INV: Inverted or Opposite case of
cv2.THRESH_TOZERO.
5
Thresholding

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

• In global thresholding, we used an arbitrary chosen value as


a threshold. In contrast, Otsu's method avoids having to
choose a value and determines it automatically.
• Consider an image with only two distinct image values
(bimodal image), where the histogram would only consist of
two peaks. A good threshold would be in the middle of those
two values. Similarly, Otsu's method determines an optimal
global threshold value from the image histogram.
• In order to do so, the cv.threshold() function is used, where
cv.THRESH_OTSU is passed as an extra flag. The threshold
value can be chosen arbitrary. The algorithm then finds the
optimal threshold value which is returned as the first output.

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

In order to show only the segmented parts of the image, we perform a


bitwise AND operation on the original image (img) and the mask
(containing the outlines of detected contours).

Finally, Convert the image back to RGB to see it segmented (while being
comparable to the original image).

dst = cv2.bitwise_and(img, img, mask=mask)


segmented = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)

22
Image Segmentation using Color Masking

• Importing libraries and Images


import numpy as np
import matplotlib.pyplot as plt
from skimage.filters import threshold_otsu
import cv2
sample_image = cv2.imread('Shapes.png')
img = cv2.cvtColor(sample_image,cv2.COLOR_BGR2RGB)
plt.axis('off')
plt.imshow(img)

Presentation title 23
Image Segmentation using Color Masking

• Create Mask by Detecting Color


• We use OpenCV inRange() function that requires us to give RGB low
and high range of the color that should be detected in the image to
create the mask. For giving the RGB range it requires your
understanding of the image. Hence this approach may not be useful in
complex multicolor images.
low = np.array([0, 0, 0])
high = np.array([215, 51, 51])
mask = cv2.inRange(img, low, high)
plt.axis('off')
plt.imshow(mask)
Presentation title 24
Image Segmentation using Color Masking

• Apply the Mask


• Finally, we use the bitwise AND operation to apply our mask for
segmenting the image.
result = cv2.bitwise_and(img, img, mask=mask)
plt.axis('off')
plt.imshow(result)

Presentation title 25
https://www.geeksforgeeks.org/python-
opencv-morphological-operations/

Thank you Dr.S.Nagarajan

You might also like