CS231. Nhập môn Thị giác máy tính: Image segmentation
CS231. Nhập môn Thị giác máy tính: Image segmentation
CS231. Nhập môn Thị giác máy tính: Image segmentation
Image segmentation
2
Image Segmentation
• a
Feature
Image Segmentation Classification
Extraction
Input Feature Vector Object Type
Object Image
Image
3
Approaches in Image Segmentation
1. Similarity approach
2. Discontinuity approach
4
Approaches in Image Segmentation
1. Similarity approach:
• Based on detecting similarity between image
pixels to form a segment, based on a threshold.
• ML algorithms like clustering are based on this
type of approach to segment an image.
5
Approaches in Image Segmentation
2. Discontinuity approach:
• This approach relies on the discontinuity of pixel
intensity values of the image.
• Line, Point, and Edge Detection techniques use
this type of approach for obtaining intermediate
segmentation results which can be later
processed to obtain the final segmented image.
6
Groups of image segmentation
• Semantic segmentation
• Instance segmentation
7
Groups of image segmentation
8
Groups of image segmentation
9
Groups of image segmentation
10
Groups of image segmentation
11
Groups of image segmentation
• Object Detection
to classify and localize all the objects in the
image
12
Groups of image segmentation
• Semantic Segmentation
• Instance segmentation
to classify each instance of a class separately
14
Groups of image segmentation
15
Image Segmentation Techniques
16
1. THRESHOLD BASED SEGMENTATION
17
Threshold Based Segmentation
18
Threshold Based Segmentation
19
Thresholding techniques
1. Global thresholding
2. Manual thresholding
3. Adaptive Thresholding
4. Optimal Thresholding
5. Local Adaptive Thresholding
20
Thresholding techniques: Global
24
Thresholding techniques: Manual
25
Thresholding techniques: Adaptive
26
Thresholding techniques: Adaptive
27
Thresholding techniques: Adaptive
28
Thresholding techniques: Optimal
29
Thresholding techniques: Optimal
30
Thresholding techniques: Local Adaptive
31
Thresholding techniques: Local Adaptive
32
Thresholding techniques: Double
34
def drawHist(x):
hist = cv.calcHist([x], [0], None, [256], [0, 256])
plt.plot(hist)
plt.xlim([0, 256])
plt.legend(('histogram'), loc = 'upper left')
plt.show()
35
def globalThresholding(img, thres=127):
img_rst = img.copy()
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if img_rst[i][j] < thres:
img_rst[i][j] = 255
else:
img_rst[i][j] = 0
return img_rst
36
def adaptiveThresholding(f, nrow, ncol):
g = f.copy()
r = int(f.shape[0] / nrow)
c = int(f.shape[1] / ncol)
for i in range(int(nrow)):
for j in range(int(ncol)):
x = f[i * r : (i + 1) * r, j * c : (j + 1) * c]
cv2_imshow(x)
drawHist(x)
#t = np.average(x)
t = int(input())
g[i * r : (i + 1) * r, j * c : (j + 1) * c] = globalThres
holding(x, t)
return g
37
Edge Based Segmentation
38
Edge Based Segmentation
39
Edge Based Segmentation
40
Edge Based Segmentation
image.
Types of edges
42
• Tham khảo bài học về thông tin cạnh - edge
43
Clustering-Based Segmentation
44
Clustering-Based Segmentation
45
Partitioning Clustering
46
K-means
Algorithm:
1. We choose k random points in the data as the center of
clusters and assign each point to the nearest cluster by
looking at the L2 distance between the point and the
center.
2. Compute the mean of each cluster, assign that mean value
as the new center of the cluster.
3. Reassign each data point to its closest cluster center.
Repeat step 2.
The process keeps going until no new assignment is performed (so the
model is converged, there is nothing to go furthermore) or for a given
number of iteration. Therefore, K-means Clustering is an iterative method
where we can determine the iteration number too.
47
K-means
• Step 4: Repeat
K-means
• Step 4: Repeat
K-means
• Step 4: Repeat
K-means
• Ground-truth vs k-means
img = cv.imread(path_filename)
cv2_imshow(img)
from sklearn.cluster import KMeans
def Kmeans(img, n_clusters = 6):
nrow, ncol,nchl = img.shape
g = img.reshape(nrow*ncol,nchl)
k_means = KMeans(n_clusters = n_clusters,
random_state = 0).fit(g)
t = k_means.cluster_centers_[k_means.labels_]
img_res = t.reshape(nrow, ncol, nchl)
return img_res
55
K-means on image pixels
Iteration 1 Iteration 5
Final: Iteration 17
K-means on image pixels
K-means on image pixels
---------------------------------
Áp dụng thuật toán k-means
59
def Kmeans2(img, n_clusters = 6):
img_tmp = img.copy()
nrow, ncol,nchl = img.shape
g = []
for y in range(nrow):
for x in range(ncol):
tmp = [img_tmp[y,x][0], img_tmp[y,x][1], img_tmp[y,x][2], x, y]
g.append(tmp)
img_res = img_tmp
i = 0
for y in range(nrow):
for x in range(ncol):
img_res[y, x] = t[i][0:3]
i += 1
return img_res 60