Существуют лучшие алгоритмы сегментации, включенные в http://scikits -image.org , но если вы хотите создать свой собственный, вы можете взглянуть на этот пример, основанный на кластеризации, которая называется сегментация ICM. Укажите N = 4, чтобы определить четыре региона.
import numpy as np
from scipy.cluster.vq import kmeans2
def ICM(data, N, beta):
print "Performing ICM segmentation..."
# Initialise segmentation using kmeans
print "K-means initialisation..."
clusters, labels = kmeans2(np.ravel(data), N)
print "Iterative segmentation..."
f = data.copy()
def _minimise_cluster_distance(data, labels, N, beta):
data_flat = np.ravel(data)
cluster_means = np.array(
[np.mean(data_flat[labels == k]) for k in range(N)]
)
variance = np.sum((data_flat - cluster_means[labels])**2) \
/ data_flat.size
# How many of the 8-connected neighbouring pixels are in the
# same cluster?
count = np.zeros(data.shape + (N,), dtype=int)
count_inside = count[1:-1, 1:-1, :]
labels_img = labels.reshape(data.shape)
for k in range(N):
count_inside[..., k] += (k == labels_img[1:-1:, 2:])
count_inside[..., k] += (k == labels_img[2:, 1:-1])
count_inside[..., k] += (k == labels_img[:-2, 1:-1])
count_inside[..., k] += (k == labels_img[1:-1, :-2])
count_inside[..., k] += (k == labels_img[:-2, :-2])
count_inside[..., k] += (k == labels_img[2:, 2:])
count_inside[..., k] += (k == labels_img[:-2, 2:])
count_inside[..., k] += (k == labels_img[2:, :-2])
count = count.reshape((len(labels), N))
cluster_measure = (data_flat[:, None] - cluster_means)**2 \
- beta * variance * count
labels = np.argmin(cluster_measure, axis=1)
return cluster_means, labels
# Initialise segmentation
cluster_means, labels = _minimise_cluster_distance(f, labels, N, 0)
stable_counter = 0
old_label_diff = 0
i = 0
while stable_counter < 3:
i += 1
cluster_means, labels_ = \
_minimise_cluster_distance(f, labels, N, beta)
new_label_diff = np.sum(labels_ != labels)
if new_label_diff != old_label_diff:
stable_counter = 0
else:
stable_counter += 1
old_label_diff = new_label_diff
labels = labels_
print "Clustering converged after %d steps." % i
return labels.reshape(data.shape)