Bai07 ImageSegmentation Part2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

TRƯỜNG ĐẠI HỌC CÔNG NGHỆ THÔNG TIN

KHOA KHOA HỌC MÁY TÍNH

CS231. Nhập môn Thị giác máy tính

Image segmentation – Part 02

Mai Tiến Dũng


Clustering-Based Segmentation
(TT)

2
K-means on image pixels

• What is wrong?
• Pixel position
– Nearby pixels are likely to
belong to the same object
– Far-away pixels are likely to
belong to different objects
• How do we incorporate
pixel position?
– Instead of representing
each pixel as (r,g,b)
– Represent each pixel as
(r,g,b,x,y)
K-means on image pixels

• Representing each pixel as (r,g,b)


– Áp dụng cho 4 ảnh: vegetables.jpg, hand.jpg,
thuoc.jpg và dogcat.jpg
– Represent each pixel as (r,g,b,x,y)
• Áp dụng cho ảnh: vegetables.jpg

4
K-means on image pixels+position
The issues with k-means

• Captures pixel similarity but


– Doesn’t capture continuity of contours
– Captures near/far relationships only weakly
– Can merge far away objects together
• Requires knowledge of k!
• Can it deal with texture?
Oversegmentation and superpixels

• We don’t know k. What is


a safe choice?
• Idea: Use large k
– Can potentially break big
objects, but will hopefully
not merge unrelated
objects
– Later processing can
decide which groups to
merge
– Called superpixels
MeanShift

from sklearn.cluster import MeanShift

https://scikit-learn.org/stable/auto_examples/cluster/plot_mean_shift.html

8
MeanShift

9
Fuzzy Clustering

• Fuzzy Clustering is a hard clustering type


while Partitioning Clustering is called soft.
• The reason for that is while in Partitioning
Clustering, 1 data point may have only in 1
cluster, in Fuzzy Clustering we have the
probabilities of a data point for each cluster
and they may belong to any cluster at this
probability level.

10
Fuzzy C-Mean Clustering

1. Initialize the probability matrix randomly. So, assign


weights to each data — cluster pair which refers to
the probability of being in cluster C for data X.
2. Calculate the center of clusters (centroids),
3. Calculate new probabilities according to the new
center of clusters.
4. Repeat 2. and 3. steps until the centers doesn’t
change or for a given iteration number

11
Fuzzy C-Mean Clustering

• We have 4 data points p1, p2, p3, p4 2-


dimensional, so we have x and y coordinates
of the points and we want to group them into 2
clusters.
• Step 1: We initialize the weight matrix
randomly:

12
Fuzzy C-Mean Clustering

• Step 2: We calculate the centroids of clusters


according to that initial probabilities with the
following formula:

Note that fuzzy parameter is something we should choose like Cluster number and
it can be chosen between 1 < m < ∞

13
Fuzzy C-Mean Clustering

• Let’s apply the formula for our example case


by choosing m = 2 :

14
Fuzzy C-Mean Clustering

• Let’s apply the formula for our example case


by choosing m = 2 :

We obtained our cluster centers, now it’s time to calculate the probabilities of points
according to that new cluster centers.
15
Fuzzy C-Mean Clustering

• Step 3: We calculate new probabilities —


weights using the following formula:

16
Fuzzy C-Mean Clustering

• Step 3:

17
fuzzy-c-means

• https://pypi.org/project/fuzzy-c-means/

• pip install fuzzy-c-means

18
fuzzy-c-means

from fcmeans import FCM

fcm = FCM(n_clusters=2)
fcm.fit(X)

fcm_labels = fcm.predict(X)

19
scikit-fuzzy

• https://pypi.org/project/scikit-fuzzy/

• pip install scikit-fuzzy


20
• https://pythonhosted.
org/scikit-
fuzzy/api/skfuzzy.clus
ter.html

21
scikit-fuzzy

22
scikit-fuzzy

23
scikit-fuzzy

24
scikit-fuzzy

25
scikit-fuzzy

• Ví dụ:
https://pythonhosted.org/scikit-
fuzzy/auto_examples/plot_cmeans.html

26
Region Growing Technique

27
Region Growing Technique

• In the case of the Region growing method, we


start with some pixel as the seed pixel and
then check the adjacent pixels. If the adjacent
pixels abide by the predefined rules, then that
pixel is added to the region of the seed pixel
and the following process continues till there is
no similarity left. This method follows the
bottom-up approach. In case of a region
growing, the preferred rule can be set as a
threshold.
28
Region Growing Technique

• For example: Consider a seed pixel of 2 in the


given image and a threshold value of 3, if a
pixel has a value greater than 3 then it will be
considered inside the seed pixel region.
Otherwise, it will be considered in another
region. Hence 2 regions are formed in the
following image based on a threshold value of
3.

29
Region Growing Technique

30
Region Splitting and Merging Technique

• In Region splitting, the whole image is first taken as


a single region. If the region does not follow the
predefined rules, then it is further divided into
multiple regions (usually 4 quadrants) and then the
predefined rules are carried out on those regions in
order to decide whether to further subdivide or to
classify that as a region. The following process
continues till there is no further division of regions
required i.e every region follows the predefined rules.

31
Region Splitting and Merging Technique

• In Region merging technique, we consider every


pixel as an individual region. We select a region as
the seed region to check if adjacent regions are
similar based on predefined rules. If they are similar,
we merge them into a single region and move ahead
in order to build the segmented regions of the whole
image. Both region splitting and region merging are
iterative processes. Usually, first region splitting is
done on an image so as to split an image into
maximum regions, and then these regions are
merged in order to form a good segmented image of
the original image. 32
Region Splitting and Merging Technique

• In case of Region splitting, the following


condition can be checked in order to decide
whether to subdivide a region or not. If the
absolute value of the difference of the
maximum and minimum pixel intensities in a
region is less than or equal to a threshold
value decided by the user, then the region
does not require further splitting.

33
Region Splitting and Merging Technique

34
Tài liệu tham khảo

• https://towardsdatascience.com/image-segmentation-part-1-9f3db1ac1c50
• https://towardsdatascience.com/image-segmentation-part-2-8959b609d268
• https://towardsdatascience.com/image-segmentation-with-clustering-
b4bbc98f2ee6
• https://towardsdatascience.com/understanding-semantic-segmentation-with-unet-
6be4f42d4b47

35
Thực hành

• Representing each pixel as (r,g,b)


– Áp dụng cho 4 ảnh: vegetables.jpg, hand.jpg,
thuoc.jpg và dogcat.jpg
• Represent each pixel as (r,g,b,x,y)
• Áp dụng cho ảnh: vegetables.jpg và thuoc.jpg
============================
Các thuật toán: MeanShift, FCM

36

You might also like