2017-07-08 11 views
1

ガウスフィルタを実装しようとしています。このため私はカーネル3x3と画像の配列を使用しています。問題は、配列の各[i、j]要素に対して3x3の部分行列を定義することです。私はコード内に詳細を書きました。画像/ガウスフィルタの配列上の3x3のカーネル行列

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


imagen = scipy.misc.imread("C:\\Users\\Reymi\\Downloads\\imagen.png") #importing image of original size (1929, 1280) 

imagen_real = scipy.pad(array=imagen, pad_width=[1, 1], mode='constant', constant_values=0) #add 1 column and 1 row of zeros to avoid the kernel of going outside the array. size is (1931, 1282) 

imagen_nueva = np.empty((1931, 1282)) #the new image. the same size as the image I will filter 

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


ker1 = np.array([[1/16, 1/8, 1/16], #3x3 kernel 
       [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): #the range starts from 1 to avoid the column and row of zeros, and ends before the last col and row of zeros 
     for j in range(1,dim_y): 
      imagen_entry = np.empty((3, 3))  #Main problem here: how do I define a 3x3 matrix for each entry? 
      valor = np.sum(imagen_entry*ker1) #Matrix 3x3 is filled with the elements around each [i, j] entry of the array 
      imagen_real[i, j] = valor 
      imagen_nueva = np.append(imagen_real[i, j], (1931, 1282)) #This is supposed to each new [i, j] entry to the new image 

print("La imagen con el blur effect es la siguiente:\n") 

multiplicar_entero() #calls function 


plt.imshow(imagen_nueva) #Show new image 
plt.gray() 
plt.show() 

申し訳ありませんが、長いコードです。助けてくれてありがとう。

+1

をあなたがより明確に求めている質問を投稿してくださいもらえますか?私が得たように、畳み込み演算子を自分で実装しようとしていますか? – akilat90

+0

はい。コンボルーションの関数 – angelustt

+1

['scipy.ndimage'にはガウスフィルタがあります(https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.ndimage.filters)。 gaussian_filter.html)?そして、 'np.append'呼び出しも機能しますか?あなたがしたいと思っているようなことはまったくないようです。 –

答えて

0

であるとして、あなたのコードでいくつかの問題があります。

  • あなたは私たちがへのアクセスを持っていない画像を使用しています。私たちがコードを実行できるように、例では常に自由に利用可能な画像を使用してください。私はここでscipy.misc.ascentを使用しました。
  • 本当にする必要がない限り、グローバル変数は使用しないでください。
  • 常に英語の変数名と英語によるコメントを記述してください。これは、このようなレビューをずっと簡単にします。

私はあなたのコードにいくつかの修正を行いました。私はあなたの問題をどのように解決したのか分かります。具体的にあなたは、配列のサブセットを抽出し、その結果を割り当てることarray[i, j]を使用することができます:インデックスを使用したい:

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

imagen = scipy.misc.ascent() # Freely available image 

(dim_x, dim_y) = np.shape(imagen) 

ker1 = np.array([[1/16, 1/8, 1/16], #3x3 kernel 
       [1/8, 1/4, 1/8], 
       [1/16, 1/8, 1/16]]) 

def multiplicar_entero(imagen): 
    imagen_nueva = np.zeros(imagen.shape) #the new image. the same size as the image I will filter 
    for i in range(1,dim_x-1): #the range starts from 1 to avoid the column and row of zeros, and ends before the last col and row of zeros 
     for j in range(1,dim_y-1): 
      imagen_entry = imagen[i-1:i+2, j-1:j+2] 
      valor = np.sum(imagen_entry*ker1) #Matrix 3x3 is filled with the elements around each [i, j] entry of the array 
      imagen_nueva[i, j] = valor 
    return imagen_nueva 

imagen_nueva = multiplicar_entero(imagen) 

plt.imshow(imagen_nueva) #Show new image 
plt.gray() 
plt.show() 
+0

「本当に必要な場合を除いては、グローバル変数を使用しないでください。」というのは大きなポイントです。しかし、この答えは暗黙的に 'dim_x'を' dim_y'と 'ker1'を[グローバル変数]として使用します(https://docs.python.org/3/faq/programming.html#what-are-the-ules Pythonではローカル変数とグローバル変数の両方に対応していますが、必ずしも "必要"ではないかもしれません。 –

関連する問題