Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Image Segmentaion

Image Segmentaion

Mahmood Amintoosi, Fall 2026

Computer Science Dept, Ferdowsi University of Mashhad

array([[3, 4, 3, 2, 1, 0], [5, 8, 0, 7, 1, 8], [9, 5, 5, 2, 7, 6], [1, 2, 7, 4, 2, 5], [1, 4, 3, 5, 9, 4]])
<Figure size 300x400 with 1 Axes>

Image segmentation by thresholding

(numpy.ndarray, array([[False, False, False, False, False, False], [False, True, False, True, False, True], [ True, False, False, False, True, True], [False, False, True, False, False, False], [False, False, False, False, True, False]]))
<Figure size 600x400 with 2 Axes>

Using K-means

C:\Users\m.amintoosi\AppData\Roaming\Python\Python310\site-packages\sklearn\cluster\_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  super()._check_params_vs_input(X, default_n_init=10)
[1 1 1 0 0 0 0]
[[8.5]
 [2. ]]

Wrong Method!

[1 0 0 1 1]
[[7.         6.5        2.5        4.5        4.         7.        ]
 [1.66666667 3.33333333 4.33333333 3.66666667 4.         3.        ]]
C:\Users\m.amintoosi\AppData\Roaming\Python\Python310\site-packages\sklearn\cluster\_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  super()._check_params_vs_input(X, default_n_init=10)

Right Method

[0 0 0 0 0 0 1 1 0 1 0 1 1 1 1 0 1 1 0 0 1 0 0 1 0 0 0 1 1 0]
[[2.17647059]
 [6.61538462]]
C:\Users\m.amintoosi\AppData\Roaming\Python\Python310\site-packages\sklearn\cluster\_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  super()._check_params_vs_input(X, default_n_init=10)
(5, 6)
<Figure size 600x400 with 1 Axes>
array([[0, 0, 0, 0, 0, 0], [1, 1, 0, 1, 0, 1], [1, 1, 1, 0, 1, 1], [0, 0, 1, 0, 0, 1], [0, 0, 0, 1, 1, 0]])

Using cluster_centers, instead of K-means labels

(array([[2.17647059], [6.61538462]]), array([[2, 2, 2, 2, 2, 2], [6, 6, 2, 6, 2, 6], [6, 6, 6, 2, 6, 6], [2, 2, 6, 2, 2, 6], [2, 2, 2, 6, 6, 2]]))
<Figure size 600x400 with 2 Axes>
((400, 600, 3), numpy.uint8)
<Figure size 1000x500 with 2 Axes>
C:\Users\m.amintoosi\AppData\Roaming\Python\Python310\site-packages\sklearn\cluster\_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  super()._check_params_vs_input(X, default_n_init=10)
<Figure size 1000x500 with 2 Axes>

Using K-means centers

C:\Users\m.amintoosi\AppData\Roaming\Python\Python310\site-packages\sklearn\cluster\_kmeans.py:1412: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  super()._check_params_vs_input(X, default_n_init=10)
<Figure size 1000x500 with 2 Axes>