Sie sind auf Seite 1von 181

IMAGE FILTERING

Vedat Tavşanoğlu
Image Filtering
 
y( n , m )    x( k ,l )h( n  k , m  l )
k   l  
 
   x( n  k , m  l )h( k ,l )
k   l  
Image Filtering

 


j x j y  j (  x m  y n )
Y( e ,e ) y( n , m )e
n   m  
   

  [   x( k ,l )h( n  k , m  l ) ] e
 j (  x m  y n )

n   m   k   l  
   

  [   x( n  k , m  l )h( k ,l ) ] e
 j (  x m  y n )

n   m   k   l  
Image Filtering
Now let

n  k  i and m  l  j
j x j y
Y( e ,e )
   

  [   x( k ,l )h( i , j ) ] e
 j (  x ( j  l ) y ( i  k ))

n   m   k   l  
   

  [   x( i , j )h( k ,l ) ] e
 j (  x ( j  l ) y ( i  k ))

n   m   k   l  
Image Filtering

j x j y
Y( e ,e )
   

  h( i , j )e   x( k ,l )e
 j (  x i  y j )  j (  x l  y k )

i   j   k   l  
j x j y j x j y
 H( e ,e )X ( e ,e )
Image Filtering
Low-pass Filtering:
Image Filtering
Low-pass Filtering:
Image Filtering
Low-pass Filtering:
Image Filtering

Low-pass Filtering:

1 1 1

h  1 1 1 
1 1 1
Image Filtering
Low-pass Fıltering:
I=zeros(12,12);
I(2:9,2:9)=1;
h=ones(3,3)/9;
Iout=conv2(I,h)
imshow(I,'notruesize'),title('Original Image')
figure,mesh(I)
imshow(h,'notruesize'),title('Original Image')
figure,mesh(h)
figure,imshow(Iout,'notruesize'),title('Filtered Image')
figure,mesh(Iout);
Image Filtering
Low-Pass Filter

Close all
I=ones(12,12);
I(2:9,2:9)=1;
h=ones(3,3)/9;
Iout=conv2(I,h)
imshow(I,'notruesize'),title('Original Image')
figure,mesh(I),title('Original Image')
F=fft2(I,256,256);
F=fftshift(F);
F2=abs(F);
Image Filtering
figure,imshow(F2,[0
5],'notruesize');colormap(jet);colorbar,title('FT Original
Image')
figure,mesh(F2),title('FT of Original Image')
figure,mesh(h),title('Filter Kernel')
H=fft2(h,256,256);
H=fftshift(H);
H2=abs(H);
figure,imshow(H2,[0
5],'notruesize');colormap(jet);colorbar,title('FT of Filter
Kernel')
figure,mesh(H2),title('FT of Filter Kernel')
figure,imshow(Iout,'notruesize'),title('Filtered Image')
figure,mesh(Iout),title('Filtered Image')
Image Filtering
I2=fft2(Iout,256,256);
I2=fftshift(I2);
I2=abs(I2);
figure,imshow(I2,[0
5],'notruesize');colormap(jet);colorbar,title('FT of
Filtered Image')
figure,mesh(I2),title('FT of Filtered Image')
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
OUTPUT IMAGE

0 0 0 0 0 0 0 0 0 0 0 0
0 .11 .22 .33 .33 .33 .33 .33 .33 .22 .11 0
0 .22 .44 .67 .67 .67 .67 .67 .67 .44 .22 0
0 .33 .67 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33 0
0 .33 .67 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33 0
0 .33 .67 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33 0
0 .33 .67 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33 0
0 .33 .67 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33 0
0 .33 .67 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33 0
0 .22 .44 .67 .67 .67 .67 .67 .67 .44 .22 0
0 .11 .22 .33 .33 .33 .33 .33 .33 .22 .11 0
0 0 0 0 0 0 0 0 0 0 0 0
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Low-pass Fıltering
Exercise: Apply the same filter to the image given by
I=ones(10,10).
Image Filtering

Iout =

Columns 1 through 10

0.1111 0.2222 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333
0.2222 0.4444 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.3333 0.6667 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0.2222 0.4444 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667 0.6667
0.1111 0.2222 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333 0.3333
Image Filtering
Columns 11 through 12

0.2222 0.1111
0.4444 0.2222
0.6667 0.3333
0.6667 0.3333
0.6667 0.3333
0.6667 0.3333
0.6667 0.3333
0.6667 0.3333
0.6667 0.3333
0.6667 0.3333
0.4444 0.2222
0.2222 0.1111
Image Filtering
FILTERED IMAGE

.11 .22 .33 .33 .33 .33 .33 .33 . 33 .33 .22 .11
.22 .44 .67 .67 .67 .67 .67 .67 .67 .67 .44 .22
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.33 .67 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 .67 .33
.22 .44 .67 .67 .67 .67 .67 .67 .67 .67 .44 .22
.11 .22 .33 .33 .33 .33 .33 .33 .33 .33 .22 .11
Image Filtering
Image Filtering
Image Filtering
Image Filtering
High-Pass Filter
Image Filtering
High-Pass Filter
High-pass filter with no DC bias
(coefficients added to zero)

 8 1 1
8
1
8

h  18 1 1 
8

18 1
8
1 
8
Image Filtering
High-Pass Filter
High-pass filter with no DC bias
(coefficients added to zero)

 8 1 1
8
1
8

h  18 1 1 
8

18 1
8
1 
8
Image Filtering High-Pass Filter
Image Filtering High-Pass Filter
Image Filtering High-Pass Filter
Image Filtering
Close all
I=zeros(16,16);
I(5:12,5:12)=1;
h=ones(3,3)/8;
h=-h;
h(2:2,2:2)=1;
Iout=conv2(I,h);
imshow(I,'notruesize'),title('Original Image')
figure,mesh(I),title('Original Image')
F=fft2(I,256,256);
F=fftshift(F);
F2=abs(F);
Image Filtering
figure,imshow(F2,[0
5],'notruesize');colormap(jet);colorbar,title('FT Original
Image')
figure,mesh(F2),title('FT of Original Image')
figure,imshow(h,'notruesize'),title('Filter Kernel')h
figure,mesh(h),title('Filter Kernel')
H=fft2(h,256,256);
H=fftshift(H);
H2=abs(H);
figure,imshow(H2,[0
1.5],'notruesize');colormap(jet);colorbar,title('FT of
Filter Kernel')
figure,mesh(H2),title('FT of Filter Kernel')
figure,imshow(Iout,[-.5 .7],'notruesize'),title('Filtered
Image')
figure,mesh(Iout),title('Filtered Image')
Image Filtering
I2=fft2(Iout,256,256);
I2=fftshift(I2);
I2=abs(I2);
figure,imshow(I2,[0
5],'notruesize');colormap(jet);colorbar,title('FT of
Filtered Image')
figure,mesh(I2),title('FT of Filtered Image')
Image Filtering
Image Filtering
I=

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 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 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 1 1 1 1 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 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 0 0 0 0 0 0 0 0
Image Filtering
Iout =

Columns 1 through 10

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
0 0 0 0 0 0 0 0 0 0
0 0 0 0 -0.1250 -0.2500 -0.3750 -0.3750 -0.3750 -0.3750
0 0 0 0 -0.2500 0.6250 0.3750 0.3750 0.3750 0.3750
0 0 0 0 -0.3750 0.3750 0 0 0 0
0 0 0 0 -0.3750 0.3750 0 0 0 0
0 0 0 0 -0.3750 0.3750 0 0 0 0
0 0 0 0 -0.3750 0.3750 0 0 0 0
0 0 0 0 -0.3750 0.3750 0 0 0 0
0 0 0 0 -0.3750 0.3750 0 0 0 0
0 0 0 0 -0.2500 0.6250 0.3750 0.3750 0.3750 0.3750
0 0 0 0 -0.1250 -0.2500 -0.3750 -0.3750 -0.3750 -0.3750
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
0 0 0 0 0 0 0 0 0 0
Image Filtering
Columns 11 through 18

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 0 0
-0.3750 -0.3750 -0.2500 -0.1250 0 0 0 0
0.3750 0.3750 0.6250 -0.2500 0 0 0 0
0 0 0.3750 -0.3750 0 0 0 0
0 0 0.3750 -0.3750 0 0 0 0
0 0 0.3750 -0.3750 0 0 0 0
0 0 0.3750 -0.3750 0 0 0 0
0 0 0.3750 -0.3750 0 0 0 0
0 0 0.3750 -0.3750 0 0 0 0
0.3750 0.3750 0.6250 -0.2500 0 0 0 0
-0.3750 -0.3750 -0.2500 -0.1250 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 0
0 0 0 0 0 0 0 0 0
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
High-pass filtering of rotated image
The image I in the previous example can be rotated 45o
by using I=imrotate(I,45),and we obtain
Image Filtering
I=

Columns 1 through 17

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 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 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 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 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
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 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 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 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 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 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 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 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 0 0 0 0 0 0 0
Image Filtering
Iout =

Columns 1 through 10
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
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
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
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 -0.1250
0 0 0 0 0 0 0 0 -0.1250 -0.3750
0 0 0 0 0 0 0 -0.1250 -0.3750 0.3750
0 0 0 0 0 0 0 -0.1250 0.6250 0.2500
0 0 0 0 0 0 0 -0.1250 -0.3750 0.3750
0 0 0 0 0 0 0 0 -0.1250 -0.3750
0 0 0 0 0 0 0 0 0 -0.1250
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
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
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
0 0 0 0 0 0 0 0 0 0
Image Filtering
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
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
0 0 0 0 0 0 0 0 0 0
0 0 -0.1250 -0.1250 -0.1250 0 0 0 0 0
0 -0.1250 -0.3750 0.6250 -0.3750 -0.1250 0 0 0 0
-0.1250 -0.3750 0.3750 0.2500 0.3750 -0.3750 -0.1250 0 0 0
-0.3750 0.3750 0.1250 0 0.1250 0.3750 -0.3750 -0.1250 0 0
0.3750 0.1250 0 0 0 0.1250 0.3750 -0.3750 -0.1250 0
0.1250 0 0 0 0 0 0.1250 0.3750 -0.3750 -0.1250
0 0 0 0 0 0 0 0.2500 0.6250 -0.1250
0.1250 0 0 0 0 0 0.1250 0.3750 -0.3750 -0.1250
0.3750 0.1250 0 0 0 0.1250 0.3750 -0.3750 -0.1250 0
-0.3750 0.3750 0.1250 0 0.1250 0.3750 -0.3750 -0.1250 0 0
-0.1250 -0.3750 0.3750 0.2500 0.3750 -0.3750 -0.1250 0 0 0
0 -0.1250 -0.3750 0.6250 -0.3750 -0.1250 0 0 0 0
0 0 -0.1250 -0.1250 -0.1250 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 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 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Hihg-pass filter with DC bias
(coefficients added to one)

 1  1  1

h   1 9  1 
 1  1  1
Image Filtering
Image Filtering
Image Filtering
Iout =

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 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 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 -1 -2 -3 -3 -3 -3 -3 -3 -2 -1 0 0 0 0
0 0 0 0 -2 6 4 4 4 4 4 4 6 -2 0 0 0 0
0 0 0 0 -3 4 1 1 1 1 1 1 4 -3 0 0 0 0
0 0 0 0 -3 4 1 1 1 1 1 1 4 -3 0 0 0 0
0 0 0 0 -3 4 1 1 1 1 1 1 4 -3 0 0 0
0
0 0 0 0 -3 4 1 1 1 1 1 1 4 -3 0 0 0
0
0 0 0 0 -3 4 1 1 1 1 1 1 4 -3 0 0 0
0 0 0 0 -3 4 1 1 1 1 1 1 4 -3 0 0 0 0
0 0 0 0 -2 6 4 4 4 4 4 4 6 -2 0 0 0 0
0 0 0 0 -1 -2 -3 -3 -3 -3 -3 -3 -2 -1 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Edge Detection Methods Based on the First Derivative
Image Filtering
Edge Detection Methods Based on the First Derivative
Image Filtering
Edge Detection (Enhancement)
Edge Detection Methods Based on the Second Derivative
Image Filtering
Edge Detection (Enhancement)
Edge Detection Methods Based on the Second Derivative

Using Euler’s forward difference in the x direction:

I ( x, y ) I ( x  x, y )  I ( x, y )

x x
Image Filtering
Edge Detection (Enhancement)

Now using Euler’s backward difference in the x direction:

I ( x  x, y )  I ( x, y ) I ( x, y )  I ( x  x, y )

 2 I ( x, y ) x x

x 2 x
I ( x  x, y )  2 I ( x, y )  I ( x  x, y )

x 2
Image Filtering
Edge Detection (Enhancement)
Similarly we obtain
 I ( x, y ) I ( x, y  y )  2 I ( x, y )  I ( x, y  y )
2

y 2
y 2
Let Dx=Dy=1pixel and x and y be the vertical and
horizontal axes. Then the above equations represent the
convolution sums for the pixel (x,y) in the output image
obtained as a result of the convolutions of the input
image I(x,y) with the filter kernels
1
hx   2, hy  1  2 1
 1 
respectively.
Image Filtering
Edge Detection(Enhancement)
Image Filtering
Edge Detection (Enhancement)

The Laplacian is given by:

 I ( x, y )  I ( x, y )
2 2
I ( x, y )  
x 2
y 2
Image Filtering
Edge Detection (Enhancement)
 2 I ( x, y )  2 I ( x, y ) I ( x  x, y )  2 I ( x, y )  I ( x  x, y ) I ( x, y  y )  2 I ( x, y )  I ( x, y  y )
I ( x, y )    
x 2 y 2 x 2 y 2
Assuming that x=y=1pixel, the above represents the
convolution sum for the pixel (x,y) in the output image
obtained as a result of the convolution of the input image
I(x,y) with the filter kernel

 0 1 0 

h   1 4  1 
 0  1 0 
which is called the Laplacian Edge Enhancement Kernel.
Image Filtering
Edge Detection (Enhancement)
Now applying Euler’s forward difference in the ‘y’ direction
I ( x  x, y  y )  2 I ( x, y  y )  I ( x  x, y  y ) I ( x  x, y )  2 I ( x, y )  I ( x  x, y )

 ( I ( x, y ))
2
x 2
x 2

yx 2 y

and finally applying Euler’s backward difference in the ‘y’ direction

I ( x  x, y  y )  2 I ( x, y  y )  I ( x  x, y  y ) I ( x  x, y )  2 I ( x, y )  I ( x  x, y )



x 2 x 2
 4 I ( x, y ) y

x 2 y 2 y
I ( x  x, y )  2 I ( x, y )  I ( x  x, y ) I ( x  x, y  y )  2 I ( x, y  y )  I ( x  x, y  y )

x 2 x 2
y

y
Image Filtering
Edge Detection (Enhancement)

 4 I ( x, y ) I ( x  x, y  y )  2 I ( x, y  y )  I ( x  x, y  y )  2 I ( x  x, y )  4 I ( x, y )  2 I ( x  x, y )  I ( x  x, y  y )  2 I ( x, y  y )  I ( x  x, y  y )



x 2 y 2 x 2 y 2

Assuming that x=y=1pixel, the above represents the


convolution sum for the pixel (x,y) in the output image
obtained as a result of the convolution of the input image
I(x,y) with the filter kernel.

 1 2 1 

h    2 4  2
 1  2 1 
Image Filtering
Edge Detection (Enhancement)

 0 1 0 

h   1 4  1 
 0  1 0 
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)

 1 2 1 

h    2 4  2
 1  2 1 
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Edge Detection (Enhancement)
Image Filtering
Close all
I = zeros(256,256);
r = 75;
r2 = r*r;
x = -r;
cx=120;
cy=120;
while x < r
y1 = (sqrt(r2-x*x)+cx);
y2 = (-sqrt(r2-x*x)+cx);
y3 = (sqrt(r2-x*x)+cy);
y4 = (-sqrt(r2-x*x)+cy);
for i = cy:round(y1)
I(i,x+cy) = 1;
end
Image Filtering
for i = round(y2):cy
I(i,x+cy) = 1;
end
I(x+cx,round(y3)) = 1;
I(x+cx,round(y4)) = 1;
x = x+1;
end
h = [1/8 1/8 1/8
1/8 1 1/8
1/8 1/8 1/8]
Iout=conv2(I,h);
imshow(I,'notruesize'),title('Original Image')
figure,mesh(I),title('Original Image')
Image Filtering
F=fft2(I,256,256);
F=fftshift(F);
F2=abs(F);
figure,imshow(F2,[0
5],'notruesize');colormap(jet);colorbar,titl
e('FT Original Image')
figure,mesh(F2),title('FT of Original Image')
figure,imshow(h,[-.125
1],'notruesize'),title('Filter Kernel')
figure,mesh(h),title('Filter Kernel')
H=fft2(h,256,256);
H=fftshift(H);
H2=abs(H);
Image Filtering
Close all
I = zeros(256,256);
r = 75;
r2 = r*r;
x = -r;
cx=120;
cy=120;
while x < r
y1 = (sqrt(r2-x*x)+cx);
y2 = (-sqrt(r2-x*x)+cx);
y3 = (sqrt(r2-x*x)+cy);
y4 = (-sqrt(r2-x*x)+cy);
for i = cy:round(y1)
I(i,x+cy) = 1;
end
Image Filtering
for i = round(y2):cy
I(i,x+cy) = 1;
end
I(x+cx,round(y3)) = 1;
I(x+cx,round(y4)) = 1;
x = x+1;
end
h = [1/8 1/8 1/8
1/8 1 1/8
1/8 1/8 1/8]
Iout=conv2(I,h);
imshow(I,'notruesize'),title('Original Image')
figure,mesh(I),title('Original Image')
Image Filtering
F=fft2(I,256,256);
F=fftshift(F);
F2=abs(F);
figure,imshow(F2,[0
5],'notruesize');colormap(jet);colorbar,titl
e('FT Original Image')
figure,mesh(F2),title('FT of Original Image')
figure,imshow(h,[-.125
1],'notruesize'),title('Filter Kernel')
figure,mesh(h),title('Filter Kernel')
H=fft2(h,256,256);
H=fftshift(H);
H2=abs(H);
Image Filtering
figure,imshow(H2,[0
1.5],'notruesize');colormap(jet);colorbar,title('FT
of Filter Kernel')
figure,mesh(H2),title('FT of Filter Kernel')
figure,imshow(Iout,'notruesize'),title('Filtered
Image')
figure,mesh(Iout),title('Filtered Image')

I2=fft2(Iout,256,256);
I2=fftshift(I2);
I2=abs(I2);
figure,imshow(I2,[0 5],'notruesize');colormap(jet);
colorbar,title('FT of Filtered Image')
figure,mesh(I2),title('FT of Filtered Image')
Image Filtering

1 / 10 1 / 10 1 / 10 1 / 10 1 / 10 1 / 10
1 / 10 1 / 5 1 / 10 1 / 10 2 / 5 1 / 10
   
1 / 10 1 / 10 1 / 10 1 / 10 1 / 10 1 / 10
Image Filtering
Middle row of the mask:

1/ 4 1 / 2 1 / 4
Yields FIR digital filter with the transfer function:

H ( z )  0.25 z  0.5  0.25 z 1


consisting of a causal and anti-causal part.

The frequency response :

H (e j )  0.5  0.5 cos 


Image Filtering
 Now let us generate an IIR structure to do
the same LP filtering.

h1 (n)   nu (n) and h2 (n)    nu (n  1)

represent, respectively, causal and anti-causal


impulse responses.

 Combination of h1(n) and h2(n) yields the


following impulse response:
Image Filtering
h(n)  h1 (n)  h2 (n)
Image Filtering

Z-transform of h(n)

1 z
H (z)  H 1(z)  H 2 (z)  
1   z 1 1  z

1 2
H ( z) 
 z 1  1   2  z
Image Filtering
Frequency response
1   2
H ( e j ) 
1   2  2 cos 
Image Filtering
Difference Equation
1 2 V ( z)
H ( z)  
 z  1    z U ( z )
1 2

 v ( n  1)  (1   2 ) v ( n )   v ( n  1)  (1   2 )u ( n )  0

This difference equation can be considered to be


obtained through the following products and sum:
Image Filtering

 (1 )2
 0 1 2
0
x x x x x x
y ( n  1) y (n) y ( n  1) x ( n  1) x ( n ) x ( n  1)
+

v(n 1)  (1   2 )v(n)  v(n  1)  (1   2 )u(n)


Image Filtering
A[ (1 )
2
] and B  [0 1 2
0]
are called templates.
A  ak k    : feedback cloning template

B  bk k    : feedforward cloning template.


A template shows the relationship of one sample of


the signal to its neighbours.
In the above case:
the signals v(n) and u(n) are 1-D signals
and
the neighbourhood  of the template is 1,
which leads to
a second-order difference equation.
Image Filtering

 Now defining:

G0  (1   ) 2 , G1  

 The difference equation

 v ( n  1)  (1   2 ) v ( n )   v ( n  1)  (1   2 )u ( n )  0

yields:
Image Filtering
G1v ( n  1)  (G0  2G1 )v ( n)  G1v ( n  1)  (1   2 )u ( n)  0
Which represents the node equation of the following
resistive circuit:

where
v(n): the node voltage of node n (output)
 u(n): the current source connected to node n,
where, u(n) is the input and 2
(1   )
 
2
.

Image Filtering
The above can be extended to the 2-D case using the
2-D templates:
0  0 0 0 0
A    (1  4 2 )   , B   0 1  4 2 0 
 0  0   0 0 0 

 v ( m , n  1)  (1  4 2 ) v ( m , n )   v ( m  1, n )   v ( m  1, n )   v ( m , n  1)  (1  4 2 )u ( m , n )  0
1

a
k ,l  1
k ,l v(m  k , n  l )  bu (m, n)  0

where
A  ak ,l 1k ,l  1 : feedback cloning template
b : feedforward cloning template
Image Filtering
Z-transform of h(m,n)

V (zx , zy ) 1  4 2
H (zx , zy )  
U (zx, zy )   z y 1  (1  4  2 )   z x   z x 1   z y

Frequency response

j x j
j x j Y (e ,e y
) 1  4 2
H (e ,e y
) j y

X (e j x
,e ) 1  4 2
 2  cos  x  2  cos  y
Image Filtering
>0 yields low-pass filters
  0.45   0.25
Image Filtering
<0 yields high-pass filters
  0.45   0.25
Image Filtering
Such 2-D transfer functions can be obtained
using resistive grid structures.

 v ( m , n  1)  (1  4 2 ) v ( m , n )   v ( m  1, n )
  v ( m  1, n )   v ( m , n  1)  (1  4 2 )u ( m , n )  0

Defining:
G 0  (1  2 ) 2
, G1  
yields:
Image Filtering
G1v ( m, n  1)  G0 v ( m, n)  G1v ( m  1, n)  G1v ( m  1, n)  G1v ( m, n  1)  (1  4 2 )u ( m, n)  0

which represents the node equation of


node (m,n) of the resistive circuit:
Image Filtering

Image processing can be carried out by


associating each node with a pixel of the
image and using photosensors.

The current source at node (m,n) will then


represent the grey level of pixel (m,n) in the
input image.
Image Filtering
Band-pass structures
Shift the low-pass

1 2
H LP (e j ) 
 e  j  1   2  e j

by o.
1   2
H LP (e j (  0 ) ) 
 e  j (  0 )  1   2  e j (  0 )
Image Filtering
Shifting the LP by  yields BP.
j ( 0 ) 1 2
H BP (e j )  H LP (e )
 1   2  2 cos(  o )
Image Filtering
The corresponding H(z):

1 2
H BP ( z ) 
 e j o z 1  1   2  e  j o z
leading to the templates:
A  [ e jo  (1  2 )  e j ]
o
and B  [0 1  2 0]


A a e k 
 jko 
k  
: feedback cloning template;

B  b e k 
 jko 
k  
: feedforward cloning template.
Image Filtering
Similarly shifting of 2-D LP yields:
j x j y j ( x  xo ) j ( y  yo ) 1  4 2
H BP (e ,e )  H LP (e ,e )
1  4 2  2 cos( x   xo )  2 cos( y   yo )

This can be normalised to:

j x j y (1  2 ) 2 / 
H BP (e ,e )
1  4 2
 2 cos( x   xo )  2 cos( y   yo )

2-D band-pass filter tuned to the centre
frequency (xo,yo)

Now let:   (1  2 ) / 
2 2
Image Filtering
j y 2
H BP (e j x , e )
4  2  2 cos( x   xo )  2 cos( y   yo )

The templates :
 j
 0 e y0 0 
 j x 0  j x 0 
A  e  (4   ) 2 e 
 0 e
j y 0
0 

0 0 0 
B  0 2 0
0 0 0
Image Filtering
 In practice, every node will also have a
parasitic capacitor connected to the ground.
 Considering these capacitors the spatial
difference equation becomes :
dv(n)
C  G1v(n 1 )  (G0  2G1 )v(n) G1v(n  1 )  ( 1  α 2 )u(n) 0
dt 1
v(n)   a v(n  k )  bu (n)
k  1
k

which represents the differential equation of


the nth cell of the 1-D Linear Cellular Neural
Network, where v(n) is the nth state voltage.
Image Filtering

CNN Filters

A circuit realisation
Image Filtering

Extension with capacitors in 2-D


dv ( m, n)
C  G1v( m, n  1)  G0 v (m, n)  G1v (m  1, n)  G1v ( m  1, n)  G1v (m, n  1)  (1  4 2 )u (m, n)  0
dt

or
1
v(m, n)  a
k ,l  1
k ,l v(m  k , n  l )  bu (m, n)

which represents the spatial differential


equation for the cell (m,n) of the 2-D Cellular
Neural Network, where v(m,n) is the state.
Image Filtering
 The 1-D and 2-D spatial difference equations
are obtained from these differential equations
when:
dv(n) dv(m, n)
0 , 0
dt dt

respectively.
 This happens when the states v(n), v(m,n)
become constant
 i.e., settle to a solution.
 That is, the processing of the image has been
completed.
Image Filtering
clear all;
close all;
I = zeros(64,64);
I(15:25,15:25)=1;
I(15:25,35:45)=1;
I(35:45,15:25)=1;
I(35:45,35:45)=1;
a = zeros(128,128);
a=[1010101010101010]
z = a;
for k = 1:7
z = [z a] ;
end
l = z;
for k = 1:127
z = [z;l];
end
z = 1 - z;
figure, imshow(z,'notruesize')
figure, imshow(I,'notruesize')
Image Filtering
clear all;
close all;
I = zeros(64,64);
I(15:25,15:25)=1;
I(15:25,35:45)=1;
I(35:45,15:25)=1;
I(35:45,35:45)=1;
a = zeros(128,128);
b = zeros(128,128);
a = [ 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0];
b=[1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0];
z = a;
s=b;
for k = 1:7
z = [z a] ;
s=[s b];
end
l = z;
j=s;
for k = 1:127
z = [z;l];
s=[s;j];
end
z = 1 - z;
figure, imshow(z,'notruesize')
figure, imshow(I,'notruesize')
figure, imshow(s,'notruesize')
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Noise Removal

Digital images are prone to a variety of types of noise. There


are several ways that noise can be introduced into an image,
depending on how the image is
created.

For example:
•If the image is scanned from a photograph made on film, the
film grain is a source of noise. Noise can also be the result
Of damage to the film, or be introduced by the scanner itself.
•If the image is acquired directly in a digital format, the
mechanism for gathering the data (such as a CCD detector) can
introduce noise.
•Electronic transmission of image data can introduce noise.
Image Filtering
The MATLAB toolbox provides a number of different
ways toremove or reduce noise in an image. Different
methods are better for different kinds of noise. The
Methods available include:

•Linear filtering
•Median filtering
•Adaptive filtering

Also, in order to simulate the effects of some of the


problems listed above, the toolbox provides the imnoise
function, which you can use to add various types of
noise to an image. The examples in this section use this
function.
Image Filtering
Median Filtering
Median filtering is similar to using an averaging filter, in that
each output pixel is set to an “average” of the pixel values in
the neighborhood of the corresponding input pixel. However,
with median filtering, the value of an output pixel is
determined by the median of the neighborhood pixels, rather
than the mean. The median is much less sensitive than the
mean to extreme values (called outliers). Median filtering is
therefore better able to remove these outliers without
reducing the sharpness of the image.
Image Filtering
The medfilt2 function implements median
filtering. The example below compares using an
averaging filter and medfilt2 to remove salt and
Pepper noise. This type of noise consists of random
pixels being set to black or white (the extremes of
the data range). In both cases the size of the
neighborhood used for filtering is 3-by-3.

First, read in the image and add noise to it.


I =imread('eight.tif');
J =imnoise(I,'salt & pepper',0.02);
imshow(I)
figure,imshow(J)
Image Filtering

Before and After Adding Salt-and-Pepper Noise to Eight.tif


Image Filtering
Now filter the noisy image and display
the results. Notice that medfilt2 does
a better job of removing noise, with less
blurring of edges.
K =filter2(fspecial('average',3),J)/255;
L =medfilt2(J,[3 3 ]);
figure,imshow(K)
figure,imshow(L)
Image Filtering

Noisy Version of Eight.tif Filtered with Averaging Filter (left)


and Median Filter (right)
Image Filtering
close all
I=[3 6 6 10 12 45 1 23 9;
210 100 25 6 12 2 89 10 5;
1 1 2 5 100 8 3 19 11;
2 5 18 32 4 15 5 150 12;
1 1 3 90 3 8 9 3 56;
2 2 2 0 2 87 12 14 12;
1 1 1 1 0 3 8 15 1;
12 65 3 2 8 4 0 1 1;
1 1 1 1 1 1 11 1 1]
imshow(I,[0 210],'notruesize'),title('Input Image I');
figure,mesh(I),title('Input Image I')
L=medfilt2(I,[5 5]),title('Median filtered Image L');
figure,imshow(L,[0 11],'notruesize')
figure,mesh(L),title('Median filtered Image L')
h=ones(3,3)/9;
Iout=conv2(I,h)
figure,imshow(Iout,[0 40],'notruesize'),title('Output Image I') ;
figure,mesh(Iout),title('Output Image I')
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Image Filtering
Adaptive Filtering
The wiener2 function applies a Wiener filter (a type
of linear filter) to an image adaptively, tailoring itself
to the local image variance. Where the variance is
large, wiener2 performs little smoothing. Where the
variance is small, wiener2 performs more smoothing.

This approach often produces better results than


linear filtering. The adaptive filter is more selective
than a comparable linear filter, preserving edges and
other high frequency parts of an image. In addition,
there are no design tasks; the wiener2 function
handles all preliminary computations, and
implements the filter for an input image. wiener2 ,
however, does require more computation time than
linear filtering.
Image Filtering

wiener2 works best when the noise is constant-power


(“white”) additive noise, such as Gaussian noise. The
example below applies wiener2 to an image of Saturn
that has had Gaussian noise added.
I =imread('saturn.tif');
J =imnoise(I,'gaussian',0,0.005);
K =wiener2(J,[5 5 ]);
imshow(J)
figure,imshow(K)

Try running nrfilt demo for an interactive demonstration


of adaptive filtering to remove noise.
Image Filtering
Image Filtering
Image Filtering
Image Filtering

close all
I =imread('saturn.tif');
J =imnoise(I,'gaussian',0,0.005);
K =medfilt2(J,[3 3 ]);
M =wiener2(J,[5 5 ]);
imshow(I),title('Original Image');
figure,imshow(J),title('Noisy Image');
figure,imshow(K),title('Median filtered Image');
figure,imshow(M),title('Wiener filtered Image');
Image Filtering
Image Filtering
Image Deblurring

Understanding Deblurring
This section provides some background on deblurring
techniques. The section includes these topics:
•“Causes of Blurring”
•“Deblurring Model”

Causes of Blurring
The blurring, or degradation, of an image can be caused
by many factors:
•Movement during the image capture process, by the
camera or, when long exposure times are used, by the
subject
Image Filtering
•Out-of-focus optics, use of a wide-angle
lens, atmospheric turbulence, or a short
exposure time, which reduced the number of
photons captured

•Scattered light distortion in confocal


microscopy
Image Filtering
Deblurring Model
A blurred or degraded image can be approximately
described by this equation
g = Hf + n, where:
g = The blurred image
H = The distortion operator, also called the point-
spread function (PSF). This function, when
convolved with the image, creates the distortion
f = The original true image
n = Additive noise, introduced during image
acquisition, that corrupts the image
Image Filtering
Note: The image f really doesn’t exist.
This image represents what you would have
if you had perfect image acquisition
conditions.

The Importance of the PSF


Based on this model, the fundamental task of
deblurring is to deconvolve the blurred image
with the PSF that exactly describes the
distortion.
Image Filtering

Note: The quality of the deblurred image is


mainly determined by knowledge of the PSF.

To illustrate, this example takes a clear image and


deliberately blurs it by convolving it with a PSF. The
example uses the fspecial function to create a
PSF that simulates a motion blur, specifying the length
of the blur in pixels, (LEN=31 ), and the angle of the
blur in degrees (THETA=11 ).
Image Filtering

Once the PSF is created, the example uses the


imfilter function to convolve the PSF with the
original image, I , to create the blurred image,
Blurred . (To see how deblurring is the reverse
of this process, using the same images, see
“Deblurring with the Wiener Filter”.)
Image Filtering
I =imread('flowers.tif');
I =I(10+[1:256 ],222+[1:256 ],:);%crop the
image
figure;imshow(I);title('Original Image');
LEN =31;
THETA =11;
PSF =fspecial('motion',LEN,THETA);
Blurred =imfilter(I,PSF,'circular','conv');
figure;imshow(Blurred);title('Blurred Image');
Image Filtering
Image Filtering
Using the Deblurring Functions
The toolbox includes four deblurring
functions,listed here in order of
complexity:
•deconvwnr— Implements deblurring
using the Wiener filter
•deconvreg — Implements deblurring
using a regularized filter
• deconvlucy-Implements deblurring
using the Lucy-Richardson algorithm
Image Filtering
All the functions accept a PSF and the blurred
image as their primary arguments. The deconvwnr
function implements a least squares solution. The
deconvreg function implements a constrained least
squares solution, where you can place constraints
on the output image (the smoothness requirement
isthe default). With either of these functions, you
should provide some information about the noise to
reduce possible noise amplification during
deblurring.
Image Filtering
The deconvlucy function implements an
accelerated, damped Lucy-Richardson
algorithm. This function performs
multiple iterations, using optimization
techniques and Poisson statistics. With
this function, you do not, need to
provide information about the additive
noise in the corrupted image.
Image Filtering
The deconvblind function implements the blind
deconvolution algorithm, which performs
deblurring without knowledge of the PSF.
When you call, deconvblind , you pass as an
argument your initial guess at the PSF. The
deconvblind function returns a restored PSF in
addition to the restored image.
The implementation uses the same damping
and iterative model as the deconvlucy
function.
Image Filtering
Note You may need to perform many iterations of the
deblurring process,varying the parameters you
specify to the deblurring functions with each iteration,
until you achieve an image that, based on the limits of
Your information, is the best approximation of the
original scene. Along the way,you must make
numerous judgements about whether newly
uncovered features in the image are features of the
original scene, or simply artifacts of the deblurring
process.
Image Filtering
For information about creating your own
deblurring functions, see “Creating
Your Own Deblurring Functions” . To avoid
“ringing” in a deblurred image,
you can use the edgetaper function to
preprocess your image before passing it to the
deblurring functions. See “Avoiding Ringing in
Deblurred Images” on page 12-20 for more
information.
Image Filtering
Deblurring with the Wiener Filter

Use the deconvwnr function to deblur an image


using the Wiener filter. Wiener deconvolution can
be used effectively when the frequency
characteristics of the image and additive noise are
known, to at least some degree. In the absence of
noise, the Wiener filter reduces to the ideal inverse
filter.
This example deblurs the blurred flower image,
created in “Deblurring Model” specifying the same
PSF function that was used to create the blur.
Image Filtering
I =imread('flowers.tif');
I =I(10+[1:256 ],222+[1:256 ],:);%crop the image
figure;imshow(I);title('Original Image');
%create PSF
LEN =31;
THETA =11;
PSF =fspecial('motion',LEN,THETA);
%blur the image
Blurred =imfilter(I,PSF,'circular','conv');
figure;imshow(Blurred);title('Blurred Image');
%deblur the image
wnr1 =deconvwnr(Blurred,PSF);
figure;imshow(wnr1);
title('Restored,True PSF');
Image Filtering
Image Filtering
This example illustrates the importance of knowing the PSF, the
function that caused the blur. When you know the exact PSF, as
in this example, the results of deblurring can be quite effective.

Refining the Result


You can affect the deconvolution results by providing values for
the optional arguments supported by the deconvwnr function.
Using these arguments you can specify the noise-to-signal
power value and/or provide autocorrelation functions to help
refine the result of deblurring. To see the impact of these
optional arguments, view the Image Processing Toolbox
Deblurring Demos.
2-D Fourier Transforms
Close all
I = zeros(256,256);
r = 75;
r2 = r*r;
x = -r;
cx=120;
cy=120;
while x < r
y1 = (sqrt(r2-x*x)+cx);
y2 = (-sqrt(r2-x*x)+cx);
y3 = (sqrt(r2-x*x)+cy);
y4 = (-sqrt(r2-x*x)+cy);
for i = cy:round(y1)
I(i,x+cy) = 1;
end
2-D Fourier Transforms
for i = round(y2):cy
I(i,x+cy) = 1;
end
I(x+cx,round(y3)) = 1;
I(x+cx,round(y4)) = 1;
x = x+1;
end
imshow(I,'notruesize'),title('Original Image')
figure,mesh(I),title('Original Image')
2-D Fourier Transforms
F=fft2(I,256,256);
F=fftshift(F);
F2=abs(F);
figure,imshow(F2,[0 5],'notruesize');
colormap(jet);colorbar,title('FT Original Image')
figure,mesh(F2),title('FT of Original Image')
figure,imshow(h,[-.125 1],'notruesize'),title('Filter
Kernel')
figure,mesh(h),title('Filter Kernel')
H=fft2(h,256,256);
H=fftshift(H);
H2=abs(H);
2-D Fourier Transforms
figure,imshow(Iout,'notruesize'),title('Output Image')
figure,mesh(Iout),title('Output Image')
Fout=fft2(Iout,256,256);
F=fftshift(Fout);
figure,imshow(F,[0 5],'notruesize');
colormap(jet);colorbar,title('FT Output Image')
figure,mesh(F2),title('FT of Output Image')
2-D Fourier Transforms
2-D Fourier Transforms
2-D Fourier Transforms
2-D Fourier Transforms

Das könnte Ihnen auch gefallen