如何获取作为函数调用结果绘制的图像并将其绘制到图像网格?

发布于 2025-01-19 20:15:14 字数 678 浏览 5 评论 0原文

我有一个函数,用于输出使用 KMeans 对其像素进行聚类的照片。我可以输入 k 值作为参数,它将适合模型并输出新图像。

def cluster_image(k, img=img):
  img_flat = img.reshape(img.shape[0]*img.shape[1], 3)
  kmeans = KMeans(n_clusters = k, random_state = 42).fit(img_flat)
  new_img = img_flat.copy()
  
  for i in np.unique(kmeans.labels_):
    new_img[kmeans.labels_ == i, :] = kmeans.cluster_centers_[i]
  
  new_img = new_img.reshape(img.shape)

  return plt.imshow(new_img), plt.axis('off');

我想编写一个循环来输出 k=2 到 k=10 的图像:

k_values = np.arange(2, 11)
for k in k_values:
  print('k = ' + str(k))
  cluster_image(k)
  show()

这将返回图像的垂直线。我该如何做类似的事情,但将每个图像输出到 3x3 图像网格?

I have a function that I use to output a photo that has had its pixels clustered using KMeans. I can input the k value as an argument, and it will fit the model and output the new image.

def cluster_image(k, img=img):
  img_flat = img.reshape(img.shape[0]*img.shape[1], 3)
  kmeans = KMeans(n_clusters = k, random_state = 42).fit(img_flat)
  new_img = img_flat.copy()
  
  for i in np.unique(kmeans.labels_):
    new_img[kmeans.labels_ == i, :] = kmeans.cluster_centers_[i]
  
  new_img = new_img.reshape(img.shape)

  return plt.imshow(new_img), plt.axis('off');

I want to write a loop to output the images for k=2 through k=10:

k_values = np.arange(2, 11)
for k in k_values:
  print('k = ' + str(k))
  cluster_image(k)
  show()

This returns a vertical line of images. How do I do something like this, but output each image to a 3x3 grid of images?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2025-01-26 20:15:14

如果允许您修改cluster_image的签名,我会做:

def cluster_image(k, ax, img=img):
    img_flat = img.reshape(img.shape[0]*img.shape[1], 3)
    kmeans = KMeans(n_clusters = k, random_state = 42).fit(img_flat)
    new_img = img_flat.copy()

    for i in np.unique(kmeans.labels_):
        new_img[kmeans.labels_ == i, :] = kmeans.cluster_centers_[i]

    new_img = new_img.reshape(img.shape)
    ax.imshow(new_img)
    ax.axis('off')

fig, axs = plt.subplots(3, 3)
axs = axs.flatten()
k_values = np.arange(2, 11)
for i, k in enumerate(k_values):
    print('k = ' + str(k))
    cluster_image(k, axs[i], img=img)

If you are allowed to modify the signature of cluster_image, I would do:

def cluster_image(k, ax, img=img):
    img_flat = img.reshape(img.shape[0]*img.shape[1], 3)
    kmeans = KMeans(n_clusters = k, random_state = 42).fit(img_flat)
    new_img = img_flat.copy()

    for i in np.unique(kmeans.labels_):
        new_img[kmeans.labels_ == i, :] = kmeans.cluster_centers_[i]

    new_img = new_img.reshape(img.shape)
    ax.imshow(new_img)
    ax.axis('off')

fig, axs = plt.subplots(3, 3)
axs = axs.flatten()
k_values = np.arange(2, 11)
for i, k in enumerate(k_values):
    print('k = ' + str(k))
    cluster_image(k, axs[i], img=img)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文