[数字图像处理]The Simulation and Application of Frequency Domain Filters(自己实现常用的滤波器)

  1. Introduction:

In this lab, I review the principles and properties of three kinds of filters (Sobel filter, Gaussian low pass and high pass, Butterworth notch filters). In order to make a better understand of these three filters, I apply these methods into different figures and compare the results of different filters.

  1. Case1: Sobel filter
  1. Principle of Sobel filter:

Sobel operator is mainly used for edge detection. It is a discrete difference operator used to calculate the approximate value of image brightness function. The core of Sobel edge detection lies in the convolution of pixel matrix. Convolution is very important for digital image processing, and many image processing algorithms are realized by convolution. The essence of convolution operation is the process of weighted summation of pixel values in the designated image region. The calculation process is that each pixel value in the image region is multiplied by each element of the convolution template respectively, and the sum of the convolution results is the result of convolution operation.

The convolution formula of the matrix is as follows:

The convolution operation between window M of 3x3 and convolution template C is as follows:

Gx and Gy are convolution factors of sobel, and the two factors are convolved with the original image as follows:

The notation A represents the original image:

Get the horizontal and longitudinal gray value Gx and Gy of each point in the image. Finally, the following formula is used to calculate the size of gray scale change:

But usually, for efficiency, you don't use the approximation of the square root, although we lose precision:

  1. Pseudocode of algorithm:
  1. calculate the product of Gx, Gy and each row of the template.
  2. the convolution of two 3x3 matrices is the multiplication and addition of each row and each column.
  3. Gx and Gy after the calculation of 3*3 template.
  4. the Gx ^ 2 + Gy ^ 2 square root or directly to the Gx and Gy sum after taking absolute value.
  5. set a threshold value, and the pixel value after operation is greater than the threshold value, and the output is all 1, and less than the threshold value, and the output is all 0.
  1. Matlab codes:

clc;

clear all;

close all;

 

Image = imread('Q3_1.tif');%

Sobel2_11711118(Image);

function Sobel2_11711118(inputImage)

 [ROW,COL, DIM] = size(inputImage);

 Gray_data = inputImage;

size1=size(inputImage);

image=zeros(2+size1(1),2+size1(2));

for a=1:size1(1)

    for b=1:size1(2)

        image(a,b)=((-1)^(a+b))*inputImage(a,b);

    end

end

F=fft2(image);

figure,imshow(log(abs(F)),[ ]);

%% Median Filter

imgn=Gray_data;

Median_Img = Gray_data;

for r = 2:ROW-1

    for c = 2:COL-1

        median3x3 =[imgn(r-1,c-1)    imgn(r-1,c) imgn(r-1,c+1)

                    imgn(r,c-1)      imgn(r,c)      imgn(r,c+1)

                    imgn(r+1,c-1)      imgn(r+1,c) imgn(r+1,c+1)];

        sort1 = sort(median3x3, 2, 'descend');

        sort2 = sort([sort1(1), sort1(4), sort1(7)], 'descend');

        sort3 = sort([sort1(2), sort1(5), sort1(8)], 'descend');

        sort4 = sort([sort1(3), sort1(6), sort1(9)], 'descend');

        mid_num = sort([sort2(3), sort3(2), sort4(1)], 'descend');

        Median_Img(r,c) = mid_num(2);

    end

end

 

% figure;

% imshow(Median_Img);

 

%% Sobel_Edge_Detect

 

Median_Img = double(Median_Img);%Convert image data to double to avoid loss of precision

Sobel_Threshold = 150;

Sobel_Img = zeros(ROW,COL);

for r = 2:ROW-1

    for c = 2:COL-1

        Sobel_x = Median_Img(r-1,c+1) + 2*Median_Img(r,c+1) + Median_Img(r+1,c+1) - Median_Img(r-1,c-1) - 2*Median_Img(r,c-1) - Median_Img(r+1,c-1);

        Sobel_y = Median_Img(r-1,c-1) + 2*Median_Img(r-1,c) + Median_Img(r-1,c+1) - Median_Img(r+1,c-1) - 2*Median_Img(r+1,c) - Median_Img(r+1,c+1);

        Sobel_Num = abs(Sobel_x) + abs(Sobel_y);

        %Sobel_Num = sqrt(Sobel_x^2 + Sobel_y^2);

        if(Sobel_Num > Sobel_Threshold)

            Sobel_Img(r,c)=0;

        else

            Sobel_Img(r,c)=255;

        end

    end

end

size2=size(Sobel_Img);

image2=zeros(2+size2(1),2+size2(2));

for a=1:size1(1)

    for b=1:size1(2)

        image2(a,b)=((-1)^(a+b))*Sobel_Img(a,b);

    end

end

F2=fft2(image2);

figure

imshow(log(abs(F2)),[ ]);

title('output')

figure;

imshow(Sobel_Img);

end

 

 

  1. Result and analysis:

 

Because the origin result is terrible and not clearly, we add the step called Median Filter and we can find that the result become much better than before.

