将从子集计算的kmeans应用于全d

2024-04-20 06:20:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有层析数据集,我想分为不同的部分,通过k-均值聚类。 由于数据集相当大,我计算数据子集的k均值。 现在我想将计算出的k均值应用到数据集的更大部分,但我似乎无法使其正确工作,分割没有正确应用。你知道吗

我加载图像的一个子集,如下所示:

import glob
import imageio
import numpy
filenames = glob.glob(os.path.join(FolderToRead, '*rec0*.tif'))
vol_subset = numpy.stack([imageio.imread(rec) for rec in filenames[::50]], 0)

k-均值聚类计算如下:

import sklearn.cluster
kmeans_volume = sklearn.cluster.MiniBatchKMeans(n_clusters=6, batch_size=2**11)
subset_clustered = kmeans_volume.fit_predict(numpy.array(vol_subset).reshape(-1,1))
subset_clustered.shape = numpy.shape(vol_subset)

标签看起来很棒,标签1是骨头,标签3是植入物,标签5是骨头中的血管。你知道吗

for c, img in enumerate(subset_clustered):
    for d, cluster in enumerate(range(number_of_clusters)):
        plt.subplot(1, number_of_clusters, d+1)
        # Show original image
        plt.imshow(img)
        # Overlay label image
        plt.imshow(numpy.ma.masked_where(img != d, img), cmap='jet_r')
        plt.title('Image %s/%s, Label %s' % (c + 1, len(vol_clustered), d))
        plt.show()

Labels applied to subset

现在我已经计算了数据子集的k均值,我想将它们应用到完整的数据集。 我试图这样做,但标签似乎不一致。你知道吗

# Apply segmentation calculated above
for c, r in enumerate(reconstructions):
    # Read in all files subsequently
    reconstruction = imageio.imread(r)
    # Label the images with the kmeans calculated from a subset of the images
    clustered_rec = kmeans_volume.fit_predict(reconstruction.reshape(-1, 1))
    clustered_rec.shape = numpy.shape(reconstruction)
    # Write out the images
    imageio.imwrite('filename' + c + '.png, numpy.uint8(clustered_rec == 3) * 255 )  # 3 being the screw label

下图显示了上面脚本的裁剪输出。 左面板上的一个图像(中间的五个斑点)上的血管被正确地标记为5,右面板上的下一个图像上的血管被标记为1,这是错误的。。。你知道吗

Wrongly applied labels

如果有人指出我做错了什么,我将不胜感激。 我希望我不必计算完整数据集上的k均值,因为有2700个TIFF图像,每个图像的大小为1944x1944像素。。。你知道吗


Tags: the数据in图像importnumpyforplt
1条回答
网友
1楼 · 发布于 2024-04-20 06:20:42

^{}的文献中,fit_predict(X[, y])都是“计算聚类中心预测每个样本的聚类指数”

而方法predict(X),只有“预测X中每个样本所属的最近聚类。”
因此,只有这一个必须在完整的数据集上使用。你知道吗

相关问题 更多 >