赞
踩
顾名思义,取平均值的滤波方法。
如上图示,对于一个3*3大小的滤波器,每次取3*3大小范围内的平均值赋值给当前像素,特别注意,滤波器应该是归一化的,对于一个n*n大小的均值滤波器,其矩阵形式为:
h
m
e
a
n
=
[
1
n
1
n
⋅
⋅
⋅
1
n
1
n
1
n
⋅
⋅
⋅
1
n
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
1
n
1
n
⋅
⋅
⋅
1
n
]
=
1
n
[
1
1
⋅
⋅
⋅
1
1
1
⋅
⋅
⋅
1
⋅
⋅
⋅
⋅
⋅
⋅
⋅
⋅
1
1
⋅
⋅
⋅
1
]
h_{mean} = \left[
在Matlab中, h m e a n h_{mean} hmean可以通过fspecial函数生成:
h = fspecial('average',hsize) returns an averaging filter h of size hsize.
例如:
>> h = fspecial('average', 3)
h =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
>>
易知,滤波器的size越大,滤波后的图像越模糊。
function imgMean = meanfilter(img,filterSize)
% img: image
% filterSize: if=5, it means 5*5
% Generate mean filter
img = double(img);
meanFilter = fspecial('average', filterSize);
% cov
imgMean = convolutionM(img, meanFilter);
imgMean = uint8(imgMean);
end
用高斯滤波器对图像进行滤波,例如:
h
g
a
u
s
s
=
[
0.075
,
0.125
,
0.075
0.125
,
0.200
,
0.125
0.075
,
0.125
,
0.075
]
h_{gauss} = \left[
在Matlab中, h g a u s s h_{gauss} hgauss可以通过fspecial函数生成:
h = fspecial('gaussian',hsize,sigma) returns a rotationally symmetric Gaussian lowpass filter of size hsize with standard deviation sigma. Not recommended. Use imgaussfilt or imgaussfilt3 instead.
Standard deviation, specified as a positive number.
例如:
>> h = fspecial('gaussian', 3, 1)
h =
0.0751 0.1238 0.0751
0.1238 0.2042 0.1238
0.0751 0.1238 0.0751
>>
function imgGaussian = gaussianfilter(img,filterSize,sigma)
% img: image
% filterSize: if=5, it means 5*5
% sigma: Standard deviation, specified as a positive number.
% Generate gaussian filter
img = double(img);
gaussianFilter = fspecial('gaussian', filterSize, sigma);
% cov
imgGaussian = convolutionM(img, gaussianFilter);
imgGaussian = uint8(imgGaussian);
end
用拉普拉斯算子对图像进行滤波,例如:
h
l
a
p
l
a
c
e
=
[
0
,
1
,
0
1
,
−
4
,
1
0
,
1
,
0
]
h_{laplace} = \left[
在Matlab中, h l a p l a c e h_{laplace} hlaplace可以通过fspecial函数生成:
h = fspecial('laplacian',alpha) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator, alpha controls the shape of the Laplacian.
Shape of the Laplacian, specified as a scalar in the range [0 1].
例如:
>> fspecial('laplacian', 0)
ans =
0 1 0
1 -4 1
0 1 0
>>
function imgLaplace = laplacefilter(img,alpha)
% img: image
% filterSize: if=5, it means 5*5
% alpha: Shape of the Laplacian, specified as a scalar in the range [0 1].
% Generate laplace filter
img = double(img);
laplaceFilter = fspecial('laplacian', alpha);
% cov
imgLaplace = convolutionM(img, laplaceFilter);
imgLaplace = uint8(imgLaplace);
end
用prewitt算子对图像进行滤波,例如:
h
x
p
=
[
−
1
0
1
−
1
0
1
−
1
0
1
]
h^{p}_{x} = \left[
在Matlab中, h p r e w i t t h_{prewitt} hprewitt可以通过fspecial函数生成:
h = fspecial('prewitt') returns a 3-by-3 filter that emphasizes horizontal edges by approximating a vertical gradient. To emphasize vertical edges, transpose the filter h'.
例如:
>> h = fspecial('prewitt') h = 1 1 1 0 0 0 -1 -1 -1 >> h = fspecial('prewitt')' % 转置 h = 1 0 -1 1 0 -1 1 0 -1 >>
function imgPrewitt = prewittfilter(img)
% img: image
% Generate prewitt filter
img = double(img);
prewittFilter = fspecial('prewitt');
% cov
imgPrewitt = convolutionM(img, prewittFilter);
imgPrewitt = uint8(imgPrewitt);
end
用sobel算子对图像进行滤波,例如:
h
x
s
=
[
−
1
0
1
−
2
0
2
−
1
0
1
]
h^{s}_{x} = \left[
在Matlab中, h s o b e l h_{sobel} hsobel可以通过fspecial函数生成:
h = fspecial('sobel')` returns a 3-by-3 filter that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient. To emphasize vertical edges, transpose the filter h'.
例如:
>> h = fspecial('sobel') h = 1 2 1 0 0 0 -1 -2 -1 >> h = fspecial('sobel')' % 转置 h = 1 0 -1 2 0 -2 1 0 -1 >>
function imgSobel = sobelfilter(img)
% img: image
% Generate sobel filter
img = double(img);
sobelFilter = fspecial('sobel');
% cov
imgSobel = convolutionM(img, sobelFilter);
imgSobel = uint8(imgSobel);
end
首先根据滤波器大小padding图像,再进行滤波:
function result=convolutionM(img, filter) [~, ~, channel] = size(img); pad_siz = idivide(size(filter,1), int32(2)); for k = 1:channel imgC = img(:, :, k); % pad img by 0 pad_img = zeros(int32(size(imgC))+pad_siz*2); pad_img(1+pad_siz:end-pad_siz, 1+pad_siz:end-pad_siz) = imgC; % conv2 out_img = zeros(size(pad_img)); for i = 1+pad_siz:size(pad_img,1)-pad_siz for j = 1+pad_siz:size(pad_img,2)-pad_siz out_img(i,j) = sum(sum(filter.*pad_img(i+pad_siz:-1:i-pad_siz, ... j+pad_siz:-1:j-pad_siz))); end end % restore original size of img result(:, :, k) = out_img(1+pad_siz:end-pad_siz, 1+pad_siz:end-pad_siz); end
调用其他函数文件的主文件为:
% imgOrg = rgb2gray(imread("peppers.png")); imgOrg = imread("peppers.png"); subplot(2,3,1); imshow(imgOrg); title("Originall img"); % mean filter imgMean = meanfilter(imgOrg, 3); % Generally, the size of a filter is an odd num subplot(2,3,2); imshow(imgMean); title("Mean filter img"); % gaussian filter imgGaussian = gaussianfilter(imgOrg, 3, 1); subplot(2,3,3); imshow(imgGaussian); title("Gaussian filter img"); % laplace filter imgSobel = laplacefilter(imgOrg, 0.2); subplot(2,3,4); imshow(imgSobel); title("Laplace filter img"); % prewitt filter imgSobel = prewittfilter(imgOrg); subplot(2,3,5); imshow(imgSobel); title("Prewitt filter img"); % sobel filter imgSobel = sobelfilter(imgOrg); subplot(2,3,6); imshow(imgSobel); title("Sobel filter img");
源码下载地址:点击此处
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。