This figure is the frequency domain of input figure.

This figure shows that the frequency domain of output figure. We can find that the noise is much less than before.

this figure is we finally get. We can see this figure is much better than the result we only do Sobel filter.

In another way, we can also do the Sobel filter:

The result and codes are in the following:

clc;

clear all;

close all;

imputImg = imread('Q3_1.tif');

Sobel1_11711118(imputImg);

function Sobel1_11711118(img);

size1=size(img);

image=zeros(2+size1(1),2+size1(2));

for a=1:size1(1)

    for b=1:size1(2)

        image(a,b)=((-1)^(a+b))*img(a,b);

    end

end

F=fft2(image);

figure,imshow(log(abs(F)),[ ]);

title('input')

%blur_img = imgaussfilt(img);

blur_img=img;

% masks

gx = [-1/2, 0, 1/2; -1, 0, 1; -1/2, 0, 1/2];

gy = gx';

% convolute

% same means the size of the new image is the same; By adding zero paddings

% before calculating and deleting them afterwards

conv_img_x = conv2(blur_img, gx, 'same');

conv_img_y = conv2(blur_img, gy, 'same');

conv_img = abs(conv_img_x) + abs(conv_img_y);

% normalize

norm_img = normalize(conv_img);

figure

imshow(norm_img )

title('output')

end

  1. Case2: Gaussian low pass and high pass
  1. Principle of Sobel filter:

Gaussian filtering is the process of weighted average of the whole image. The value of each pixel is obtained by weighted average of itself and other pixel values in the neighborhood. The specific operation of gaussian filtering is to scan every pixel in the image with a template (or convolution, mask) and replace the value of the center pixel of the template with the weighted average gray value of the pixel in the neighborhood determined by the template.

The expression of Gaussian function is:

For a 3*3 template:

(x, y) is the template coordinate, then w1=h (-1,1), w2=h (-1,0) ...Generate the corresponding 3*3 filter template; Sigma is the standard deviation.

  1. Pseudocode of algorithm:
  1. Read the image
  2. Generate the kernel of Gaussian
  3. Convolution
  4. Get the new picture
  1. Matlab codes:

clc;close all;

 

GaussianLow_11711118(30);

GaussianLow_11711118(60);

GaussianLow_11711118(160);

 

GaussianHigh_11711118(30);

GaussianHigh_11711118(60);

GaussianHigh_11711118(160);

function GaussianLow_11711118(setD0)

a=setD0;

I=imread('Q3_2.tif');

I=im2double(I); 

M=2*size(I,1);

N=2*size(I,2);                        %the size of filter

u=-M/2:(M/2-1);

v=-N/2:(N/2-1);

[U,V]=meshgrid(u,v);

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

D0=setD0;

H=exp(-(D.^2)./(2*(D0^2)));          %generate the Guassian filter

J=fftshift(fft2(I,size(H,1),size(H,2)));

G=J.*H;

L=ifft2(fftshift(G));

L=L(1:size(I,1),1:size(I,2));

figure;

imshow(L);

title(['Result of GLPF with D0 = ',num2str(a)]);

end

function GaussianHigh_11711118(setD0)

a=setD0;

img=imread('Q3_2.tif');

% img=rgb2gray(img);

% figure,imshow(img);

g=fft2(double(img));

%%fourier transform

g=fftshift(g);

%centralization

%imshow(abs(g),[])

[N1,N2]=size(g);

n=2;

d0=setD0;

n1=fix(N1/2);

n2=fix(N2/2);

 

for i=1:N1

  for j=1:N2

      d=sqrt((i-n1)^2+(j-n2)^2);

      h=1-exp(-d*d/(2*d0*d0));

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

  end

end

result=ifftshift(result);

X2=ifft2(result);

%Fourier decentralization and inverse

final=uint8(real(X2));

%Normalize the results

figure

imshow(final);

title(['Result of GHPF with D0 = ',num2str(a)]);

end

 

 

  1. Result and analysis:

 

We can find in the output figure of GLPF, the clarity increases as the value of D0 increases. Because the D0 represent the cutoff frequency. Even the noise part of the figure become much more clearly.

Then we take a closer look at the result picture and the input picture:

The left one is input figure and the right one is output figure with D0=160. By observing and comparing the details of two figures. We can find that the figure after the Gaussian filter becomes smoother. But at the edge of the figure is becoming fuzzy.

 

We can only know the figure become more and more darker with the value of D0 increasing. But if we have a closer look of these pictures. We will find some differences that we haven’t found.

Figure 1                                 Figure 2

Figure1 is the detail of output figure with D0=30

Figure2 is the detail of output figure with D0=60

Figure3 is the detail of output figure with D0=160

Figure 3

We can find that the edge of the letter ‘a’ become more and more accurate and the white band become more and more narrow.

  1. Case3: Butterworth notch filters
  1. Principle of Sobel filter:

Notch filters are also used to remove periodic noise. Although band stop filters can remove periodic noise, band stop filters also attenuate components other than noise. A notch filter attenuates a point, but does not lose the rest of the components.

