图像信号 matlab滤波器设计1 :高通滤波器应用和设计

%--------------------------------------------------------

%函数说明:

%输入:

% image: 读入图片的路径

% D0: 参数D0

% n: 巴特沃思与指数滤波器的阶数

%输出:

% 经过各种滤波后的图片

%--------------------------------------------------------

function HPFilter (image , D0 , n);

%image = \'lena_noise.bmp\';

f = imread(image);

[u,v] = size(f);

mid_x = u/2;

mid_y = v/2;

%D0 = 50;

%----------------------------------------------------------

%高通滤波器的设计

%----------------------------------------------------------

figure;

subplot(2,2,1);

imshow(f);

title(\'原图\');

%-------------------------------

%理想高通滤波器(ILPF)

%-------------------------------

H = zeros(u,v);

for i = 1:u

for j = 1:v

if(sqrt((i-mid_x)^2+(j-mid_y)^2) > D0)

H(i,j) = 1;

end

end

end

g = myfilter (f, H, D0);

subplot(2,2,2);

imshow(g);

title(\'理想高通滤波\');

%-------------------------------

%巴特沃思高通滤波器(BLPF)

%-------------------------------

for i = 1:u

for j = 1:v

H(i,j) = (1+(D0^2/(i^2+j^2))^n)^-1;

end

end

g = myfilter (f, H, D0);

subplot(2,2,3);

imshow(g);

title(\'巴特沃思高通滤波\');

%-------------------------------

%指数高通滤波器(elpf)

%-------------------------------

for i = 1:u

for j = 1:v

H(i,j) = exp(-(D0^2/(i^2+j^2))^n);

end

end

g = myfilter (f, H, D0);

subplot(2,2,4);

imshow(g);

title(\'指数高通滤波\');

关键字:

实验1

j=imread(\'checkerboard.bmp\');

j=double(j);

f=fft2(j);

g=fftshift(f);

figure,subplot(2,2,1);imshow(log(1+abs(g)),[]);

[m,n]=size(f)

for i=1:m

for j=1:n

h(i,j)=(1+(d0^2/(i^2+j^2))^n)^-1

g(i,j)=h(i,j)*g(i,j);

end

end

我们还是从刚萨雷斯的数字图像实例干起吧

??? Error: File: hpfilter.m Line: 28 Column: 32

Unbalanced or misused parentheses or brackets.

>> H1=hpfilter(\'index\',PQ(1),PQ(2),D0);

??? Error: File: hpfilter.m Line: 28 Column: 35

Expression or statement is incorrect--possibly unbalanced (, {, or [.

>> H1=hpfilter(\'index\',PQ(1),PQ(2),D0);

Warning: sorry ,just such filter ,we will progaram others in the future

> In lpfilter at 31

In hpfilter at 23

??? Output argument "H" (and maybe others) not assigned during call to "G:\实验专用图片\信号处理\lpfilter.m (lpfilter)".

Error in ==> lpfilter at 16

[U,V]=dftuv(M,N);

Error in ==> hpfilter at 23

Hlp=lpfilter(type,M,N,D0,n)

>>

>> M=8,N=5;

M =

8

>> N=5

N =

5

>> U,V]=dftuv(M,N);

D=sqrt(U.^2+V.^2);

??? U,V]=dftuv(M,N);

|

Error: Unbalanced or misused parentheses or brackets.

>> [U,V]=dftuv(M,N);

D=sqrt(U.^2+V.^2);

>> D

D =

0 1.0000 2.0000 2.0000 1.0000

1.0000 1.4142 2.2361 2.2361 1.4142

2.0000 2.2361 2.8284 2.8284 2.2361

3.0000 3.1623 3.6056 3.6056 3.1623

4.0000 4.1231 4.4721 4.4721 4.1231

3.0000 3.1623 3.6056 3.6056 3.1623

2.0000 2.2361 2.8284 2.8284 2.2361

1.0000 1.4142 2.2361 2.2361 1.4142

>> D.

??? D.

|

Error: Expression or statement is incomplete or incorrect.

>> D0=65

D0 =

65

>> (D./D0).

??? (D./D0).

|

Error: Unexpected MATLAB operator.

>> H=1./(1+(D./D0).^(2*n));

??? Undefined function or variable \'n\'.

>> n=1

n =

1

>> H=1./(1+(D./D0).^(2*n));

>> H=exp( -( D0/ D.).^n );

??? H=exp( -( D0/ D.).^n );

|

Error: Unbalanced or misused parentheses or brackets.

>> H=exp( -( (D0/ D.)^n). );

??? H=exp( -( (D0/ D.)^n). );

|

Error: Unbalanced or misused parentheses or brackets.

>> H=exp( -(D.^2)./(2*(D0^2) ) );

>> H=exp( -( (D0^n)./ (D.^n). );

??? H=exp( -( (D0^n)./ (D.^n). );

|

Error: Unexpected MATLAB operator.

>> H=exp( -( (D0^n)./ (D.^n) );

??? H=exp( -( (D0^n)./ (D.^n) );

|

Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

>> H=exp( - (D0^n)./ (D.^n) );

Warning: Divide by zero.

>> H

H =

1.0e-006 *

0 0.0000 0.0000 0.0000 0.0000

0.0000 0.0000 0.0000 0.0000 0.0000

0.0000 0.0000 0.0001 0.0001 0.0000

0.0004 0.0012 0.0148 0.0148 0.0012

0.0876 0.1424 0.4873 0.4873 0.1424

0.0004 0.0012 0.0148 0.0148 0.0012

0.0000 0.0000 0.0001 0.0001 0.0000

0.0000 0.0000 0.0000 0.0000 0.0000

>> H=exp( -(D.^2)./(2*(D0^2) ) );

>> H

H =

1.0000 0.9999 0.9995 0.9995 0.9999

0.9999 0.9998 0.9994 0.9994 0.9998

0.9995 0.9994 0.9991 0.9991 0.9994

0.9989 0.9988 0.9985 0.9985 0.9988

0.9981 0.9980 0.9976 0.9976 0.9980

0.9989 0.9988 0.9985 0.9985 0.9988

0.9995 0.9994 0.9991 0.9991 0.9994

0.9999 0.9998 0.9994 0.9994 0.9998

>>

>> H=hpfilter(\'index\',PQ(1),PQ(2),D0);

Warning: Divide by zero.

> In hpfilter at 28

>> H1=hpfilter(\'gaussian\',PQ(1),PQ(2),D0);

Warning: sorry ,just such filter ,we will progaram others in the future

> In hpfilter at 51

??? Output argument "H" (and maybe others) not assigned during call to "G:\实验专用图片\信号处理\hpfilter.m (hpfilter)".

Error in ==> hpfilter at 21

switch type

>> H1=hpfilter(\'ideal\',PQ(1),PQ(2),D0);

>> H1=hpfilter(\'btw\',PQ(1),PQ(2),D0);

>> H1=hpfilter(\'gaussian\',PQ(1),PQ(2),D0);

Warning: sorry ,just such filter ,we will progaram others in the future

> In hpfilter at 52

??? Output argument "H" (and maybe others) not assigned during call to "G:\实验专用图片\信号处理\hpfilter.m (hpfilter)".

Error in ==> hpfilter at 21

switch type

>> H1=hpfilter(\'gaussian\',PQ(1),PQ(2),D0);

??? Output argument "H" (and maybe others) not assigned during call to "G:\实验专用图片\信号处理\hpfilter.m (hpfilter)".

Error in ==> hpfilter at 21

原因是在于hpfilter中用guassian而在lpfilter中用gausssian

switch type

>> H1=hpfilter(\'gaussian\',PQ(1),PQ(2),D0);

>>

>> u=i-g;

??? Error using ==> minus

Integers can only be combined with integers of the same class, or scalar doubles.

>> u=double(i-g);

??? Error using ==> minus

Integers can only be combined with integers of the same class, or scalar doubles.

>> u=double(i)-double(g);

> In thpfilter at 17

??? Output argument "H" (and maybe others) not assigned during call to "G:\实验专用图片\信号处理\thpfilter.m (thpfilter)".

Error in ==> thpfilter at 5

switch type

显然是H 定义出错,主要还是idui矩阵的掌握上,所以修改了一下

>> [U,V]=dftuv(8,5);

D=sqrt(U.^2+V.^2);

>> H=zeros(size(D));

>> H2=~zeros(size(D));;

>> H2=~zeros(size(D));

>> H3=(D-D0)*(1/(D1-D0) );

>> H

H =

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

>> H2

H2 =

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

>> H3

H3 =

-0.1111 -0.1090 -0.1069 -0.1069 -0.1090

-0.1090 -0.1081 -0.1064 -0.1064 -0.1081

-0.1069 -0.1064 -0.1052 -0.1052 -0.1064

-0.1048 -0.1045 -0.1035 -0.1035 -0.1045

-0.1027 -0.1024 -0.1017 -0.1017 -0.1024

-0.1048 -0.1045 -0.1035 -0.1035 -0.1045

-0.1069 -0.1064 -0.1052 -0.1052 -0.1064

-0.1090 -0.1081 -0.1064 -0.1064 -0.1081

>>

事实上,那样的编写是肯定错的:下面足以证实:

>>

[U,V]=dftuv(8,5);

D=sqrt(U.^2+V.^2);

>> U

U =

0 1 2 -2 -1

0 1 2 -2 -1

0 1 2 -2 -1

0 1 2 -2 -1

0 1 2 -2 -1

0 1 2 -2 -1

0 1 2 -2 -1

0 1 2 -2 -1

>> V

V =

0 0 0 0 0

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

4 4 4 4 4

-3 -3 -3 -3 -3

-2 -2 -2 -2 -2

-1 -1 -1 -1 -1

>> D

D =

0 1.0000 2.0000 2.0000 1.0000

1.0000 1.4142 2.2361 2.2361 1.4142

2.0000 2.2361 2.8284 2.8284 2.2361

3.0000 3.1623 3.6056 3.6056 3.1623

4.0000 4.1231 4.4721 4.4721 4.1231

3.0000 3.1623 3.6056 3.6056 3.1623

2.0000 2.2361 2.8284 2.8284 2.2361

1.0000 1.4142 2.2361 2.2361 1.4142

>> H4=hpfilter(\'gaussian\',8,5,0.05*8);%高斯高通

>> h4

??? Undefined function or variable \'h4\'.

>> H4

H4 =

0 0.9561 1.0000 1.0000 0.9561

0.9561 0.9981 1.0000 1.0000 0.9981

1.0000 1.0000 1.0000 1.0000 1.0000

1.0000 1.0000 1.0000 1.0000 1.0000

1.0000 1.0000 1.0000 1.0000 1.0000

1.0000 1.0000 1.0000 1.0000 1.0000

1.0000 1.0000 1.0000 1.0000 1.0000

0.9561 0.9981 1.0000 1.0000 0.9981

>>