Python:点聚类/平均

时间:2019-03-13 12:25:52

标签: python cluster-analysis k-means

我有一个检测器,该检测器返回检测到的对象的边界框中心,在大多数情况下它工作正常。但是,我想做的是考虑10帧而不是1帧进行检测,这样我就可以消除更多的误报。

检测器正常工作的方式如下:

1. Get a frame.
2. Conduct the algorithm. 
3. Record the centers into a dictionary per each frame. 

我认为有助于减少误报的方法是:

1. Set up a loop of 10: 
   1. Get a frame.
   2. Conduct the algorithm. 
   3. Record the centers into a dictionary per each frame.
2. Loop over the recorded points after every 10 frames.
3. Use a clustering algorithm or simple distance averaging
4. Get the final centers.

因此,我已经实现了一些这种逻辑。我在步骤1.3中,我需要找到一种对坐标进行分组并最终确定估算值的方法。

10帧之后,我的字典保存了这些值(无法全部粘贴):

      (4067.0, 527.0): ['torx8', 'screw8'], 
      (4053.0, 527.0): ['torx8', 'screw1'], 
      (2627.0, 707.0): ['torx8', 'screw12'], 
      (3453.0, 840.0): ['torx6', 'screw14'], 
      (3633.0, 1373.0): ['torx6', 'screw15'], 
      (3440.0, 840.0): ['torx6', 'screw14'], 
      (3447.0, 840.0): ['torx6', 'screw14'], 
      (1660.0, 1707.0): ['torx8', 'screw3'], 
      (2633.0, 700.0): ['torx8', 'screw7'], 
      (2627.0, 693.0): ['torx8', 'screw8'], 
      (4060.0, 533.0): ['torx8', 'screw6'], 
      (3627.0, 1367.0): ['torx6', 'screw13'], 
      (2600.0, 680.0): ['torx8', 'screw15'], 
      (2607.0, 680.0): ['torx8', 'screw7']

您会注意到,这些点中的大多数已经是相同的点,只是有一点点像素偏移,这就是为什么我试图找到一种方法来消除所谓的重复项。

是否有一种聪明有效的方法来解决此问题?我首先想到的是k均值聚类,但是我不确定这是否适合此问题。

有人有类似的经历吗?

编辑:好的,我取得了一些进步,并且能够使用层次聚类对点进行聚类,因为在我的情况下,我不了解聚类数。因此,需要一个近似值。

# cluster now
points = StandardScaler().fit_transform(points)   
db = self.dbscan.fit(points)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_

# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(db.labels_)) - (1 if -1 in db.labels_ else 0)
n_noise_ = list(db.labels_).count(-1)

# Black removed and is used for noise instead.
unique_labels = set(labels)
colors = [plt.cm.Spectral(each)
        for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
    if k == -1:
        # Black used for noise.
        col = [0, 0, 0, 1]

    class_member_mask = (labels == k)

    xy = points[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
            markeredgecolor='k', markersize=14)

    xy = points[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
            markeredgecolor='k', markersize=6)

plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()

Clustering Result

效果很好。我可以消除误报(请参见黑点),但是,我仍然不知道如何获得每个群集的平均值。就像在找到群集之后,如何在每个群集上循环并平均所有X,Y值? (显然,在StandardScaler().fit_transform(points)之前,我失去了像素坐标,因此它们在负1到1之间是合适的。)

1 个答案:

答案 0 :(得分:0)

好吧,终于,我明白了。因为我还需要将点保持在原始比例(而不是-1和1之间),所以我还必须进行重新缩放。无论如何,这是完整的魔力:

def cluster_dbscan(self, points, visualize=False):

        # scale the points between -1 and 1
        scaler = StandardScaler()
        scaled_points = scaler.fit_transform(points)

        # cluster
        db = DBSCAN(eps=self.clustering_epsilon, min_samples=self.clustering_min_samples, metric='euclidean')
        db.fit(scaled_points)
        core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
        core_samples_mask[db.core_sample_indices_] = True

        # Number of clusters in labels, ignoring noise if present.
        n_clusters_ = len(set(db.labels_)) - (1 if -1 in db.labels_ else 0)
        n_noise_ = list(db.labels_).count(-1)

        if (visualize == True):
            # Black removed and is used for noise instead.
            unique_labels = set(db.labels_)
            colors = [plt.cm.Spectral(each)
                    for each in np.linspace(0, 1, len(unique_labels))]
            for k, col in zip(unique_labels, colors):
                if k == -1:
                    # Black used for noise.
                    col = [0, 0, 0, 1]

                class_member_mask = (db.labels_ == k)

                xy = scaled_points[class_member_mask & core_samples_mask]
                plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
                        markeredgecolor='k', markersize=14)

                xy = scaled_points[class_member_mask & ~core_samples_mask]
                plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
                        markeredgecolor='k', markersize=6)

            plt.title('Estimated number of clusters: %d' % n_clusters_)
            plt.show()

        # back to original scale
        points = scaler.inverse_transform(scaled_points) 

        # loop over the clusters, get the centers
        centers = np.zeros((n_clusters_, 2)) # for x and y
        for i in range(0, n_clusters_):
            cluster_points = points[db.labels_ == i]
            cluster_mean = np.mean(cluster_points, axis=0)
            centers[i, :] = cluster_mean

        # we need the original points
        return centers