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

Python中图像均值计算后保留矩阵及PCA维度错误解决求助

图像PCA维度错误问题排查与解决

操作流程

  • 上传图像后转为灰度图,格式设为uint8
  • 执行me_df= img_df.mean(axis=0)计算图像均值,此时图像变为2D数组
  • 选取感兴趣区域的代码:
mask = np.zeros(me_df.shape, dtype=me_df.dtype)
mask[roi_lower:roi_upper,roi_left:roi_right] = 1
mask_df = mean_df * mask

遇到的问题

尝试将数据转为1D以适配PCA时失败,因为当前数据并非3D结构。调整代码移除mask_df.shape[2]相关操作后,仍出现报错:

maximum supported dimension for an ndarray is 32, found 640 or only integer scalar arrays can be converted to a scalar index

相关出错代码片段:

image_shape = mask_df[0].shape
frames_reshaped = mask_df.reshape([
    mask_df.shape[0],
    mask_df.shape[1]*mask_df.shape[2]
    ])

ncomp=4
pca = PCA(n_components=ncomp) 
pca.fit(test)

后续可视化代码同样存在维度问题:

dd = pca.components_[pc_number-1].reshape(frames_reshaped)
mag = np.max(dd) - np.min(dd)
et = (dd-np.min(dd))/mag*255
plt.imshow(et, cmap="Greys_r")

求助需求

如何在计算图像列表均值后保留正确的矩阵形式,同时解决PCA运行时的维度错误问题?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 00:15:44