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

使用scikit-image的pyramid_reduce后图像维度异常,如何修复?

解决scikit-image pyramid_reduce降采样后通道数丢失的问题

你的问题出在pyramid_reduce的默认参数设置上——它默认不会把图像的最后一个维度识别为颜色通道,而是将其当作普通的特征维度进行降采样,所以原本的3通道RGB图被压缩成了单通道。

下面是两种针对性的解决方案,适配不同版本的scikit-image:

方案1:针对scikit-image 0.19及以后版本(推荐)

新版本中multichannel参数被弃用,改用channel_axis来指定颜色通道的位置。对于RGB图像,通道在最后一维,所以设置channel_axis=-1即可:

import os, cv2, glob
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.transform import pyramid_reduce

# 拆分导入语句,代码可读性更好
plt.style.use('dark_background')

img_path = os.path.join(img_base_path, value[0])
img = io.imread(img_path)
# 指定通道维度,保留RGB三通道结构
resized = pyramid_reduce(img, downscale=4, channel_axis=-1)
print(resized.shape)  # 现在应该输出(60, 60, 3)

方案2:针对scikit-image 0.18及更早版本

旧版本使用multichannel=True来告诉函数最后一维是颜色通道:

resized = pyramid_reduce(img, downscale=4, multichannel=True)

为什么会出现这个问题?

默认情况下,pyramid_reduce会把输入的数组当成多特征的单通道数据(比如每个像素有3个特征值),而不是RGB图像。所以它会对所有维度(包括通道维度)执行降采样操作,最终把3通道压缩成了1通道。通过指定通道相关的参数,函数就会单独对每个颜色通道进行降采样,保留原有的3通道结构,这样matplotlib.imshow就能正常识别并显示图像了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:17:04