注释很重要

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)

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

  1. matlab中用imnoise向图像中加入特定高斯半径的噪声

    最近需要做图像仿真,感觉用C++编写速度有点慢,也是为了省事,就用了matlab做前期的处理,后期到发布版本时,再用C++. 由于是初接触到图像仿真,有些问题没有很好的深入.这里就针对对某个图像中,加 ...

  2. 无线通信与编码实验_MATLAB利用限幅和滤波操作实现OFDM系统降峰均比(PAPR)_含实现代码

    MATLAB实现OFDM系统降峰均比 在OFDM系统中,经过IFFT运算之后所有的子载波相加,时域的发射信号会有很高的峰值.与单载波系统相比,OFDM系统具有很高的PAPR. (峰值-平均功率比 Pe ...

  3. python图像锐化滤波_OpenCV-Python学习(九):图像滤波

    目录: 1.滤波的相关概念 2.卷积操作 3.平滑操作(低通滤波) 均值滤波 中值滤波 高斯滤波 双边滤波 4.锐化操作(高通滤波) 自定义锐化核 USM锐化(UnsharpMask) 5.梯度操作( ...

  4. 05-图像的平滑处理(不同的滤波操作)

    对图像进行平滑处理实则就是对图像进行滤波操作罢了 每张图片都有若干个像素点所构成,滤波操作上就是将照片上的某些部分像素点进行修改从而达到平滑的效果 先展示一下原图 import cv2 img = c ...

  5. 如何用matlab绘制双调谐滤波器的阻抗频率特性曲线,一种双调谐无源滤波器的参数设计方法与流程...

    本发明涉及电力滤波技术领域,具体涉及一种双调谐无源滤波器的参数设计方法. 背景技术: 理想的公用电网所提供的电压应该是单一固定的频率以及规定的电压幅值.高次谐波电流和谐波电压的出现,对公用电网是一种污 ...

  6. Matlab 频域滤波处理周期噪声图像(带阻滤波器滤波)

    带阻滤波器的传递函数为: D0为截止半径(或是截止宽度) D1.D2由下式确定,表示与(u0,v0)和(-u0,v0)点的距离,此时(u0,v0)是在频域中心为坐标原点表示的,所以是对称关系. 使用二 ...

  7. 【图像去噪】图像邻域滤波【含GUI Matlab源码 2547期】

    ⛄一.图像去噪及滤波简介 1 图像去噪 1.1 图像噪声定义 噪声是干扰图像视觉效果的重要因素,图像去噪是指减少图像中噪声的过程.噪声分类有三种:加性噪声,乘性噪声和量化噪声.我们用f(x,y)表示图 ...

  8. matlab对图像频谱图分析,应用Matlab对图像信号进行频谱分析及滤波

    应用Matlab对图像信号进行频谱分析及滤波 选取一张彩色图片,建议把像素设置成200*200,提取图像的灰度值,并显示出灰度图像:在图像中增加正弦噪声信号(自己设置几个频率的正弦信号),画出加入噪声 ...

  9. 在matlab环境中实现图像的傅里叶变换,matlab用傅里叶变换实现图像的低通滤波

    低通滤波器指去除图像中的高频成分,而低通滤波器指去除图像中的高频成分. 考虑的有三种低通滤波器:理想滤波器.布特沃斯滤波器和高斯滤波器. 理想低通滤波器:以原点为圆心,D为半径的圆内, 无衰减地通过所 ...

最新文章

  1. 旋转目标检测rotation-yolov5笔记
  2. easyui复杂表单_EasyUI中实现form表单提交的示例分享
  3. 允许活动内容如脚本和activex控件
  4. 基于xilinx异构平台上视频采集分析
  5. 参数binlog_rows_query_log_events和binlog_row_image 与用 binlog恢复数据
  6. java xml出错,Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException...
  7. wps完成率怎么设置_WPS表格中如何计算完成率?详细操作方法看这里!
  8. PHP学习系列(1)——字符串处理函数(2)
  9. MySQL 常用的查询命令
  10. UI进阶——XMPP即时通讯
  11. GNU autotools 下载和安装
  12. c语言编译bss和data,.bss段和.data段的区别
  13. 无法打开FTP在 windows资源管理器中打开FTP站点解决方法
  14. python缺失数据处理_python 缺失值处理的方法(Imputation)
  15. “限时分享“ 本地80个小游戏 HTML+CSS+JS源码分享
  16. 鸿蒙系统代还,连米粉都骂的小米11 ultra,现在还能有人买吗?
  17. 智能+建筑>智能建筑
  18. 解决梯度消失和梯度弥散的方法
  19. 精读《Prisma 的使用》
  20. 搜索引擎常用技巧——英文资料篇

热门文章

  1. VS2010 编译程序自动生成ipch文件夹和.sdf文件
  2. 4090显卡上部署 Baichuan-13B-Chat
  3. 第八周存款利息计算器
  4. ichunqiu Misc Web 爆破2
  5. 合约自动化跟单系统项目开发逻辑(代码演示方案)
  6. 2020年新的开始和新的征程
  7. 中小学生知识技能计算机竞赛题,2007中小学生计算机知识技能竞赛题.doc
  8. Duc 是一个工具集
  9. python实现亚毫秒(微秒)级延时
  10. 50-使用 epoll 改写服务器