CONVOLUCAO

 1 def convolucao(I, Q):
 2 
 3     I, Q = asarray(I), asarray(Q)
 4 
 5     M, N = I.shape
 6 
 7     Mq, Nq = Q.shape
 8 
 9     FM = (Mq-1)/2 # Borda da mascara
10     FN = (Nq-1)/2 # Borda da mascara
11 
12     Iout = zeros((M-(FM*2), N-(FN*2))) # Imagem resultante nao terah as bordas
13 
14     # Aplica a mascara na imagem ++++++++++++++++++++++++++++++++++++++++++++++
15     for iM in range(FM, M-FM):
16         for iN in range(FN, N-FN):
17             value = 0
18 
19             # Soma os valores da mascara --------------------------------------
20             for iMq in range(0, Mq):
21                 for iNq in range(0, Nq):
22 
23                     value = value + I[iM + iMq - FM, iN + iNq - FN] * Q[iMq, iNq]
24             #-------------------------------------------------------------------
25 
26             Iout[iM-FM, iN-FN] = value
27 
28     # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 
30     return Iout
31 
32 #def

Testando a Convolucao - Note que a funcao remove as bordas

1 # MASCARAS !!!!
2 Media3x3  = array(((0.111,0.111,0.111),(0.111,0.111,0.111) ,(0.111,0.111,0.111)))
3 Lapla3x3 = array(((0,1,0),(1,-4,1),(0,1,0)))
4 Lapld3x3 = array(((1,1,1),(1,-8,1),(1,1,1)))
5 Media5x5 = array(((0.04,0.04,0.04,0.04,0.04),(0.04,0.04,0.04,0.04,0.04),(0.04,0.04,0.04,0.04,0.04),(0.04,0.04,0.04,0.04,0.04),(0.04,0.04,0.04,0.04,0.04)))
De :
[[0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]]
Para (Media): 
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
De :
[[0 0 0 0 0 0]
 [9 9 9 9 9 9]
 [9 9 9 9 9 9]
 [9 9 9 9 9 9]
 [0 0 0 0 0 0]]
Para (Media): 
[[ 5.994  5.994  5.994  5.994]
 [ 8.991  8.991  8.991  8.991]
 [ 5.994  5.994  5.994  5.994]]
De :
[[0 0 0 0 0 0]
 [9 0 9 0 9 0]
 [9 0 9 0 9 0]
 [9 0 9 0 9 0]
 [0 0 0 0 0 0]]
Para (Media): 
[[ 3.996  1.998  3.996  1.998]
 [ 5.994  2.997  5.994  2.997]
 [ 3.996  1.998  3.996  1.998]]

Imagem Original

Imagem com filtro da media 3x3 aplicado

Imagem media 5x5 applicado

Imagem com laplaciano aplicado na imagem

Imagem com laplaciano aplicado na imagem ja filtrada na media

Imagem com laplaciano 2 aplicado na imagem ja filtrada na media

Imagem Original

Imagem com filtro da media 3x3 aplicado

Imagem media 5x5 applicado

Imagem com laplaciano aplicado na imagem

Imagem com laplaciano aplicado na imagem ja filtrada na media

Imagem com laplaciano 2 aplicado na imagem ja filtrada na media

...

Autor:gkaneblai
Data:22/04/2009