-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.py
65 lines (49 loc) · 2.24 KB
/
cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2
import numpy as np
from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt
def make_cluster(img_path,satellite_path):
# Load the segmented image
image = cv2.imread(img_path)
sat_img=cv2.imread(satellite_path)
# Define the color range for yellow and dark yellow in HSV
lower_yellow = np.array([20, 100, 100])
upper_yellow = np.array([30, 255, 255])
lower_dark_yellow = np.array([15, 100, 100])
upper_dark_yellow = np.array([25, 255, 255])
# Convert the image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Create masks for yellow and dark yellow
mask_yellow = cv2.inRange(hsv_image, lower_yellow, upper_yellow)
mask_dark_yellow = cv2.inRange(hsv_image, lower_dark_yellow, upper_dark_yellow)
# Combine masks
mask = cv2.bitwise_or(mask_yellow, mask_dark_yellow)
# Find the coordinates of the land pixels
land_pixels = np.column_stack(np.where(mask > 0))
# Apply DBSCAN clustering
db = DBSCAN(eps=12, min_samples=100).fit(land_pixels)
labels = db.labels_
# Number of clusters in labels, ignoring noise if present.
n_clusters = len(set(labels)) - (1 if -1 in labels else 0)
print(f'Estimated number of clusters: {n_clusters}')
# Extract the coordinates of the cluster centers
cluster_centers = []
for cluster_id in range(n_clusters):
cluster_points = land_pixels[labels == cluster_id]
centroid = cluster_points.mean(axis=0)
cluster_centers.append(centroid)
print(f'Centroid of cluster {cluster_id}: {centroid}')
cluster_centers = np.array(cluster_centers)
# Plot the clusters
plt.figure(figsize=(10, 10))
plt.imshow(cv2.cvtColor(sat_img, cv2.COLOR_BGR2RGB))
for cluster_id in range(n_clusters):
cluster_mask = (labels == cluster_id)
cluster_points = land_pixels[cluster_mask]
# plt.scatter(cluster_points[:, 1], cluster_points[:, 0], s=2, alpha=0.2,label=f'Cluster {cluster_id}')
# Plot the cluster centers with a unique marker
for i, center in enumerate(cluster_centers):
plt.scatter(center[1], center[0], c='red', marker='x', s=100, label=f'Center {i}')
plt.legend()
plt.show()
return [n_clusters,tuple(cluster_centers)]