forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added kmeans image segmentation tutorial
- Loading branch information
Showing
6 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# [How to Use K-Means Clustering for Image Segmentation using OpenCV in Python](https://www.thepythoncode.com/article/kmeans-for-image-segmentation-opencv-python) | ||
To run this: | ||
- `pip3 install -r requirements.txt` | ||
- If you want to perform image segmentation on the image `image.jpg`: | ||
``` | ||
python kmeans_segmentation.py image.jpg | ||
``` | ||
- For live camera, consider using `live_kmeans_segmentation.py`. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions
47
machine-learning/kmeans-image-segmentation/kmeans_segmentation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import cv2 | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import sys | ||
|
||
# read the image | ||
image = cv2.imread(sys.argv[1]) | ||
|
||
# convert to RGB | ||
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | ||
|
||
# reshape the image to a 2D array of pixels and 3 color values (RGB) | ||
pixel_values = image.reshape((-1, 3)) | ||
# convert to float | ||
pixel_values = np.float32(pixel_values) | ||
|
||
print(pixel_values.shape) | ||
# define stopping criteria | ||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2) | ||
|
||
# number of clusters (K) | ||
k = 3 | ||
compactness, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) | ||
|
||
print(labels) | ||
print(centers.shape) | ||
|
||
# convert back to 8 bit values | ||
centers = np.uint8(centers) | ||
|
||
# convert all pixels to the color of the centroids | ||
segmented_image = centers[labels.flatten()] | ||
|
||
# reshape back to the original image dimension | ||
segmented_image = segmented_image.reshape(image.shape) | ||
|
||
# show the image | ||
plt.imshow(segmented_image) | ||
plt.show() | ||
|
||
# disable only the cluster number 2 | ||
masked_image = np.copy(image) | ||
masked_image[labels == 2] = [0, 0, 0] | ||
|
||
# show the image | ||
plt.imshow(masked_image) | ||
plt.show() |
41 changes: 41 additions & 0 deletions
41
machine-learning/kmeans-image-segmentation/live_kmeans_segmentation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import cv2 | ||
import numpy as np | ||
|
||
cap = cv2.VideoCapture(0) | ||
k = 5 | ||
|
||
# define stopping criteria | ||
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2) | ||
|
||
while True: | ||
# read the image | ||
_, image = cap.read() | ||
|
||
# reshape the image to a 2D array of pixels and 3 color values (RGB) | ||
pixel_values = image.reshape((-1, 3)) | ||
# convert to float | ||
pixel_values = np.float32(pixel_values) | ||
|
||
# number of clusters (K) | ||
_, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) | ||
|
||
# convert back to 8 bit values | ||
centers = np.uint8(centers) | ||
|
||
# convert all pixels to the color of the centroids | ||
segmented_image = centers[labels.flatten()] | ||
|
||
# reshape back to the original image dimension | ||
segmented_image = segmented_image.reshape(image.shape) | ||
|
||
# reshape labels too | ||
labels = labels.reshape(image.shape[0], image.shape[1]) | ||
|
||
cv2.imshow("segmented_image", segmented_image) | ||
# visualize each segment | ||
|
||
if cv2.waitKey(1) == ord("q"): | ||
break | ||
|
||
cap.release() | ||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
opencv-python | ||
numpy | ||
matplotlib |