【问题标题】:Index out of bounds / IndexError索引超出范围/IndexError
【发布时间】:2017-07-07 05:27:19
【问题描述】:

我正在尝试围绕图像数组移动内核以创建高斯滤波器。我得到一个 IndexError,我知道为什么。这是代码:第 34 行的错误

import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt

imagen_nueva = np.empty((1931, 1282))

imagen = scipy.misc.imread("C:\\Users\\Reymi\\Downloads\\imagen.png")

imagen_real = scipy.pad(array=imagen, pad_width=[1, 1], mode='constant', 
constant_values=0)

(dim_x,dim_y)=np.shape(imagen_real)
print((dim_x,dim_y))

ker1 = np.array([[1/16, 1/8, 1/16],
            [1/8, 1/4, 1/8],
            [1/16, 1/8, 1/16]])

def multiplicar_entero():
    global imagen_nueva
    for i in range(1,dim_x+1):
    for j in range(1,dim_y+1):
        matriz_elemento = np.array([[imagen_real[i + 1, j - 1], 
imagen_real[i + 1, j], imagen_real[i + 1, j - 1]],
                        [imagen_real[i, j - 1], imagen_real[i, j], 
imagen_real[i, j + 1]],
                        [imagen_real[i - 1, j - 1], imagen_real[i - 1, j], 
imagen_real[i - 1, j + 1]]])
        valor = np.sum(matriz_elemento*ker1)
        imagen_real[i, j] = valor
        imagen_nueva = np.append(imagen[i, j], (1931, 1282))

至于is, js的混淆矩阵。它是数组中每个元素的 3x3 矩阵。我知道这可能不是最好的方法

【问题讨论】:

  • 您的行没有编号,我们不知道错误...您可以编辑该错误吗?
  • python 2 还是 python 3?
  • multiplicar_entero() 中,第二个for-loop 的缩进不正确,我想知道之后的任何地方是否正确...

标签: python arrays numpy scipy bounds


【解决方案1】:

对您的代码进行一些小的修改,例如修复缩进和使用开源图像,我没有收到任何错误。所以这似乎是一个缩进错误。

查看下面的工作代码:

import numpy as np
import scipy
from scipy import misc
import matplotlib.pyplot as plt

imagen_nueva = np.empty((1931, 1282))

imagen = scipy.resize(misc.ascent(), (1931, 1282))

imagen_real = scipy.pad(array=imagen, pad_width=[1, 1], mode='constant',
                        constant_values=0)

(dim_x, dim_y) = np.shape(imagen_real)
print((dim_x, dim_y))

ker1 = np.array([[1/16, 1/8, 1/16],
                 [1/8, 1/4, 1/8],
                 [1/16, 1/8, 1/16]])


def multiplicar_entero():
    global imagen_nueva
    for i in range(1, dim_x + 1):
        for j in range(1, dim_y + 1):
            matriz_elemento = np.array([[imagen_real[i + 1, j - 1], imagen_real[i + 1, j], imagen_real[i + 1, j - 1]],
                                        [imagen_real[i, j - 1], imagen_real[i, j], imagen_real[i, j + 1]],
                                        [imagen_real[i - 1, j - 1], imagen_real[i - 1, j], imagen_real[i - 1, j + 1]]])

            valor = np.sum(matriz_elemento*ker1)
            imagen_real[i, j] = valor
            imagen_nueva = np.append(imagen[i, j], (1931, 1282))

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 2019-11-25
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2011-10-31
    • 2018-11-16
    相关资源
    最近更新 更多