2017-07-07 185 views
0

我正在嘗試在圖像數組周圍移動內核來創建高斯過濾器。我得到一個IndexError和Idk爲什麼。這是代碼:第34行的錯誤索引超出範圍/ IndexError

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。我知道它可能不是最好的方法

+2

你的線都沒有編號,我們不知道的錯誤...您可以編輯錯誤? –

+1

python 2或python 3? –

+1

在'multiplicar_entero()'中,第二個for循環的縮進是不正確的,我想知道它在以後的任何地方是否正確... –

回答

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