In the spatial domain, the image has periodic noise. In the Fourier spectrum, you can see where the noise is (I've circled them in red here, they're not part of the data). If we use a band-stop filter, it is very troublesome and it will cause image loss. Notch filter can attenuate the noise directly and has good effect of denoising.

The expression is shown below. The notch filter can be obtained by shifting the center of the high-pass filter.

Here, because of the periodicity of the Fourier transform, there is no way in the Fourier spectrum that there can be a noise at a single point, there must be a noise pair symmetric about the far point. Here  and The method to get the noise point is as follows:

 

 

  1. Matlab codes:

close all;

clear all;

clc;

img = imread('Q3_3.tif');

Butterworth_11711118(img);

function Butterworth_11711118(imputImage);

f=imputImage;

f = mat2gray(f,[0 255]);

 

[M,N] = size(f);

P = 2*M;

Q = 2*N;

fc = zeros(M,N);

 

for x = 1:1:M

    for y = 1:1:N

        fc(x,y) = f(x,y) * (-1)^(x+y);

    end

end

 

F = fft2(fc,P,Q);

 

H_NF = ones(P,Q);

 

for x = (-P/2):1:(P/2)-1

     for y = (-Q/2):1:(Q/2)-1

        D = 30;

       

        v_k = 59; u_k = 77;

        D_k = ((x+u_k)^2 + (y+v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

        D_k = ((x-u_k)^2 + (y-v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

       

        v_k = 59; u_k = 159;

        D_k = ((x+u_k)^2 + (y+v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

        D_k = ((x-u_k)^2 + (y-v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

       

        v_k = -54; u_k = 84;

        D_k = ((x+u_k)^2 + (y+v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

        D_k = ((x-u_k)^2 + (y-v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

       

        v_k = -54; u_k = 167;

        D_k = ((x+u_k)^2 + (y+v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

        D_k = ((x-u_k)^2 + (y-v_k)^2)^(0.5);

        H_NF(x+(P/2)+1,y+(Q/2)+1) = H_NF(x+(P/2)+1,y+(Q/2)+1) * 1/(1+(D/D_k)^4);

     end

end

 

G_1 = H_NF .* F;

 

g_1 = real(ifft2(G_1));

g_1 = g_1(1:1:M,1:1:N);    

 

for x = 1:1:M

    for y = 1:1:N

        g_1(x,y) = g_1(x,y) * (-1)^(x+y);

    end

end

 

%% -----show-------

close all;

 

figure();

subplot(1,2,1);

imshow(f,[0 1]);

xlabel('a).Original Image');

 

subplot(1,2,2);

imshow(log(1 + abs(F)),[ ]);

xlabel('b).Fourier spectrum of a');

 

figure();

subplot(1,2,1);

imshow(H_NF,[0 1]);

xlabel('c).Butterworth Notch filter(D=30 n=2)');

 

subplot(1,2,2);

h = mesh(1:10:Q,1:10:P,H_NF(1:10:P,1:10:Q));

set(h,'EdgeColor','k');

axis([0 Q 0 P 0 1]);

xlabel('u');ylabel('v');

zlabel('|H(u,v)|');

 

figure();

subplot(1,2,2);

imshow(log(1 + abs(G_1)),[ ]);

xlabel('e).Fourier spectrum of d');

 

subplot(1,2,1);

imshow(g_1,[0 1]);

xlabel('d).Result image');

end

 

 

  1. Result and analysis:

 

 

 

 

This is H we choose with D=30 and the order is 2.

We can see that the noise has been removed and the picture become much more clearly.

  1. Answer and analysis of questions:

Question1:

This step is equal to we move the corner to center in spatial domain. But in frequency domain, we need to multiply the (-1) ^(u+v). we can use the date of the origin picture better. The quality of the output figure will be much better.

Question2:

Gaussian blur is the convolution of image I with a gaussian kernel:

G sigma is a two-dimensional gaussian kernel with standard deviation (sigma). It is defined as:

The larger the blur radius, the blurrier the image. From a numerical point of view, the smoother the value.

The width of the gaussian filter (which determines the smoothness) is represented by the parameter sigma, and the relationship between sigma and the smoothness is very simple. The bigger the sigma, the wider the band of the gaussian filter and the better the smoothness. By regulating the smoothness parameter (sigma), a compromise can be reached between the over blur (over smooth) of the image features and the over unexpected mutation (under smooth) of the image due to noise and fine texture.

But when the D is too much, the low frequency components are severely attenuated, causing the image to lose layers.

Question3:

In some range, the quality of output figure become better as the value of D is larger. But if the value of D exceeds the range the figure will be different to be recognized. And the order of the filter is higher, the quality of the figure is better.

Question4:

Because the DFT of a real image is a conjugate symmetric function centered at the origin. The input image * (1) ^ (x + y). When you're done multiplying, you're essentially moving to the center in the frequency domain, and the natural multiplication filter has to be symmetric, so that it's correct to do the reduction.

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值