pyosv.ai.clustering
1from sklearn.cluster import KMeans 2import numpy as np 3 4def pixel_clustering(img : np.ndarray, n_clusters : int = 3) -> np.ndarray: 5 ''' 6 Image pixel clustering using Support Vector machine 7 8 Parameters: 9 ----------- 10 - img : np.ndarray 11 a WxHxB image, with width W, height H and B bands (channel last) 12 - n_clusters : int 13 number of cluster to be identified 14 15 Returns: 16 -------- 17 - clusters : np.ndarray 18 clustered pixels 19 20 Usage: 21 ------ 22 23 ```python 24 import numpy as np 25 26 img = np.array( 27 [[ 28 [0.1, 0.2, 0.3], 29 [0.4, 0.5, 0.6], 30 [0.7, 0.8, 0.9] 31 ], 32 [ 33 [1.1, 1.2, 1.3], 34 [1.4, 1.5, 1.6], 35 [1.7, 1.8, 1.9] 36 ] 37 ] 38 ) 39 40 # Making channels last 41 img = np.moveaxis(img, 0, -1) 42 43 clusters = pixel_clustering(img, n_clusters = 2) 44 ``` 45 Output: 46 ------ 47 ``` 48 array([[0, 0, 0], 49 [0, 0, 1], 50 [1, 1, 1]], dtype=int32) 51 ``` 52 53 ''' 54 55 if len(img.shape) != 3: 56 raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)') 57 58 image_2D = img.reshape(img.shape[0]*img.shape[1], img.shape[2]) 59 kmeans = KMeans(n_clusters = n_clusters, random_state = 0).fit(image_2D) 60 clusters = np.array(kmeans.labels_).reshape(img.shape[0], img.shape[1]) 61 62 return clusters 63 64def conv_clustering(): pass
def
pixel_clustering(img: numpy.ndarray, n_clusters: int = 3) -> numpy.ndarray:
5def pixel_clustering(img : np.ndarray, n_clusters : int = 3) -> np.ndarray: 6 ''' 7 Image pixel clustering using Support Vector machine 8 9 Parameters: 10 ----------- 11 - img : np.ndarray 12 a WxHxB image, with width W, height H and B bands (channel last) 13 - n_clusters : int 14 number of cluster to be identified 15 16 Returns: 17 -------- 18 - clusters : np.ndarray 19 clustered pixels 20 21 Usage: 22 ------ 23 24 ```python 25 import numpy as np 26 27 img = np.array( 28 [[ 29 [0.1, 0.2, 0.3], 30 [0.4, 0.5, 0.6], 31 [0.7, 0.8, 0.9] 32 ], 33 [ 34 [1.1, 1.2, 1.3], 35 [1.4, 1.5, 1.6], 36 [1.7, 1.8, 1.9] 37 ] 38 ] 39 ) 40 41 # Making channels last 42 img = np.moveaxis(img, 0, -1) 43 44 clusters = pixel_clustering(img, n_clusters = 2) 45 ``` 46 Output: 47 ------ 48 ``` 49 array([[0, 0, 0], 50 [0, 0, 1], 51 [1, 1, 1]], dtype=int32) 52 ``` 53 54 ''' 55 56 if len(img.shape) != 3: 57 raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)') 58 59 image_2D = img.reshape(img.shape[0]*img.shape[1], img.shape[2]) 60 kmeans = KMeans(n_clusters = n_clusters, random_state = 0).fit(image_2D) 61 clusters = np.array(kmeans.labels_).reshape(img.shape[0], img.shape[1]) 62 63 return clusters
Image pixel clustering using Support Vector machine
Parameters:
- img : np.ndarray
a WxHxB image, with width W, height H and B bands (channel last)
- n_clusters : int
number of cluster to be identified
Returns:
- clusters : np.ndarray
clustered pixels
Usage:
import numpy as np
img = np.array(
[[
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9]
],
[
[1.1, 1.2, 1.3],
[1.4, 1.5, 1.6],
[1.7, 1.8, 1.9]
]
]
)
# Making channels last
img = np.moveaxis(img, 0, -1)
clusters = pixel_clustering(img, n_clusters = 2)
Output:
array([[0, 0, 0],
[0, 0, 1],
[1, 1, 1]], dtype=int32)
def
conv_clustering():
65def conv_clustering(): pass