Matlab对含噪声图像的滤波操作_两种噪声_三种滤波器_两种方法

注释很重要

Matlab对含噪声图像的滤波操作。

噪声:

  • 高斯噪声(正态分布)
  • 均匀噪声

用到的滤波器:

  • 高斯滤波器
  • 盒型滤波器
  • 中值滤波器

用到的两种方法:

  • 直接conv2
  • fft2
%%C1
figure;
tiledlayout(1,3);
img = imread("\LenaG.bmp");
fft = fft2(img);
nexttile;
imshow(img);
title("LenaG");
fft_shift = double(fftshift(fft));
fft_magnitude_shift = abs(fft_shift);
nexttile;
imshow(log(fft_magnitude_shift),[]); %加对数以便于显示图像,为了更好地显示细节,不进行log变换的话灰度的动态范围被压缩
title("FFT with shift");
fft_angle_shift = angle(fft_shift);
nexttile;
imshow(fft_angle_shift,[]);%根据 C 中像素值的范围缩放显示.使用 [min(C(:))max(C(:))] 作为显示范围。 imshow 将 C 中的最小值显示为黑色,将最大值显示为白色。使用imshow(A,[]),即可把图像矩阵A显示为正常的灰度图像。本来A是0-1,把double拉伸到[0 255]
title("Phase with shift");

%%C2
figure;
tiledlayout(1,2);
img = imread("\LenaG.bmp");
dct = dct2(img);
nexttile
imshow(img);
title('LenaG');
dct_abs = abs(dct);
nexttile;
imshow(log(dct_abs), []);
%colormap(gca, gray)
colorbar;
title('Cosine transform');

%%C3 
figure;
tiledlayout(1,2);
img = imread("\LenaG.bmp");
fft2_img = fft2(img);
hartley = real(fft2_img) - imag(fft2_img);
nexttile;
imshow(img);
title('LenaG')
hartley_abs = abs(hartley);
nexttile;
imshow(log(hartley_abs), []);
colormap(gca, gray);
colorbar;
title('Hatley');

%%C4 (a)
figure;
tiledlayout(1,2);
img = imread("\LenaG.bmp");
kernel_r = fspecial('gaussian', size(img), 10); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernel_r = rot90(kernel_r,2);
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
nexttile;
imshow(img);
title("LenaG");
conv_img = conv2(img,kernel_r);
nexttile;
imshow(conv_img);
title("conv2 10");

%%C4 (all)
img = imread("\LenaG.bmp");
standard_deviation = [10, 20, 30, 5, 3];
kernels = {};
for i = 1:length(standard_deviation)
    kernels{i} = fspecial('gaussian', size(img), standard_deviation(i)); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
    kernels{i} = rot90(kernels{i},2);
end
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(standard_deviation)
    figure;
    tiledlayout(1,2);
    nexttile;
    imshow(img);
    title("LenaG");
    conv_img = conv2(img,kernels{j});
    nexttile;
    imshow(conv_img);
    title("conv2 "+standard_deviation(j));
end

%模糊半径越大,图像就越模糊。从数值角度看,数值越平滑
%The larger the blur radius, the more blurred the image. From a numerical
%point of view, the smoother the value。
%平滑线性滤波器,它可以降低图像灰度的“尖锐”变化
%Gaussian filter is a smooth linear filter, which can reduce the "sharp" changes in the image gray level


%%C5
tiledlayout(1,3);
img = imread("\LenaG.bmp");
kernel = fspecial('gaussian', size(img), 10); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
kernel_r = rot90(kernel,2);
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
nexttile;
imshow(img);
title("LenaG");
conv_img = conv2(img,kernel_r);
nexttile;
imshow(conv_img);
title("conv2 10");
img_fft = fft2(img);
%fft_magnitude_shift = abs(fftshift(img_fft));
%nexttile;
%imshow(log(fft_magnitude_shift),[]);
%title("magnitude 10");
kernel_fft = fft2(kernel);
multiply = img_fft .* kernel_fft;
img_ifft = ifft2(multiply);
result = fftshift(img_ifft);
nexttile;
imshow(result);
title("FFT convolution 10");
assert(isreal(result), "imaginary parts are not zero");

%%C5 (all)
img = imread("\LenaG.bmp");
standard_deviation = [10, 20, 30, 5, 3];
kernels = {};
for i = 1:length(standard_deviation)
    kernels{i} = fspecial('gaussian', size(img), standard_deviation(i)); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
    kernels{i} = rot90(kernels{i},2);
end
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(standard_deviation)
    figure;
    tiledlayout(1,3);
    nexttile;
    imshow(img);
    title("LenaG Gaussian");
    conv_img = conv2(img,kernels{j});
    nexttile;
    imshow(conv_img);
    title("conv2 "+standard_deviation(j));
    img_fft = fft2(img);
    %fft_magnitude_shift = abs(fftshift(img_fft));
    %nexttile;
    %imshow(log(fft_magnitude_shift),[]);
    %title("magnitude "+standard_deviation(j));
    kernel_fft = fft2(kernels{j});
    multiply = img_fft .* kernel_fft;
    img_ifft = ifft2(multiply);
    result = fftshift(img_ifft);
    nexttile;
    imshow(result);
    title("FFT convolution "+standard_deviation(j));
    assert(isreal(result), "imaginary parts are not zero");
end

%卷积是一种运算操作,傅里叶变换是一种变换操作。卷积在图像处理的应用中一般是卷积滤波,
%即用一个卷积模板(卷积核/滤波器)去进行滤波,而傅里叶变换在信号处理中往往是变换时域和频域,在图像处理中便是空域和频域。
%Convolution is an arithmetic operation, and Fourier transform is a transformation operation. 
%Convolution in the application of image processing is generally convolution filtering, that is, a convolution template (convolution kernel/filter) 
%is used to filter, and the Fourier transform in signal processing often transforms the time domain and the frequency domain. In image processing, it is the spatial domain and the frequency domain.

%Con2: 一个卷积核去对图像进行卷积操作,算法复杂度低
%FFT: 傅里叶变换到时域->在时域进行操作->傅里叶反变换回空域。可能会引入高频分量干扰(Introduce high-frequency component interference)

%%C6
img = imread("\LenaG.bmp");
box = {[5, 5], [10, 10], [20, 20]};

kernels = {};
for i = 1:length(box)
    img_size = size(img);
    kernels{i} = zeros(img_size, 'double');
    x_number_range = fix(img_size(1)/2 - box{i}(1)/2) : fix(img_size(1)/2 + box{i}(1)/2);
    y_number_range = fix(img_size(2)/2 - box{i}(2)/2) : fix(img_size(2)/2 + box{i}(2)/2);
    kernels{i}(x_number_range, y_number_range) = 1/(numel(x_number_range) * numel(y_number_range));
    %box不需要反转
end
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(box)
    figure;
    tiledlayout(1,3);
    nexttile;
    imshow(img);
    title("LenaG Box");
    conv_img = conv2(img,kernels{j});
    nexttile;
    imshow(conv_img);
    title("conv2 "+box{j}(1)+" * "+box{j}(1));
    img_fft = fft2(img);
    %fft_magnitude_shift = abs(fftshift(img_fft));
    %nexttile;
    %imshow(log(fft_magnitude_shift),[]);
    %title("magnitude "+standard_deviation(j));
    kernel_fft = fft2(kernels{j});
    multiply = img_fft .* kernel_fft;
    img_ifft = ifft2(multiply);
    result = fftshift(img_ifft);
    nexttile;
    imshow(result);
    title("FFT convolution "+standard_deviation(j));
    assert(isreal(result), "imaginary parts are not zero");
end
%box滤波是一种averaging filter,是方块区域 N*M 内,中心点的像素为全部点像素值的平均值。不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节,从而使图像变得模糊
%Box filtering means that the pixel at the center point in the box area N*M is the average of all pixel values.
%Can not protect the image details well, it also destroys the details of the image while denoising the image, which makes the image blurry
%所以盒子越大,需要平均的范围越大,越模糊
%The larger the range of the mean,the larger range needs average, the more fuzzy


%%C7
img = imread("\LenaG.bmp");
gaussian_img = img + uint8(20*randn(size(img)));
img = im2double(gaussian_img);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
medfilt = {[3, 3],[5, 5],[7, 7]};
kernels = {};
for i = 1:length(medfilt)
    figure;
    tiledlayout(1, 2);
    nexttile;
    imshow(img);
    title("LenaG medfilt");
    img_medfilt = medfilt2(img, medfilt{i});
    nexttile;
    imshow(img_medfilt);
    title("medfilt "+medfilt{i}(1)+" * "+medfilt{i}(1));
end


%中值滤波是一种非线性数字滤波器,半径越大,细节越少,所以越大,细节越看不清楚
%Median filter is a non-linear digital filter, the less the details, so when the parameter is bigger, the less clear the details
%Set the gray value of each pixel to the median of the gray values of all pixels in a certain area window at that point.
%在均值滤波器中,由于噪声成分被放入平均计算中,所以输出受到了噪声的影响,但是在中值滤波器中,
%由于噪声成分很难选上,所以几乎不会影响到输出。因此同样用3x3区域进行处理,中值滤波消除的噪声能力更好
%In the average filter, because the noise component is put into the average calculation, 
%the output is affected by the noise, but in the median filter, because the noise component is difficult to select, 
%it hardly affects the output. Therefore, the 3x3 area is also used for processing, and the noise removal ability of the median filter is better.

