(Digital Image Processing MATLAB+Python) Chapter 6 Image Smoothing - Section 2: Frequency Domain Smoothing Filtering

Frequency domain smoothing filtering : It is a filtering method commonly used in digital image processing, and its main idea is to filter images in the frequency domain . The steps are as follows

  • Perform Fourier transform on the original image to obtain the frequency domain image
  • For filtering operations on frequency domain images , you can choose commonly used low-pass filters, such as Gaussian low-pass filters, mean filters, etc.
  • Inverse Fourier transform is performed on the filtered frequency domain image to obtain a smoothed image

insert image description here

The core key of low-pass filtering in the frequency domain is to design a suitable low-pass filter H ( u , v ) H(u,v)H(u,v)

G ( u , v ) = H ( u , v ) F ( u , v ) G(u,v)=H(u,v)F(u,v) G(u,v)=H(u,v)F(u,v)

Frequency domain smoothing filter has the following categories

  • ideal low pass filter
  • Buttersworth filter
  • Exponential low-pass filtering
  • trapezoidal low-pass filter

One: ideal low-pass filter

(1 Overview

Ideal low-pass filter : It is a filter that filters out high-frequency signals and retains low-frequency signals in the frequency domain. Its mathematical expression is

H ( u , v ) = { 1 D ( u , v ) ≤ D 0 0 D ( u , v ) > D 0 H(u, v)=\left\{\begin{array}{ll}1 & D(u, v) \leq D_{0} \\0 & D(u, v)>D_{0}\end{array}\right. H(u,v)={ 10D(u,v)D0D(u,v)>D0

Among them, H ( u , v H(u, vH(u,v ) means that the frequency domain filter is at( u , v ) (u,v)(u,v )处的值,D ( u , v ) = u 2 + v 2 D(u,v)=\sqrt{ u^{2}+v^{2} }D(u,v)=u2+v2 Represents the frequency domain midpoint ( u , v ) (u,v)(u,v ) the distance to the center point,D 0 D_{0}D0is the cutoff frequency, that is, signals below this frequency pass through, and signals above this frequency are filtered out

Specifically, the image can be converted into the frequency domain by performing Fourier transform on the image. Then, the ideal low-pass filter is multiplied by the frequency domain image to obtain the filtered frequency domain image. Finally, inverse Fourier transform is performed on the filtered frequency domain image to obtain the filtering result in the spatial domain

insert image description here

The ideal low-pass filter has the advantages of clear cutoff frequency and good filtering effect, but due to its sudden cutoff frequency, it will cause "ringing" phenomenon in the filtering result, and it is not suitable for practical applications, because the high frequency in the actual signal It is difficult to completely cut off the weight

  • "Ringing" phenomenon : H ( u , v ) H(u,v)H(u,v ) inD 9 D_{9}D9From 1 to 0, the corresponding impulse response h ( x , y ) h(x,y)h(x,y ) in the form of concentric rings in the airspace; the radius of the concentric rings is the same asD 0 D_{0}D0Inversely proportional; D 0 D_{0}D0The smaller the value, the larger the radius of the concentric rings and the more blurred

insert image description here

The following figure shows the "ringing" phenomenon

insert image description here

(2) Program

Realize the following effect: ideal low-pass filter with different cut-off frequencies

insert image description here


matlab implementation :

% 读入图像并显示
Image=imread('lena.bmp');
imshow(Image);

% 对图像进行傅里叶变换,并进行频谱搬移
FImage=fftshift(fft2(double(Image)));  

% 获取频域图像的大小
[N M]=size(FImage);

% 初始化一个全零的数组
g=zeros(N,M);

% 计算频谱的中心点坐标
r1=floor(M/2);  r2=floor(N/2);

% 在傅里叶频谱图上绘制4个圆形,并显示傅里叶频谱图像
figure;
imshow(log(abs(FImage)+1),[]),title('傅里叶频谱');
hold on
circle(r1,r2,5)
circle(r1,r2,11)
circle(r1,r2,45)
circle(r1,r2,68)

% 初始化截止频率数组
d0=[5 11 45 68];

% 循环对不同截止频率进行低通滤波并显示
for i=1:4
    for x=1:M
        for y=1:N
            % 计算当前像素点到频谱中心的距离
            d=sqrt((x-r1)^2+(y-r2)^2);
            % 根据距离和截止频率确定滤波器的值
            if d<=d0(i)
                h=1;
            else
                h=0;
            end
            % 用滤波器乘以频域图像
            g(y,x)=h*FImage(y,x);
        end
    end
    % 对滤波后的频域图像进行反傅里叶变换得到空域的图像,并显示
    g= real(ifft2(ifftshift(g)));
    figure,imshow(uint8(g)),title(['理想低通滤波D0=',num2str(d0(i))]);
end
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读入图像并显示
Image = cv2.imread('lena.bmp', cv2.IMREAD_GRAYSCALE)
plt.imshow(Image, cmap='gray')
plt.show()

# 对图像进行傅里叶变换,并进行频谱搬移
FImage = np.fft.fftshift(np.fft.fft2(Image.astype(float)))

# 获取频域图像的大小
N, M = FImage.shape

# 初始化一个全零的数组
g = np.zeros((N, M))

# 计算频谱的中心点坐标
r1 = int(M/2)
r2 = int(N/2)

# 在傅里叶频谱图上绘制4个圆形,并显示傅里叶频谱图像
plt.imshow(np.log(np.abs(FImage)+1), cmap='gray')
plt.title('傅里叶频谱')
plt.hold(True)
circle1 = plt.Circle((r1,r2), 5, color='r', fill=False)
circle2 = plt.Circle((r1,r2), 11, color='r', fill=False)
circle3 = plt.Circle((r1,r2), 45, color='r', fill=False)
circle4 = plt.Circle((r1,r2), 68, color='r', fill=False)
plt.gca().add_artist(circle1)
plt.gca().add_artist(circle2)
plt.gca().add_artist(circle3)
plt.gca().add_artist(circle4)
plt.show()

# 初始化截止频率数组
d0 = [5, 11, 45, 68]

# 循环对不同截止频率进行低通滤波并显示
for i in range(4):
    for x in range(M):
        for y in range(N):
            # 计算当前像素点到频谱中心的距离
            d = np.sqrt((x-r1)**2 + (y-r2)**2)
            # 根据距离和截止频率确定滤波器的值
            if d <= d0[i]:
                h = 1
            else:
                h = 0
            # 用滤波器乘以频域图像
            g[y,x] = h * FImage[y,x]
    # 对滤波后的频域图像进行反傅里叶变换得到空域的图像,并显示
    g = np.real(np.fft.ifft2(np.fft.ifftshift(g)))
    plt.imshow(g.astype(np.uint8), cmap='gray')
    plt.title('理想低通滤波D0=' + str(d0[i]))
    plt.show()

Two: Butterworth low-pass filtering

(1 Overview

Butterworth low-pass filtering : It is a digital filtering method used to remove high-frequency noise in images. It is a filter with optimized amplitude-frequency characteristics, which can retain more low-frequency information in the image. The filter is designed based on the Butterworth function . The Butterworth function is an ideal low-pass filter whose frequency response has a very steep cut-off characteristic, but there will be some defects in practical applications. Therefore, the Butterworth low-pass filter is an optimized filter based on the characteristics of the Butterworth function, which can eliminate its defects by adding some appropriate compensation

{ H ( u , v ) = 1 1 + [ D ( u , v ) / D 0 ] 2 n H ( u , v ) = 1 1 + ( 2 − 1 ) [ D ( u , v ) / D 0 ] 2 n \left\{\begin{array}{l}H(u, v)=\frac{1}{1+\left[D(u, v) / D_{0}\right]^{2 n}} \\H(u, v)=\frac{1}{1+(\sqrt{2}-1)\left[D(u, v) / D_{0}\right]^{2 n}}\end{array}\right. { H(u,v)=1+[D(u,v)/D0]2 n1H(u,v)=1+(2 1)[D(u,v)/D0]2 n1

D ( u , v ) = D 0 D(u,v)=D_{0}D(u,v)=D0time,H ( u , v ) H(u, v)H(u,v ) is reduced to 1 2 \frac{1}{2}of the maximum value21or 1 2 \frac{1}{\sqrt{ 2 }}2 1

Compared with other low-pass filters, Butterworth low-pass filters have the following characteristics :

  • Has a steeper cut-off characteristic for better removal of high-frequency noise
  • The performance of the filter can be flexibly controlled by adjusting the order and cutoff frequency
  • The implementation in the frequency domain is relatively simple, usually using a second-order filter cascade

insert image description here

As shown in the figure below, with the order nnAs n increases, the image ringing effect becomes more and more obvious

insert image description here

(2) Program

Achieve the following effects: Butterworth low-pass filters with different orders

insert image description here


matlab implementation :

Image=imread('lena.bmp'); % 读取lena.bmp图像
Image=imnoise(Image,'gaussian'); % 加入高斯噪声
imshow(Image); % 显示噪声图像
FImage=fftshift(fft2(double(Image)));  % 对加噪声图像进行傅里叶变换及频谱搬移
[N M]=size(FImage); % 获取傅里叶变换后频率矩阵的大小
g=zeros(N,M); % 用0矩阵初始化滤波后的频率矩阵
r1=floor(M/2);  r2=floor(N/2); % 计算中心点位置
figure;
imshow(log(abs(FImage)+1),[]),title('傅里叶频谱'); % 显示傅里叶频谱
hold on
circle(r1,r2,30) % 在频谱图上绘制圆形滤波器的半径
d0=30; % 设置圆形滤波器的半径
n=[1 2 3 4]; % 不同的Butterworth滤波器阶数
for i=1:4
    for x=1:M
        for y=1:N
            d=sqrt((x-r1)^2+(y-r2)^2); % 计算像素点到中心点的距离
            h=1/(1+(d/d0)^(2*n(i))); % 计算Butterworth滤波器的值
            g(y,x)=h*FImage(y,x); % 对傅里叶变换后的频率矩阵进行滤波
        end
    end
    g=ifftshift(g); % 频谱搬移
    g=real(ifft2(g)); % 逆傅里叶变换得到滤波后的图像
    figure,imshow(uint8(g)),title(['Butterworth低通滤波n=',num2str(n(i))]); % 显示滤波后的图像
end

Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像并加入高斯噪声
Image = cv2.imread('lena.bmp', 0)
Image = cv2.GaussianBlur(Image, (5, 5), 0)
Image = Image.astype(np.float32)
Image += np.random.normal(0, 20, Image.shape)
Image = np.uint8(np.clip(Image, 0, 255))

# 显示噪声图像
plt.imshow(Image, cmap='gray')
plt.title('加噪声的图像')
plt.show()

# 对加噪声图像进行傅里叶变换并进行频谱搬移
FImage = np.fft.fftshift(np.fft.fft2(Image))

# 获取频域图像的大小
N, M = FImage.shape

# 初始化一个全零的数组
g = np.zeros((N, M))

# 计算频谱的中心点坐标
r1 = M // 2
r2 = N // 2

# 在傅里叶频谱图上绘制一个圆形,并显示傅里叶频谱图像
plt.imshow(np.log(np.abs(FImage) + 1), cmap='gray')
plt.title('傅里叶频谱')
plt.plot(r1, r2, 'wo', markerfacecolor='none', markersize=60)
plt.show()

# 初始化截止频率数组和Butterworth滤波器阶数数组
d0 = 30
n = [1, 2, 3, 4]

# 循环对不同Butterworth滤波器阶数进行低通滤波并显示
for i in range(4):
    for x in range(M):
        for y in range(N):
            # 计算当前像素点到频谱中心的距离
            d = np.sqrt((x - r1) ** 2 + (y - r2) ** 2)
            # 根据距离和截止频率计算Butterworth滤波器的值
            h = 1 / (1 + (d / d0) ** (2 * n[i]))
            # 用滤波器乘以频域图像
            g[y, x] = h * FImage[y, x]
    # 对滤波后的频域图像进行反傅里叶变换得到空域的图像,并显示
    g = np.real(np.fft.ifft2(np.fft.ifftshift(g)))
    plt.imshow(np.uint8(g), cmap='gray')
    plt.title('Butterworth低通滤波 n=' + str(n[i]))
    plt.show()

Three: Exponential low-pass filtering

(1 Overview

Exponential low-pass filtering : It is a method of filtering images in the frequency domain, mainly used to remove high-frequency noise. The core idea of ​​this filter is to attenuate the value of the high-frequency component in the frequency domain, so as to reduce the high-frequency part of the image and retain the effect of the low-frequency part of the image.

H ( u , v ) = e − D 2 ( u , v ) 2 D 0 2 H(u, v)=e^{-\frac{D^{2}(u, v)}{2 D_{0}^{2}}} H(u,v)=e2 D02D2(u,v)

Among them, D ( u , v ) D(u,v)D(u,v ) is the pixel point( u , v ) (u,v) in(u,v ) the distance to the center point,D 0 D_0D0is the cutoff frequency of the filter, which is the frequency at which the filter starts to function. H ( u , v ) H(u,v)H(u,v ) is the value of the filter in the frequency domain, which is convolved with the Fourier transform of the original image in the frequency domain to obtain the filtered image. WhenD ( u , v ) = D 0 D(u,v)=D_{0}D(u,v)=D0time,H ( u , v ) H(u, v)H(u,v ) drops to 0.607 of the maximum value

insert image description here

As shown in the figure below, with the cut-off frequency D 0 D_{0}D0The increase of the image ringing effect becomes less and less obvious

insert image description here

(2) Program

Achieve the following effects: Exponential low-pass filters with different cut-off frequencies
insert image description here


matlab implementation :

Image=imread('lena.bmp');  %读取lena.bmp图像
Image=imnoise(Image,'gaussian');   %添加高斯噪声
imshow(Image);  %显示加噪声后的图像
FImage=fftshift(fft2(double(Image)));  %对图像进行傅里叶变换,并对频谱进行移位
[N M]=size(FImage);  %获取变换后频谱的大小
g=zeros(N,M);  %初始化滤波后的频谱
r1=floor(M/2);  r2=floor(N/2);  %获取频谱中心位置
figure;
imshow(log(abs(FImage)+1),[]),title('傅里叶频谱');  %显示傅里叶频谱
d0=[20 40];  %设置指数低通滤波器的参数
n=2;
for i=1:2  %遍历两个不同的截止频率
    for x=1:M
        for y=1:N
            d=sqrt((x-r1)^2+(y-r2)^2);  %计算距离
            h=exp(-0.5*(d/d0(i))^n);   %计算滤波器响应
            g(y,x)=h*FImage(y,x);  %对频谱进行滤波
        end
    end
    g=ifftshift(g);  %对滤波后的频谱进行移位
    g=real(ifft2(g));  %对滤波后的频谱进行逆傅里叶变换
figure,imshow(uint8(g)),title(['指数低通滤波D0=',num2str(d0(i))]);  %显示滤波后的图像
end

Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取lena.bmp图像
Image = cv2.imread('lena.bmp', cv2.IMREAD_GRAYSCALE)

# 添加高斯噪声
Image = cv2.GaussianBlur(Image, (3, 3), 0)
Image = Image + np.uint8(np.random.normal(0, 20, size=Image.shape))

# 显示加噪声后的图像
plt.imshow(Image, cmap='gray')
plt.show()

# 对图像进行傅里叶变换,并对频谱进行移位
FImage = np.fft.fftshift(np.fft.fft2(np.double(Image)))

# 获取变换后频谱的大小
N, M = FImage.shape

# 初始化滤波后的频谱
g = np.zeros((N, M))

# 获取频谱中心位置
r1, r2 = M // 2, N // 2

# 显示傅里叶频谱
plt.imshow(np.log(np.abs(FImage) + 1), cmap='gray')
plt.title('傅里叶频谱')
plt.show()

# 设置指数低通滤波器的参数
d0 = [20, 40]
n = 2

# 遍历两个不同的截止频率
for i in range(2):
    for x in range(M):
        for y in range(N):
            # 计算距离
            d = np.sqrt((x - r1)**2 + (y - r2)**2)
            # 计算滤波器响应
            h = np.exp(-0.5 * (d / d0[i])**n)
            # 对频谱进行滤波
            g[y, x] = h * FImage[y, x]

    # 对滤波后的频谱进行移位
    g = np.fft.ifftshift(g)
    # 对滤波后的频谱进行逆傅里叶变换
    g = np.real(np.fft.ifft2(g))

    # 显示滤波后的图像
    plt.imshow(np.uint8(g), cmap='gray')
    plt.title('指数低通滤波D0=' + str(d0[i]))
    plt.show()

Four: trapezoidal low-pass filter

(1 Overview

Trapezoidal low-pass filter : It is a low-pass filter that can simultaneously balance the ringing and bandwidth of the frequency response to a certain extent . Its frequency response resembles a trapezoid, hence the name. The trapezoidal low-pass filter can be used for frequency domain filtering in image processing. By controlling the slope and distance of the upper and lower boundaries of the trapezoid, its cut-off frequency and bandwidth can be adjusted. Compared with other low-pass filters, the trapezoidal low-pass filter can provide a smoother transition area and wider transition bandwidth, thereby reducing image distortion and information loss

H ( u , v ) = { 1 , D ( u , v ) ≤ D 0 D ( u , v ) − D 1 D 0 − D 1 , D 0 < D ( u , v ) ≤ D 1 0 D ( u , v ) > D 1 H(u, v)=\left\{\begin{array}{cc}1, & D(u, v) \leq D_{0} \\\frac{D(u, v)-D_{1}}{D_{0}-D_{1}}, & D_{0}<D(u, v) \leq D_{1} \\0 & D(u, v)>D_{1}\end{array}\right. H(u,v)= 1,D0D1D(u,v)D1,0D(u,v)D0D0<D(u,v)D1D(u,v)>D1

where D 0 D_{0}D0is the cutoff frequency, D 0 , D 1 D_{0}, D_{1}D0D1Need to satisfy: D 0 < D 1 D_{0}<D_{1}D0<D1

insert image description here

As shown below

insert image description here

(2) Program

Gradient low-pass filters with different cutoff frequencies to achieve the following effects

insert image description here


matlab implementation :

Image=imread('lena.bmp');
Image=imnoise(Image,'gaussian');   %加入噪声
imshow(Image);
FImage=fftshift(fft2(double(Image)));  %傅里叶变换及频谱搬移
[N M]=size(FImage);
g=zeros(N,M);
r1=floor(M/2);  r2=floor(N/2);
%figure;
%imshow(log(abs(FImage)+1),[]),title('傅里叶频谱');
d0=[5 30];
d1=[45 70];

for i=1:2
    for x=1:M
        for y=1:N
            d=sqrt((x-r1)^2+(y-r2)^2);
            
            if d>d1  
                h=0;
            else
                if d>d0
                    h=(d-d1)/(d0-d1);
                else
                    h=1;
                end
            end            
            g(y,x)=h*FImage(y,x);
        end
    end
    g=ifftshift(g);
    g=real(ifft2(g));
    figure,imshow(uint8(g)),title(['梯度低通滤波D0=',num2str(d0(i)),',D1=',num2str(d1(i))]);
end

Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像并添加高斯噪声
Image = cv2.imread('lena.bmp', cv2.IMREAD_GRAYSCALE)
Image = cv2.GaussianBlur(Image, (5, 5), 0)
Image = cv2.addWeighted(Image, 1.5, np.zeros(Image.shape, Image.dtype), 0, -0.5)

# 显示原始图像
plt.imshow(Image, cmap='gray')
plt.title('Original Image')
plt.show()

# 进行傅里叶变换及频谱搬移
FImage = np.fft.fftshift(np.fft.fft2(Image.astype(np.float64)))

# 获取图像尺寸
N, M = FImage.shape

# 初始化梯度低通滤波器参数
g = np.zeros((N, M), dtype=np.complex64)
r1 = int(M / 2)
r2 = int(N / 2)
d0 = [5, 30]
d1 = [45, 70]

# 循环处理两组参数
for i in range(2):
    # 计算滤波器系数
    for x in range(M):
        for y in range(N):
            d = np.sqrt((x - r1)**2 + (y - r2)**2)
            
            if d > d1[i]:
                h = 0
            else:
                if d > d0[i]:
                    h = (d - d1[i]) / (d0[i] - d1[i])
                else:
                    h = 1
                    
            g[y, x] = h * FImage[y, x]
    
    # 进行傅里叶逆变换并显示结果
    g = np.fft.ifftshift(g)
    g = np.real(np.fft.ifft2(g))
    plt.imshow(g, cmap='gray')
    plt.title(f'梯度低通滤波D0={
      
      d0[i]},D1={
      
      d1[i]}')
    plt.show()

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/130335640