机器视觉学习笔记--图像滤波1

来源:互联网 发布:偷网站源码包方法 编辑:程序博客网 时间:2024/06/05 07:15

基本理论

图像滤波的目的:去除图像噪声,修复图像损坏,改变图像分布。

  • 均值滤波(Mean Filter)

    求取NxN像素范围内的均值作为中心点的像素值,N为核大小
    这里写图片描述
    例:核为3
    例如核为3
    核越大,噪声消除效果越好,但图像也会越模糊(细节损失),因此在使用时要学会折衷。
    也可以给不同像素增加权值方式来消除这种影响
    这里写图片描述
    滤波效果如下:
    这里写图片描述
    实现代码:

clear;close all;img = imread('apple.jpg');img = rgb2gray(img);figure('name','Median filter')subplot(221);imshow(img);title('原图')%添加高斯噪声gauss_img  = imnoise(img,'gaussian',0,0.015);subplot(222);imshow(gauss_img);title('加入高斯噪声')%下面使用均值滤波来消除噪声h=fspecial('ave%h=fspecial('average',[3,3]);erage',[3,3]);img_filtered = imfiltsubplot(223);%subplot(224)imshow(img_filteredtitle('N=3')
  • 高斯滤波(Gaussian Filter)

高斯函数的傅里叶变换仍是高斯函数

这里写图片描述
高斯函数核不论在那个空间都可以被分离高
离散高斯滤波(Discrete Guassian Filter)

这里写图片描述
k为标准化算子
滤波效果如下:
这里写图片描述
实现代码如下:

clear;close all;img = imread('apple.jpg');img = rgb2gray(img);figure('name','Filter')subplot(221);imshow(img);title('原图')%添加高斯噪声gauss_img  = imnoise(img,'gaussian',0,0.015);subplot(222);imshow(gauss_img);title('加入高斯噪声');sigma = 1h=fspecial('gaussian',[5,5],sigma);img_filtered = imfilter(gauss_img,h,'replicate'); subplot(223);imshow(img_filtered);title('sigma =1');
  • 中值滤波(Median Filter)非线性
    将滤波核(filter mask)大小NxN个像素按照灰度值大小进行排序,选择序列的中值作为该点的像素值。这种滤波方法对椒盐噪声的滤波效果非常好。
    例如:
    这里写图片描述
    则滤波结果为:
    这里写图片描述
    对椒盐噪声的处理结果如下图所示:
    这里写图片描述
    实现代码如下:
clear;close all;img = imread('apple.jpg');img = rgb2gray(img);figure('name','Filter')subplot(221);imshow(img);title('原图')%添加高斯噪声%gauss_img  = imnoise(img,'gaussian',0,0.015);%添加椒盐噪声noise_img = imnoise(img,'salt',0.02);subplot(222);imshow(noise_img);title('加入盐噪声') %下面使用中值滤波来消除椒盐噪声img_filtered=medfilt2(noise_img,[5,5]);subplot(223);imshow(img_filtered);title('N=5')
  • 最小值滤波(Minimum Filter)
    最小值滤波适用于消除盐噪声(白点),与上述中值滤波相比,
    这里写图片描述
  • 最大值滤波(Maximum Filter)
    最大值滤波适用于消除胡椒噪声(黑点),与上述中值滤波相比,
    这里写图片描述
    最大值最小值滤波的实现效果如下图,由于新版本的matlab不能单独添加盐噪声/椒盐噪声,所以使用的是椒盐噪声的图像来进行实验的。
    这里写图片描述

“`matlab
clear;close all;
img = imread(‘apple.jpg’);
img = rgb2gray(img);
figure(‘name’,’Filter’)
subplot(221);
imshow(img);
title(‘原图’)
%添加高斯噪声
%gauss_img = imnoise(img,’gaussian’,0,0.015);
%添加椒盐噪声
noise_img = imnoise(img,’salt’,0.02);
subplot(222);
imshow(noise_img);
title(‘加入盐噪声’)
%下面使用最小值滤波
img_filtered=ordfilt2(noise_img,1,ones(3,3));
subplot(223);
imshow(img_filtered);
title(‘最小值滤波’)

%下面使用最大值滤波
img_filtered=ordfilt2(noise_img,9,ones(3,3));
subplot(224);
imshow(img_filtered);
title(‘最大值滤波’)
“`
- 中间值滤波(Midpoint Filter)
能够消除高斯噪声和均匀噪声(uniform noise)
这里写图片描述
暂时没有找到实现的代码,等到后面有时间再添加好了。
后面在另一篇文章中介绍下面两种滤波方法。
- 形态学滤波(Morphological Filter)
- 直方图调整(Histogram Modification,图像增强方法)

0 0
原创粉丝点击