Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are better segmentation algorithms included in <a href="http://scikits-image.org" rel="nofollow">http://scikits-image.org</a> , but if you want to build your own you can look at this example, based on clustering, called ICM segmentation. Specify N=4 to identify four regions.</p> <pre><code>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 &lt; 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload