def save_image_with_clusters(x, clusters, filename, shape=(10, 10), scale_each=False,
transpose=False):
'''single image, each row is a cluster'''
makedirs(filename)
n = x.shape[0]
images = np.zeros_like(x)
curr_len = 0
for i in range(10):
images_i = x[clusters==i, :]
n_i = images_i.shape[0]
images[curr_len : curr_len+n_i, :] = images_i
curr_len += n_i
x = images
if transpose:
x = x.transpose(0, 2, 3, 1)
if scale_each is True:
for i in range(n):
x[i] = rescale_intensity(x[i], out_range=(0, 1))
n_channels = x.shape[3]
x = img_as_ubyte(x)
r, c = shape
if r * c < n:
print('Shape too small to contain all images')
h, w = x.shape[1:3]
ret = np.zeros((h * r, w * c, n_channels), dtype='uint8')
for i in range(r):
for j in range(c):
if i * c + j < n:
ret[i * h:(i + 1) * h, j * w:(j + 1) * w, :] = x[i * c + j]
ret = ret.squeeze()
io.imsave(filename, ret)
评论列表
文章目录