Beispiel #1
0
def calculate_cluster(lena, lena_mat, quantile):
    bandwidth = estimate_bandwidth(lena_mat, quantile=quantile, n_samples=500)
    ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
    ms.fit(lena_mat)
    labels = ms.labels_
    cluster_centers = ms.cluster_centers_

    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)

    lena_clustered = lena.copy()
    lena_clustered_value = lena.copy()
    lena_mat_clustered = lena_mat.copy()
    lena_mat_clustered_value = lena_mat.copy()

    for point, pointb, value in zip(lena_mat_clustered, lena_mat_clustered_value, labels):
        point[2] = value
        pointb[2] = cluster_centers[value, 2]
        lena_clustered[point[0], point[1]] = value
        lena_clustered_value[point[0], point[1]] = cluster_centers[value, 2]

    image = {"image": lena_clustered_value,
             "quantile": quantile,
             "clusters": n_clusters_}
    return image
Beispiel #2
0
def calculate_cluster(camera, camera_mat, quantile):
    bandwidth = estimate_bandwidth(camera_mat,
                                   quantile=quantile,
                                   n_samples=500)
    ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
    ms.fit(camera_mat)
    labels = ms.labels_
    cluster_centers = ms.cluster_centers_

    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)

    camera_clustered = camera.copy()
    camera_clustered_value = camera.copy()
    camera_mat_clustered = camera_mat.copy()
    camera_mat_clustered_value = camera_mat.copy()

    for point, pointb, value in zip(camera_mat_clustered,
                                    camera_mat_clustered_value, labels):
        point[2] = value
        pointb[2] = cluster_centers[value, 2]
        camera_clustered[point[0], point[1]] = value
        camera_clustered_value[point[0], point[1]] = cluster_centers[value, 2]

    image = {
        "image": camera_clustered_value,
        "quantile": quantile,
        "clusters": n_clusters_
    }
    return image
Beispiel #3
0
def meanshift(desc, quantile, hs=16, hr=16, copy=True):
    """
    Do nothing for now...
    """
    if copy:
        desc = desc.copy()
    desc[:, :2] /= hs
    desc[:, 2:] /= hr
    bandwidth = estimate_bandwidth(desc, quantile=quantile, n_samples=500)

    ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
    ms.fit(desc)
    ms.cluster_centers_[:, :2] *= hs
    ms.cluster_centers_[:, 2:] *= hr

    return ms
from collections import defaultdict
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.cluster.mean_shift_ import estimate_bandwidth
from ipywidgets import interactive_output, IntSlider, Play, jslink
import matplotlib.pyplot as plt
from matplotlib.figure import figaspect
from matplotlib.patches import Circle

min_bin_freq = 1
X, _ = make_blobs(centers=3,
                  cluster_std=1,
                  center_box=(-7, 7),
                  shuffle=False,
                  random_state=1234)
bandwidth = estimate_bandwidth(X, n_jobs=-1)

bin_sizes = defaultdict(int)
for point in X:
    binned_point = np.round(point / bandwidth)
    bin_sizes[tuple(binned_point)] += 1

lowers = np.round(X.min(axis=0) / bandwidth)
uppers = np.round(X.max(axis=0) / bandwidth)
xbounds = np.arange(lowers[0] + 0.5, uppers[0] + 1.5) * bandwidth
ybounds = np.arange(lowers[1] + 0.5, uppers[1] + 1.5) * bandwidth

bin_seeds = np.array(
    [point for point, freq in bin_sizes.items() if freq >= min_bin_freq],
    dtype=np.float32) * bandwidth