You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中RGB图像分量经K-means滤波后如何合并重构为原尺寸图像

分量合并操作步骤

你可以按照以下两步完成三个滤波后颜色分量的合并:

  1. 把image_df中存储的一维分量向量,重塑为原始尺寸(284, 321)的二维通道矩阵,注意转换为uint8类型适配图像像素取值要求
  2. 把三个二维通道矩阵合并为完整的RGB图像

示例代码

import numpy as np
from PIL import Image

# 需替换为你实际存储滤波后分量的DataFrame列名,若直接修改了原vec系列列则对应替换即可
r_col = 'filtered_red'
g_col = 'filtered_green'
b_col = 'filtered_blue'

# 分量重塑为二维矩阵
height, width = 284, 321
r_mat = image_df[r_col].values.reshape(height, width).astype(np.uint8)
g_mat = image_df[g_col].values.reshape(height, width).astype(np.uint8)
b_mat = image_df[b_col].values.reshape(height, width).astype(np.uint8)

# 合并为RGB图像
# 方案一:numpy堆叠后生成图像
rgb_img = Image.fromarray(np.dstack([r_mat, g_mat, b_mat]))

# 方案二:PIL原生通道合并
# r = Image.fromarray(r_mat)
# g = Image.fromarray(g_mat)
# b = Image.fromarray(b_mat)
# rgb_img = Image.merge('RGB', (r, g, b))

# 可选操作:保存或展示图像
# rgb_img.save('kmeans滤波结果.jpg')
# rgb_img.show()

注意事项

如果你的K-means滤波输出的是0-1区间的归一化值,需要先将分量值乘以255之后再转换为uint8类型,避免出现图像全黑或者像素值异常的问题。

内容的提问来源于stack exchange,提问作者yras8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 07:15:03