首页 文章

索引超出范围/ IndexError

提问于
浏览
0

我试图围绕图像数组移动内核以创建高斯滤波器 . 我得到一个IndexError,而Idk为什么 . 这是代码:第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))

至于混淆矩阵是和,和js . 它是阵列中每个元素的矩阵3x3 . 我知道这可能不是最好的方式

1 回答

  • 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))
    

相关问题