MATLAB 中有哪些不同类型的去噪滤波器?
数字图像容易出现各种类型的噪声,从而使图像质量最差。图像噪声是捕获图像中亮度或颜色信息的随机变化。噪声基本上是由相机等外部来源引起的图像信号劣化。含有乘性噪声的图像具有越亮区域噪声越大的特性。我们将讨论各种去噪滤波器,以从数字图像中去除这些噪声。
本文中讨论的过滤器类型列出为:
- 均值滤波器
- 中值滤波器
- 高斯滤波器
- 维纳滤波器
均值滤波器
- 它也称为框平均滤波技术。
- 它使用内核并基于卷积。
- 它计算所有像素及其周围像素的平均值,并将结果分配给中心像素。
- 这是去除泊松噪声的一种非常有效的技术。
Syntax:
k=rgb2gray(k); // To convert into grayscale:
gaussian_noise = imnoise(k, ‘gaussian’, 0, 0.01);
// To create the image corrupted with gaussian noise:
denoised=conv2(noisy_image, mean_filter, ‘same’);
// To perform the convolution between gaussian_noisy image and mean filter:
To display the image: imtool( image_variable, [ ]);
To create the image corrupted with poisson noise: poisson_noise = imnoise(k, ‘poisson’);
salt_noise = imnoise(k, ‘salt & pepper’, 0.05);
// To create the image corrupted with salt and pepper noise:
speckle_noise = imnoise(k, ‘speckle’, 0.05);
// To corrupt the image with speckle noise:
例子:
Matlab
% MATLAB code for Mean filter
% read the image.
k=imread("einstein_colored.png");
% convert into grayscale.
k=rgb2gray(k);
% create image corrupted with gaussian noise.
gaussian_noise=imnoise(k,'gaussian',0,0.01);
% denoise the image with gaussian noise
gaussian_denoised=conv2(gaussian_noise,h,'same');
% display the org image.
imtool(k,[]);
% display the noised and denoised image.
imtool(gaussian_noise,[]);
imtool(gaussian_denoised,[]);
% create image corrupted with poisson noise.
poisson_noise=imnoise(k,'poisson');
% denoise the image with poisson noise
poisson_denoised=conv2(poisson_noise,h,'same');
% display the org image.
imtool(k,[]);
% display the noised and denoised image.
imtool(poisson_noise,[]);
imtool(poisson_denoised,[]);
% create image corrupted with salt & pepper noise.
salt_noise=imnoise(k,'salt & pepper', 0.05);
% denoise the image with salt & pepper noise
salt_denoised=conv2(salt_noise,h,'same');
% display the org image.
imtool(k,[]);
% display the noised and denoised image.
imtool(salt_noise,[]);
imtool(salt_denoised,[]);
% create image corrupted with speckle noise.
speckle_noise=imnoise(k,'speckle', 0.05);
% denoise the image with speckle noise
speckle_denoised=conv2(speckle_noise,h,'same');
%display the org image.
imtool(k,[]);
% display the noised and denoised image.
imtool(speckle_noise,[]);
imtool(speckle_denoised,[]);
Matlab
% MATLaB code for Median filter
% read the image.
k=imread("einstein_colored.png");
% Convert into grayscale image.
k=rgb2gray(k);
% Create the image corrupted with gaussian noise.
gaussian_noise=imnoise(k,'gaussian',0,0.01);
% Create the image corrupted with poisson noise.
poisson_noise=imnoise(k,'poisson');
% Create the image corrupted with salt
% & pepper noise.
salt_noise=imnoise(k,'salt & pepper', 0.05);
% Create the image corrupted
% with speckle noise.
speckle_noise=imnoise(k,'speckle', 0.05);
% Get the denoised image from noised
% image of: gaussian_noise
gaussian_denoised=medfilt2(gaussian_noise, [5 5]);
% Get the denoised image from
% noised image of:poisson_noise
poisson_denoised=medfilt2(poisson_noise, [5 5]);
% Get the denoised image from noised
% image of: salt_noise
salt_denoised=medfilt2(salt_noise, [5 5]);
% Get the denoised image from noised
% image of: speckle_noise
speckle_denoised=medfilt2(speckle_noise, [5 5]);
% Display the noised and denoised image
% side by side for Gaussian noise.
imshowpair(gaussian_noise, gaussian_denoised, 'montage');
% Display the noised and denoised image
%side by side for poisson_noise.
imshowpair(poisson_noise, poisson_denoised, 'montage');
% Display the noised and denoised
% image side by side for salt_noise.
imshowpair(salt_noise, salt_denoised, 'montage');
% Display the noised and denoised image
% side by side for speckle_noise.
imshowpair(speckle_noise, speckle_denoised, 'montage');
Matlab
% MATLAB code using Gaussian Filter
% read the image.
k=imread("einstein_colored.jpg");
% convert to grayscale.
k=rgb2gray(k);
% create the image corrupted with gaussian noise
gaussian_noise=imnoise(k,'gaussian',0,0.01);
% create the image corrupted with poisson noise
poisson_noise=imnoise(k,'poisson');
% create the image corrupted with salt & pepper noise
salt_noise=imnoise(k,'salt & pepper', 0.05);
% create the image corrupted with speckle noise
speckle_noise=imnoise(k,'speckle', 0.05);
% get the denoised image from gaussian_noise image.
gaussian_denoised=imgaussfilt(gaussian_noise,1);
% get the denoised image from poisson_noise image.
poisson_denoised=imgaussfilt(poisson_noise, 1);
% get the denoised image from salt_noise image.
salt_denoised=imgaussfilt(salt_noise, 1);
% get the denoised image from speckle_noise image.
speckle_denoised=imgaussfilt(speckle_noise, 1);
% display noised and denoised images side by side.
montage({gaussian_noise,gaussian_denoised });
title('Gaussian noise and denoised image using gaussian filter');
% display noised and denoised images side by side.
montage({poisson_noise,poisson_denoised});
title('poisson noise img vs poisson denoised img');
% display noised and denoised images side by side.
montage({salt_noise,salt_denoised});
title('salt&pepper noise img vs denoised img');
% display noised and denoised images side by side.
montage({speckle_noise,speckle_denoised});
title('speckle noise img vs speckle denoised img');
Matlab
% MATLAB code for Wiener2 filter
k=imread("einstein_colored.png");
k=rgb2gray(k);
% different noise with their parameters
gaussian_noise=imnoise(k,'gaussian',0,0.025);
poisson_noise=imnoise(k,'poisson');
salt_noise=imnoise(k,'salt & pepper', 0.05);
speckle_noise=imnoise(k,'speckle', 0.05);
% wiener filter with different noise
gaussian_denoised=wiener2(gaussian_noise, [5 5]);
poisson_denoised=wiener2(gaussian_noise, [5 5]);
salt_denoised=wiener2(salt_noise, [5 5]);
speckle_denoised=wiener2(speckle_noise, [5 5]);
imshowpair(gaussian_noise, gaussian_denoised, 'montage');
imshowpair(poisson_noise, poisson_denoised, 'montage');
imshowpair(salt_noise, salt_denoised, 'montage');
imshowpair(speckle_noise, speckle_denoised, 'montage');
输出:
均值滤波器不能有效去除任何特定噪声。
代码说明:
- k=imread(“einstein_colored);该行读取图像。
- k=rgb2gray(k);此行转换为灰度。
- gaussian_noise = imnoise(k, 'gaussian', 0, 0.01);这条线创建了被高斯噪声破坏的图像。
- gaussian_denoised=conv2(gaussian_noise, h, 'same');此行执行 gaussian_noisy 图像和均值滤波器之间的卷积。
- poisson_noise = imnoise(k, 'poisson');这条线创建了被泊松噪声破坏的图像。
- poisson_denoised=conv2(poisson_noise, h, 'same');此行执行 poisson_noise 图像和均值滤波器之间的卷积。
- salt_noise = imnoise(k, '盐和胡椒', 0.05);这条线创建了被盐和胡椒噪声破坏的图像,占总像素的 5%。
- salt_denoised=conv2(salt_noise, h, 'same');此行执行 salt_noise 图像和均值滤波器之间的卷积。
- 斑点噪声 = imnoise(k, '斑点', 0.05);这条线用 0.05 方差的散斑噪声破坏了图像。
- speckle_denoised=conv2(speckle_noise, h, 'same');此行执行斑点噪声图像和均值滤波器之间的卷积。
- imtool(k, []);此行显示原始图像。
- imtool(noised_image, []);这一行显示了noise_image。
- imtool(去噪图像,[]);此行显示降噪后的图像。
中值滤波器
中值滤波器是非线性滤波器。它对窗口覆盖的像素进行排序,并按升序对它们进行排序,然后返回它们的中值。
对于 2D 图像中的中值滤波器。我们使用 medfit()。它需要2个参数。首先是噪声图像,其次是使用的窗口大小。默认窗口大小为 [3 3]。
Syntax:
medfilt2( noisy_image, window_size);
为了并排显示两个图像,我们使用 imshowpair().imshowpair() 是一个重载函数,它在 Matlab 中有许多签名。 montage 关键字表示必须并排显示 2 个图像。
Syntax:
imshowpair(img1, img2, ‘montage’);
例子:
MATLAB
% MATLaB code for Median filter
% read the image.
k=imread("einstein_colored.png");
% Convert into grayscale image.
k=rgb2gray(k);
% Create the image corrupted with gaussian noise.
gaussian_noise=imnoise(k,'gaussian',0,0.01);
% Create the image corrupted with poisson noise.
poisson_noise=imnoise(k,'poisson');
% Create the image corrupted with salt
% & pepper noise.
salt_noise=imnoise(k,'salt & pepper', 0.05);
% Create the image corrupted
% with speckle noise.
speckle_noise=imnoise(k,'speckle', 0.05);
% Get the denoised image from noised
% image of: gaussian_noise
gaussian_denoised=medfilt2(gaussian_noise, [5 5]);
% Get the denoised image from
% noised image of:poisson_noise
poisson_denoised=medfilt2(poisson_noise, [5 5]);
% Get the denoised image from noised
% image of: salt_noise
salt_denoised=medfilt2(salt_noise, [5 5]);
% Get the denoised image from noised
% image of: speckle_noise
speckle_denoised=medfilt2(speckle_noise, [5 5]);
% Display the noised and denoised image
% side by side for Gaussian noise.
imshowpair(gaussian_noise, gaussian_denoised, 'montage');
% Display the noised and denoised image
%side by side for poisson_noise.
imshowpair(poisson_noise, poisson_denoised, 'montage');
% Display the noised and denoised
% image side by side for salt_noise.
imshowpair(salt_noise, salt_denoised, 'montage');
% Display the noised and denoised image
% side by side for speckle_noise.
imshowpair(speckle_noise, speckle_denoised, 'montage');
输出:
中值滤波器完全去除了椒盐噪声,但会给图像带来模糊。它在处理其他噪音时表现不佳。
高斯滤波器
Syntax:
B = imgaussfilt(A, sigma); // To obtain the filtered image using gaussian fitler:
// imgaussfilt() is the built-in function in Matlab, which takes 2 parameters.
To display the noisy and denoised image side by side in single frame: imshowpair(P{noisy, denoised}); title(noisy vs denoised’);
例子:
MATLAB
% MATLAB code using Gaussian Filter
% read the image.
k=imread("einstein_colored.jpg");
% convert to grayscale.
k=rgb2gray(k);
% create the image corrupted with gaussian noise
gaussian_noise=imnoise(k,'gaussian',0,0.01);
% create the image corrupted with poisson noise
poisson_noise=imnoise(k,'poisson');
% create the image corrupted with salt & pepper noise
salt_noise=imnoise(k,'salt & pepper', 0.05);
% create the image corrupted with speckle noise
speckle_noise=imnoise(k,'speckle', 0.05);
% get the denoised image from gaussian_noise image.
gaussian_denoised=imgaussfilt(gaussian_noise,1);
% get the denoised image from poisson_noise image.
poisson_denoised=imgaussfilt(poisson_noise, 1);
% get the denoised image from salt_noise image.
salt_denoised=imgaussfilt(salt_noise, 1);
% get the denoised image from speckle_noise image.
speckle_denoised=imgaussfilt(speckle_noise, 1);
% display noised and denoised images side by side.
montage({gaussian_noise,gaussian_denoised });
title('Gaussian noise and denoised image using gaussian filter');
% display noised and denoised images side by side.
montage({poisson_noise,poisson_denoised});
title('poisson noise img vs poisson denoised img');
% display noised and denoised images side by side.
montage({salt_noise,salt_denoised});
title('salt&pepper noise img vs denoised img');
% display noised and denoised images side by side.
montage({speckle_noise,speckle_denoised});
title('speckle noise img vs speckle denoised img');
输出:
高斯滤波器在处理高斯噪声和毒噪声时效果更好。
维纳滤波器
- 它是一种自适应低通滤波技术。
- 它按像素过滤图像。
- 它是一种线性滤波器。
Syntax:
J = wiener2(I,[m n],noise)
I = grayscale input image
[m n] = neighbouring window size
wiener2函数自适应地将维纳滤波器应用于图像。维纳过滤器将自己贴在局部图像的方差上。 Wiener2 几乎不执行平滑,只要方差很大。 Wiener2 执行更多的平滑,只要方差很小。
Syntax:
k=imread(“einstein_colored); // To read the image:
denoised=wiener2(noisy_image, [5 5]);
//To get the filtered image using Wiener filter
imshowpair(P{noisy, denoised}); title(noisy vs denoised’);
//To display the noisy and denoised image side by side in single frame
在这里,weiner2() 是一个内置函数,这里需要 2 个参数。 1.要过滤的噪声图像2。相邻窗口的大小。
例子:
MATLAB
% MATLAB code for Wiener2 filter
k=imread("einstein_colored.png");
k=rgb2gray(k);
% different noise with their parameters
gaussian_noise=imnoise(k,'gaussian',0,0.025);
poisson_noise=imnoise(k,'poisson');
salt_noise=imnoise(k,'salt & pepper', 0.05);
speckle_noise=imnoise(k,'speckle', 0.05);
% wiener filter with different noise
gaussian_denoised=wiener2(gaussian_noise, [5 5]);
poisson_denoised=wiener2(gaussian_noise, [5 5]);
salt_denoised=wiener2(salt_noise, [5 5]);
speckle_denoised=wiener2(speckle_noise, [5 5]);
imshowpair(gaussian_noise, gaussian_denoised, 'montage');
imshowpair(poisson_noise, poisson_denoised, 'montage');
imshowpair(salt_noise, salt_denoised, 'montage');
imshowpair(speckle_noise, speckle_denoised, 'montage');
输出:
代码说明:
- k=imread(“einstein_colored);该行读取图像。
- k=rgb2gray(k);此行转换为灰度。
- gaussian_noise = imnoise(k, 'gaussian', 0, 0.01);这条线创建了被高斯噪声破坏的图像。
- poisson_noise = imnoise(k, 'poisson');这条线创建了被泊松噪声破坏的图像。
- salt_noise = imnoise(k, '盐和胡椒', 0.05);这条线创建了被盐和胡椒噪声破坏的图像,占总像素的 5%。
- 斑点噪声 = imnoise(k, '斑点', 0.05);这条线用 0.05 方差的散斑噪声破坏了图像。
- gaussian_denoised=wiener2(gaussian_noise, [5 5]);此行使用窗口大小对高斯噪声图像应用滤波器 [5 5]
- poisson_denoised=wiener2(gaussian_noise, [5 5]);此行使用窗口大小对泊松噪声图像应用滤波器 [5 5]
- salt_denoised=wiener2(salt_noise, [5 5]);此行使用窗口大小对高斯椒盐图像应用过滤器 [5 5]
- speckle_denoised=wiener2(speckle_noise, [5 5]);此行使用窗口大小在散斑噪声图像上应用过滤器 [5 5]
- imshowpair(gaussian_noise, gaussian_denoised, '蒙太奇');此行在同一帧中并排显示高斯噪声图像和去噪图像。
- imshowpair(poisson_noise,poisson_denoised,'蒙太奇');这条线在同一帧中并排显示泊松噪声图像和去噪图像。
- imshowpair(salt_noise, salt_denoised, '蒙太奇');该行在同一帧中并排显示盐和胡椒噪声图像和去噪图像。
- imshowpair(斑点噪声,斑点降噪,“蒙太奇”);该行在同一帧中并排显示散斑噪声图像和去噪图像。
自适应方法通常比线性过滤产生令人满意的结果。自适应滤波器的特性是它比类似的线性滤波器更具选择性,因为它保留了图像的边缘和其他高频部分。这样做有一个缺点,Wiener2 确实比另一种线性过滤需要更多的计算时间。
当噪声为恒功率加性噪声时,如高斯白噪声; wiener2 给出了最好的结果。