Skip to content

Commit f8817ad

Browse files
committed
added kmeans image segmentation tutorial
1 parent 9776921 commit f8817ad

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3131
- [How to Detect Shapes in Images in Python](https://www.thepythoncode.com/article/detect-shapes-hough-transform-opencv-python). ([code](machine-learning/shape-detection))
3232
- [How to Detect Contours in Images using OpenCV in Python](https://www.thepythoncode.com/article/contour-detection-opencv-python). ([code](machine-learning/contour-detection))
3333
- [How to Recognize Optical Characters in Images in Python](https://www.thepythoncode.com/article/optical-character-recognition-pytesseract-python). ([code](machine-learning/optical-character-recognition))
34+
- [How to Use K-Means Clustering for Image Segmentation using OpenCV in Python](https://www.thepythoncode.com/article/kmeans-for-image-segmentation-opencv-python). ([code](machine-learning/kmeans-image-segmentation))
3435
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
3536
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
3637
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).

Diff for: machine-learning/kmeans-image-segmentation/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [How to Use K-Means Clustering for Image Segmentation using OpenCV in Python](https://www.thepythoncode.com/article/kmeans-for-image-segmentation-opencv-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- If you want to perform image segmentation on the image `image.jpg`:
5+
```
6+
python kmeans_segmentation.py image.jpg
7+
```
8+
- For live camera, consider using `live_kmeans_segmentation.py`.

Diff for: machine-learning/kmeans-image-segmentation/image.jpg

438 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import cv2
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import sys
5+
6+
# read the image
7+
image = cv2.imread(sys.argv[1])
8+
9+
# convert to RGB
10+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
11+
12+
# reshape the image to a 2D array of pixels and 3 color values (RGB)
13+
pixel_values = image.reshape((-1, 3))
14+
# convert to float
15+
pixel_values = np.float32(pixel_values)
16+
17+
print(pixel_values.shape)
18+
# define stopping criteria
19+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
20+
21+
# number of clusters (K)
22+
k = 3
23+
compactness, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
24+
25+
print(labels)
26+
print(centers.shape)
27+
28+
# convert back to 8 bit values
29+
centers = np.uint8(centers)
30+
31+
# convert all pixels to the color of the centroids
32+
segmented_image = centers[labels.flatten()]
33+
34+
# reshape back to the original image dimension
35+
segmented_image = segmented_image.reshape(image.shape)
36+
37+
# show the image
38+
plt.imshow(segmented_image)
39+
plt.show()
40+
41+
# disable only the cluster number 2
42+
masked_image = np.copy(image)
43+
masked_image[labels == 2] = [0, 0, 0]
44+
45+
# show the image
46+
plt.imshow(masked_image)
47+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import cv2
2+
import numpy as np
3+
4+
cap = cv2.VideoCapture(0)
5+
k = 5
6+
7+
# define stopping criteria
8+
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
9+
10+
while True:
11+
# read the image
12+
_, image = cap.read()
13+
14+
# reshape the image to a 2D array of pixels and 3 color values (RGB)
15+
pixel_values = image.reshape((-1, 3))
16+
# convert to float
17+
pixel_values = np.float32(pixel_values)
18+
19+
# number of clusters (K)
20+
_, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
21+
22+
# convert back to 8 bit values
23+
centers = np.uint8(centers)
24+
25+
# convert all pixels to the color of the centroids
26+
segmented_image = centers[labels.flatten()]
27+
28+
# reshape back to the original image dimension
29+
segmented_image = segmented_image.reshape(image.shape)
30+
31+
# reshape labels too
32+
labels = labels.reshape(image.shape[0], image.shape[1])
33+
34+
cv2.imshow("segmented_image", segmented_image)
35+
# visualize each segment
36+
37+
if cv2.waitKey(1) == ord("q"):
38+
break
39+
40+
cap.release()
41+
cv2.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opencv-python
2+
numpy
3+
matplotlib

0 commit comments

Comments
 (0)