%%C8.C4
% rand 生成均匀分布的伪随机数。分布在(0~1)之间
% randn 生成标准正态分布的伪随机数(均值为0,方差为1)
img = imread("\LenaG.bmp");
standard_deviation = [10, 20, 30, 5, 3];
kernels = {};
for i = 1:length(standard_deviation)
    kernels{i} = fspecial('gaussian', size(img), standard_deviation(i)); % Compose Gaussian Kernel S.D. = 10
%两个函数,翻转其中一个,再滑动求积分,叫卷积(convultion);不翻转就滑动求积分,叫做互相关(cross-correlation)。如果其中之一是偶函数,那么卷积和互相关效果相同。
%Two functions, flip one of them, and then slide for integration, called convultion; without flipping, slide for integration, called cross-correlation. If one of them is an even function, then convolution and cross-correlation have the same effect.
%卷积意味着把输入信号在时间轴上翻转,然后跟信号处理系统的描述方程(冲激响应)叠加积分.
%Convolution means flipping the input signal on the time axis, and then
%superimposing the integral with the description equation (impulse response) of the signal processing system.
    kernels{i} = rot90(kernels{i},2);
end
uniform = mean(img, 'all') * 0.2 * 2; % Uniform power is 0.2
uniform_image = img + uint8(uniform * (rand(size(img)) - 0.5));
img = im2double(uniform_image);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(standard_deviation)
    figure;
    tiledlayout(1,2);
    nexttile;
    imshow(img);
    title("LenaG");
    conv_img = conv2(img,kernels{j});
    nexttile;
    imshow(conv_img);
    title("conv2 "+standard_deviation(j));
end

%%C8.C6
img = imread("\LenaG.bmp");
box = {[5, 5], [10, 10], [20, 20]};

kernels = {};
for i = 1:length(box)
    img_size = size(img);
    kernels{i} = zeros(img_size, 'double');
    x_number_range = fix(img_size(1)/2 - box{i}(1)/2) : fix(img_size(1)/2 + box{i}(1)/2);
    y_number_range = fix(img_size(2)/2 - box{i}(2)/2) : fix(img_size(2)/2 + box{i}(2)/2);
    kernels{i}(x_number_range, y_number_range) = 1/(numel(x_number_range) * numel(y_number_range));
    %box不需要反转
end
uniform = mean(img, 'all') * 0.2 * 2; % Uniform power is 0.2
uniform_image = img + uint8(uniform * (rand(size(img)) - 0.5));
img = im2double(uniform_image);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
for j=1:length(box)
    figure;
    tiledlayout(1,3);
    nexttile;
    imshow(img);
    title("LenaG Box");
    conv_img = conv2(img,kernels{j});
    nexttile;
    imshow(conv_img);
    title("conv2 "+box{j}(1)+" * "+box{j}(1));
    img_fft = fft2(img);
    %fft_magnitude_shift = abs(fftshift(img_fft));
    %nexttile;
    %imshow(log(fft_magnitude_shift),[]);
    %title("magnitude "+standard_deviation(j));
    kernel_fft = fft2(kernels{j});
    multiply = img_fft .* kernel_fft;
    img_ifft = ifft2(multiply);
    result = fftshift(img_ifft);
    nexttile;
    imshow(result);
    title("FFT Convolution");
    assert(isreal(result), "imaginary parts are not zero");
end

%%C8.C7
img = imread("\LenaG.bmp");
uniform = mean(img, 'all') * 0.25 * 2; % Uniform power is 0.25
uniform_image = img + uint8(uniform * (rand(size(img)) - 0.5));
img = im2double(uniform_image);%图片都以uint8数据储存。当对图片进行数据运算时,数据大于256就出现溢出,结果失真。而双精度数运算时不会出现溢出
medfilt = {[3, 3],[5, 5],[7, 7]};
kernels = {};
for i = 1:length(medfilt)
    figure;
    tiledlayout(1, 2);
    nexttile;
    imshow(img);
    title("LenaG medfilt");
    img_medfilt = medfilt2(img, medfilt{i});
    nexttile;
    imshow(img_medfilt);
    title("medfilt "+medfilt{i}(1)+" * "+medfilt{i}(1));
end

%高斯滤波对于抑制服从正态分布的噪声效果非常好,因为高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。
%Gaussian filtering is very effective in suppressing noise that obeys the normal distribution
%Gaussian noise refers to a type of noise whose probability density function obeys Gaussian distribution (ie normal distribution)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值