【问题标题】:image histogram: IndexError: index 4 is out of bounds for axis 1 with size 4图像直方图:IndexError:索引 4 超出轴 1 的范围,大小为 4
【发布时间】:2020-10-28 20:11:44
【问题描述】:

我尝试运行一个 Python 程序来绘制给定图像的直方图。它的行为符合预期,但会产生以下错误:

axes[k,idx].hist(x=fruit[:, :, i].flat, bins=256, range=(0, 255)
IndexError: index 4 is out of bounds for axis 1 with size 4

这是我的代码:

import numpy
import numpy as np
import skimage.io
import matplotlib.pyplot
import matplotlib.pyplot as plt



raspberry = skimage.io.imread(fname="raspberry.jpg", as_gray=False)
apple = skimage.io.imread(fname="apple.jpg", as_gray=False)
fruits_data = [apple,raspberry]


fig, axes = plt.subplots(nrows=2, ncols=4)
ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7 = axes.flatten()
axs = [ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7]
i = 0
k=0
for j in range(0, 6, 4):
    for fruit in fruits_data:
        for idx in range(j,j+4):
          if i < 3:
            axes[k,idx].hist(x=fruit[:, :, i].flat, bins=256, range=(0, 255))
            i += 1
          else :
              i = 0
              axes[k,idx].imshow(fruit)
              k = 1

plt.show()

【问题讨论】:

    标签: python numpy matplotlib image-processing


    【解决方案1】:

    该错误告诉你究竟出了什么问题:你试图访问4 位置上的axes 数组中的一个值(通过axes[k, idx] 中的idx 参数),而数组只有@987654325 @long 在其1st 轴上(所以最高索引是3)。

    附:检查您的导入,您将某些东西导入两次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2015-08-06
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2018-05-09
      相关资源
      最近更新 更多