lionisEX07

Autor: lionis 30/04/2009

Description

The convolution theorem is illustrated by comparing the result of the iaconv function (from ia636 toolbox) and the new implemented one: iaconvlinear2.

Definition of discrete linear convolution

The convolution of f(x,y) and h(x,y) is denoted by f(x,y)*h(x,y). The expression is written bellow:

where the negative signs do the 180 degrees rotation of the correlation mask, generating the convolution mask.

where AxB and CxD are dimension axis of f(x,y) e h(x,y) respectively.

Script

```1 h = mmreadgray('cameraman.pgm')
```

So the 3x3 and 5x5 convolution masks for sharpening and unsharpening are defined as follows:

``` 1 f_suavizacao3 = array(
2   [[ 0.11, 0.11, 0.11],
3    [ 0.11, 0.11, 0.11],
4    [ 0.11, 0.11, 0.11]])
5
6 f_laplace3 = array(
7   [[ 0, 1, 0],
8    [ 1,-4, 1],
9    [ 0, 1, 0]])
10
11 f_laplaced3 = array(
12   [[ 1, 1, 1],
13    [ 1,-8, 1],
14    [ 1, 1, 1]])
15
16 f_sobelVertical3 = array(
17   [[ 1, 2, 1],
18    [ 0, 0, 0],
19    [-1,-2,-1]])
20
21 f_sobelHorizontal3 = array(
22   [[ 1, 0,-1],
23    [ 2, 0,-2],
24    [ 1, 0,-1]])
```
``` 1 f_suavizacao5 = array(
2   [[0.04, 0.04, 0.04, 0.04, 0.04],
3    [0.04, 0.04, 0.04, 0.04, 0.04],
4    [0.04, 0.04, 0.04, 0.04, 0.04],
5    [0.04, 0.04, 0.04, 0.04, 0.04],
6    [0.04, 0.04, 0.04, 0.04, 0.04]])
7
8 f_laplace5 = array(
9   [[0, 0,  1, 0, 0],
10    [0, 1,  2, 1, 0],
11    [1, 2,-16, 2, 1],
12    [0, 1,  2, 1, 0],
13    [0, 0,  1, 0, 0]])
14
15 f_laplaced5 = array(
16   [[1, 1,  1, 1, 1],
17    [1, 2,  2, 2, 1],
18    [1, 2,-32, 2, 1],
19    [1, 2,  2, 2, 1],
20    [1, 1,  1, 1, 1]])
21
22 f_sobelVertical5 = array(
23   [[ 1, 4,  6, 4, 1],
24    [ 2, 8, 12, 8, 2],
25    [ 0, 0,  0, 0, 0],
26    [-2,-8,-12,-8,-2],
27    [-1,-4, -6,-4,-1]])
28
29 f_sobelHorizontal5 = array(
30   [[1, 2, 0, -2,-1],
31    [4, 8, 0, -8,-4],
32    [6,12, 0,-12,-6],
33    [4, 8, 0, -8,-4],
34    [1, 2, 0, -2,-1]])
```

Implementing the normalization function

```1 def normalize(I, r): #esta normalizacao nao descarta os valores negativos nem valores acima de 255; todo o intervalo e' preservado
2     min,max = r[0],r[1] #valores minimo e maximo da nova escala
3
4     I = (I - I.min())/(I.max() - I.min())
5     I *= (max - min) + min #conversao das escalas
6
7     return uint(I)
8
9 r = array([0,255])
```

Original iaconv function

``` 1 adshow(ianormalize(iaconv(f_suavizacao3, h), r), title = 'Convolução linear de suavizacao 3x3')
2 adshow(ianormalize(iaconv(f_suavizacao5, h), r), title = 'Convolução linear de suavizacao 5x5')
3
4 adshow(ianormalize(iaconv(f_laplace3, h), r), title = 'Convolução linear de Laplace 3x3')
5 adshow(ianormalize(iaconv(f_laplace5, h), r), title = 'Convolução linear de Laplace 5x5')
6
7 adshow(ianormalize(iaconv(f_laplaced3, h), r), title = 'Convolução linear de Laplace diagonal 3x3')
8 adshow(ianormalize(iaconv(f_laplaced5, h), r), title = 'Convolução linear de Laplace diagonal 5x5')
9
10 adshow(ianormalize(iaconv(f_sobelVertical3, h), r), title = 'Convolução linear de Sobel vertical 3x3')
11 adshow(ianormalize(iaconv(f_sobelVertical5, h), r), title = 'Convolução linear de Sobel vertical 5x5')
12
13 adshow(ianormalize(iaconv(f_sobelHorizontal3, h), r), title = 'Convolução linear de Sobel horizontal 3x3')
14 adshow(ianormalize(iaconv(f_sobelHorizontal5, h), r), title = 'Convolução linear de Sobel horizontal 5x5')
```

Implementing the linear discrete convolution

``` 1 def iaconvlinear2(f, h): #utilizando zero padding, assim como definido para a iaconv original
2     a,b = f.shape #dimensoes da imagem de convolucao
3     c,d = h.shape #dimensoes da mascara
4     n = a + c - 1 #numero de linhas da imagem "temporaria" a ser trabalhada
5     m = b + d - 1 #numero de colunas da imagem "temporaria" a ser trabalhada
6
7     new_f = zeros((n,m))
8     new_f[0:a,0:b] = f
9     new_h = zeros((n,m))
10     new_h[0:c,0:d] = h
11
12     I = zeros((n,m)) #matriz final, a qual recebera os valores da convolucao
13
14     for i in range(n):
15         for j in range(m): #i e j iteram a imagem original
16             for k in range(a):
17                 for l in range(b): #k e l iteram a mascara de convolucao
18                     I[i,j] += new_f[k,l] * new_h[i-k,j-l] #convolucao da mascara com a imagem
19
20     return I
21
22 adshow(normalize(iaconvlinear2(f_suavizacao3, h), r), title = 'Convolução linear de suavizacao 3x3')
23 adshow(normalize(iaconvlinear2(f_suavizacao5, h), r), title = 'Convolução linear de suavizacao 5x5')
24
25 adshow(normalize(iaconvlinear2(f_laplace3, h), r), title = 'Convolução linear de Laplace 3x3')
26 adshow(normalize(iaconvlinear2(f_laplace5, h), r), title = 'Convolução linear de Laplace 5x5')
27
28 adshow(normalize(iaconvlinear2(f_laplaced3, h), r), title = 'Convolução linear de Laplace diagonal 3x3')
29 adshow(normalize(iaconvlinear2(f_laplaced5, h), r), title = 'Convolução linear de Laplace diagonal 5x5')
30
31 adshow(normalize(iaconvlinear2(f_sobelVertical3, h), r), title = 'Convolução linear de Sobel vertical 3x3')
32 adshow(normalize(iaconvlinear2(f_sobelVertical5, h), r), title = 'Convolução linear de Sobel vertical 5x5')
33
34 adshow(normalize(iaconvlinear2(f_sobelHorizontal3, h), r), title = 'Convolução linear de Sobel horizontal 3x3')
35 adshow(normalize(iaconvlinear2(f_sobelHorizontal5, h), r), title = 'Convolução linear de Sobel horizontal 5x5')
```
```ERROR execute

*** Exception while evaluating code: