簡體   English   中英

索引超出范圍/ IndexError

[英]Index out of bounds / IndexError

我正在嘗試圍繞圖像數組移動內核以創建高斯濾波器。 我收到一個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))

至於與is和js混淆的矩陣。 它是數組每個元素的矩陣3x3。 我知道這可能不是最好的方法

對您的代碼進行一些小的修改,例如修復縮進和使用開源圖像,我不會遇到任何錯誤。 因此,這似乎是一種壓痕錯誤。

請參見下面的工作代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM