MVlec8

Embed Size (px)

Citation preview

  • 8/11/2019 MVlec8

    1/11

    Lecture 8: Filters in Frequency Domain

    If b is an image, then its Fourier transform will reflect the frequencies of the

    periodic parts of the image. By masking or filtering out the unwanted frequencies one can

    obtained a new image by applying the inverse Fourier transformation. A filteris a matrix

    with the same dimension as the Fourier transform of the padded image. The components

    of the filter usually vary from 0 to 1. If the component is 1, then the frequency is allowed

    to pass; if the component is 0, then the frequency is tossed out. Let Filterrepresent such a

    matrix. Then the filtered imageis given byNewbin

    2( .* 2( ,2* 1,2* 1) ).Newb ifft Filter fft b nx ny=

    The Matlab codes fftsine.m and fftsine2d.m in the previous lectures illustrated this for alow frequency sine wave with higher frequency sine noise.

    Three important types of filters are low pass, high pass and band-reject. They are

    depicted in the following graphic where the low frequencies have been shifted to the

    center and one is viewing the diagonal section of the filter matrix.

    low passhigh

    pass

    band-reject

    frequency

    The above filters have jumps and are often called ideal filters. The important

    filters that make use of polynomial and exponential approximations are the Butterworth

    and Gaussian filters. Let w, d0be given and dist(i,j) = ((i-(nx+1))2+ (j-(ny+1))

    2)1/2

  • 8/11/2019 MVlec8

    2/11

    Butterworth Band-reject Filter:

    2 2 2

    1( , )

    1 ( ( , ) /( ( , ) 0 )) nFilter i j

    dist i j w dist i j d =

    +

    .

    Gaussian Bandreject Filter:2 2 21(( ( , ) 0 ) / ( , ) )

    2( , ) 1 .dist i j d dist i j w

    Filter i j e

    =

    The parameters wand d0control the width and location of the band. In the Butterworth

    band-reject filter the exponent n controls the steepest of the boundaries in the band of

    frequencies to be rejected. There are similar versions of high and low pass filters.

    The Matlab code filter_ncsu.m uses a low pass filter to mask the noise from sine

    and cosine functions with frequency equal to 80 (160 in the padded Fourier transform).

  • 8/11/2019 MVlec8

    3/11

    Matlab Code filter_ncsu.m

    cl ear ;ncsu = [ bi gn( 5) bi gc( 7) bi gs( 9) bi gu( 11) ] ;newncsu = 20*ncsu;

    [ nx ny] = si ze( newncsu) ;nxnynewncsu = newncsu( nx: - 1: 1, : ) ;newncsu1 = ui nt8(newncsu) ;i mwr i t e( newncsu1, ' ncsu. j pg' ) ;u = newncsu;f or i = 1: nx % Thi s i s NCSU wi t h per i odi c noi se.

    f or j = 1: nyu( i , j ) = u( i , j ) + . . .

    15. *( 1+si n( 2*pi *( ( i - 1) / nx) *80) ) +. . .15. *( 1+si n( 2*pi *( ( j - 1) / ny) *80) ) ;

    end

    endsi nencsu = ui nt 8( u) ;i mwr i t e( si nencsu, ' si nencsu. j pg' ) ;f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;f f t u = f f t s hi f t ( f f t u) ;subpl ot ( 2, 2, 1) ;mesh( u' ) ;subpl ot ( 2, 2, 2) ;mesh( l og( 1+( abs( f f t u) ) ) ) ;f i l t er = ones( 2*nx- 1, 2*ny- 1) ;d0 = 150; % Use i deal l ow pass f i l t er .f or i = 1: 2*nx- 1

    f or j =1: 2*ny- 1di st = ( ( i - ( nx+1) ) 2 + ( j - ( ny+1) ) 2) . 5;

    i f di st > d0f i l t er ( i , j ) = 0;

    endend

    endsubpl ot ( 2, 2, 3) ;mesh( f i l t er ) ;f i l _ncsu = f i l t er . * f f tu;subpl ot ( 2, 2, 4) ;mesh( l og( 1+abs( f i l _ncsu) ) ) ;f i l _ncsu = i f f t shi f t ( f i l _ncsu) ;f i l _ncsu = i f f t 2( f i l _ncsu, 2*nx- 1, 2*ny- 1) ;f i l _ncsu = r eal ( f i l _ncsu( 1: nx, 1: ny) ) ;

    f i l _ncsu = ui nt 8( f i l _ncsu) ;i mwr i te( f i l _ncsu, ' s i nencsu_f i l . j pg' ) ;

  • 8/11/2019 MVlec8

    4/11

    The filtered image sinencsu_fil.jpg is not entirely satisfactory. Another approach

    is to use a band-reject filter where one may need to experiment with the width, w, and the

    location, d0, of the band. The Matlab code filter_bu.m uses the Butterworth band-reject

    filter and may be applied to either the noisy big sine wave or the noisy ncsu.

  • 8/11/2019 MVlec8

    5/11

    Matlab Code filter_bu.m

    cl ear ;ncsu = [ bi gn( 5) bi gc( 7) bi gs( 9) bi gu( 11) ] ;newncsu = 20*ncsu;[ nx ny] = si ze( newncsu) ;

    nxnynewncsu = newncsu( nx: - 1: 1, : ) ;newncsu1 = ui nt8( newncsu) ;i mwr i t e( newncsu1, ' ncsu. j pg' ) ;u = newncsu;f or i = 1: nx

    f or j = 1: ny% Thi s i s t he bi g wave wi t h per i odi c noi se.u( i , j ) = 0+100*( 1+si n( 2*pi *( ( j - 1) / ny) *5) ) + . . .

    15. *( 1+si n( 2*pi *( ( i - 1) / nx) *80) ) +. . .15. *( 1+si n( 2*pi *( ( j - 1) / ny) *80) ) ;

    % Thi s i s NCSU wi t h per i odi c noi se.%u( i , j ) = u( i , j ) + . . .

    15. *( 1+si n( 2*pi *( ( i - 1) / nx) *80) ) +. . .15. *( 1+si n( 2*pi *( ( j - 1) / ny) *80) ) ;

    endendsi nencsu = ui nt 8( u) ;i mwr i t e( si nencsu, ' si nencsu. j pg' ) ;f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;f f t u = f f t s hi f t ( f f t u) ;subpl ot ( 2, 2, 1) ;mesh( u' ) ;subpl ot ( 2, 2, 2) ;mesh( l og( 1+( abs( f f t u) ) ) ) ;f i l t er = ones( 2*nx- 1, 2*ny- 1) ;

    d0 = 160; % Use But t er wor t h band- r ej ect f i l t er .n = 4;w = 20;f or i = 1: 2*nx- 1

    f or j =1: 2*ny- 1di st = ( ( i - ( nx+1) ) 2 + ( j - ( ny+1) ) 2) . 5;i f di st ~= d0

    f i l t er ( i , j ) = 1/ ( 1 + ( di st*w/ ( di st 2 - d0 2) ) ( 2*n) ) ;el se

    f i l t er ( i , j ) = 0;end

    endendsubpl ot ( 2, 2, 3) ;

    mesh( f i l t er ) ;f i l _ncsu = f i l t er . * f f tu;subpl ot ( 2, 2, 4) ;mesh( l og( 1+abs( f i l _ncsu) ) ) ;f i l _ncsu = i f f t shi f t ( f i l _ncsu) ;f i l _ncsu = i f f t 2( f i l _ncsu, 2*nx- 1, 2*ny- 1) ;f i l _ncsu = r eal ( f i l _ncsu( 1: nx, 1: ny) ) ;f i l _ncsu = ui nt 8( f i l _ncsu) ;i mwr i te( f i l _ncsu, ' s i nencsu_f i l . j pg' ) ;

  • 8/11/2019 MVlec8

    6/11

    Another application of the band-reject filter is to the noisy aerial photograph. This

    image suffers from too much light an exposure and from banded sine and cosine noise.

    The light is modified by use of the power transformation with the power equal to two,and then the Butterworth band-reject filter is used to reduce to noise.

  • 8/11/2019 MVlec8

    7/11

    Matlab Code filter_aerial.m

    cl ear ;aer i al = i mr ead( ' Fi g3. 09( a) . j pg' ) ;aer i al = doubl e( aer i al ) ;

    [ nx ny] = si ze( aer i al ) ;nxnyu = aer i al ;

    f or i = 1: nxf or j = 1: ny

    % Thi s i s aer i al wi t h per i odi c noi se.u( i , j ) = u( i , j ) + . . .

    5. *( 1+si n( 2*pi *( ( i - 1) / nx) *200) ) +. . .5. *( 1+si n( 2*pi *( ( j - 1) / ny) *200) ) +. . .5. *( 1+cos( 2*pi *( ( i - 1) / nx+( j - 1) / ny) *141) ) +. . .5. *(1+si n( 2*pi *(( i - 1) / nx- ( j - 1) / ny) *141) ) ; ;

    endendsi neaer i al = ui nt 8( u) ;i mwr i t e( s i neaer i al , ' s i neaer i al . j pg' ) ;

    c = 1. ; % Use t he power t r ansf ormat i on t o dar ken.gamma = 2;f _f p =255*c* ( u/ 255) . gamma;u = f _f p;f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;f f t u = f f t s hi f t ( f f t u) ;subpl ot ( 1, 2, 1)mesh( l og( 1+( abs( f f t u) ) ) ) ;

    f i l t er = ones( 2*nx- 1, 2*ny- 1) ;d0 = 400; % Use But t er wort h band r ej ect f i l t er .n = 4;w = 20;f or i = 1: 2*nx- 1

    f or j =1: 2*ny- 1di st = ( ( i - ( nx+1) ) 2 + ( j - ( ny+1) ) 2) . 5;i f di st ~= d0

    f i l t er ( i , j ) = 1/ ( 1 + ( di st*w/ ( di st 2 - d0 2) ) ( 2*n) ) ;el se

    f i l t er ( i , j ) = 0;end

    end

    endf i l _ aer i al = f i l t er . * f f t u;subpl ot ( 1, 2, 2)mesh( l og( 1+abs( f i l _aer i al ) ) ) ;

    f i l _aer i al = i f f t shi f t ( f i l _aer i al ) ;f i l _aer i al = i f f t2( f i l _aer i al , 2*nx- 1, 2*ny- 1) ;f i l _aer i al = real ( f i l _aer i al ( 1: nx, 1: ny) ) ;f i l _aer i al = ui nt8( f i l _aer i al ) ;i mwr i te( f i l _aer i al , ' si neaer i al _ f i l . j pg' ) ;

  • 8/11/2019 MVlec8

    8/11

  • 8/11/2019 MVlec8

    9/11

    The Matlab code filter_micro.m uses a high pass filter to give emphasis to the

    higher frequencies in the image of a damaged electronic chip. The high pass image is

    then added to the original image so as to obtain a sharper image. The reader may find it

    interesting to experiment with width and frequency threshold of the Butterworth or the

    Gaussian high pass filters. Also, it is interesting to compare this sharpening, which is

    done in the frequency domain, with the sharpening done in the space domain as in the

    third lecture .

    FFT of Image High Pass Filter of FFT Image

  • 8/11/2019 MVlec8

    10/11

    Matlab Code filter_micro.m

    cl ear ;

    mi cro = i mr ead( ' Fi g4. 04( a) . j pg' ) ;mi cr o = doubl e(mi cr o) ;[ nx ny] = si ze( mi cr o);nxnyu = mi cr o;mi cr o = ui nt 8( u) ;i mwr i t e( mi cro, ' mi cro. j pg' ) ;f f t u = f f t 2( u, 2*nx- 1, 2*ny- 1) ;f f t u = f f t s hi f t ( f f t u) ;subpl ot ( 1, 2, 1)mesh( l og( 1+( abs( f f t u) ) ) ) ;% Use But t er wor t h or Gaussi an hi gh pass f i l t er .f i l t er = ones( 2*nx- 1, 2*ny- 1) ;

    d0 = 100;n = 4;f or i = 1: 2*nx- 1

    f or j =1: 2*ny- 1di st = ( ( i - ( nx+1) ) 2 + ( j - ( ny+1) ) 2) . 5;% Use But t er wor t h hi gh pass f i l t er .f i l t er ( i , j ) = 1/ ( 1 + ( di st / d0) ( 2*n) ) ;f i l t er ( i , j ) = 1. 0 - f i l t er ( i , j ) ;% Use Gaussi an hi gh pass f i l t er .%f i l t er ( i , j ) = exp( - di st 2/ ( 2*d0 2) ) ;%f i l t er ( i , j ) = 1. 0 - f i l t er ( i , j ) ;

    endend

    % Updat e i mage wi t h hi gh f r equenci es.f i l _mi cro = f f tu + f i l t er . * f f tu;subpl ot ( 1, 2, 2)mesh( l og( 1+abs( f i l _mi cro- f f t u) ) ) ;f i l _mi cro = i f f t shi f t ( f i l _mi cro) ;f i l _mi cro = i f f t 2( f i l _mi cro, 2*nx- 1, 2*ny- 1) ;f i l _mi cro = r eal ( f i l _mi cro( 1: nx, 1: ny) ) ;f i l _mi cro = ui nt 8( f i l _mi cro) ;i mwr i te( f i l _mi cro, ' mi cro_ f i l . j pg' ) ;

  • 8/11/2019 MVlec8

    11/11