当前位置:   article > 正文

LocalOperation01_图像滤波(壹):mean、gauss、laplace、prewitt、sobel_laplace filter

laplace filter

1 均值滤波

1.1 原理

顾名思义,取平均值的滤波方法。

在这里插入图片描述
如上图示,对于一个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[

1n 1n ··· 1n1n 1n ··· 1n··········1n 1n ··· 1n
\right] = \frac{1}{n}\left[
1 1 ··· 11 1 ··· 1········1 1 ··· 1
\right] hmean=n1 n1  n1n1 n1  n1n1 n1  n1=n11 1  11 1  11 1  1

在Matlab中, h m e a n h_{mean} hmean可以通过fspecial函数生成:

h = fspecial('average',hsize) returns an averaging filter h of size hsize.
  • 1

例如:

>> h = fspecial('average', 3)

h =

    0.1111    0.1111    0.1111
    0.1111    0.1111    0.1111
    0.1111    0.1111    0.1111
    
>>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

易知,滤波器的size越大,滤波后的图像越模糊。

1.2 函数编写

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2 高斯滤波

2.1 原理

用高斯滤波器对图像进行滤波,例如:

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[

0.075, 0.125, 0.0750.125, 0.200, 0.1250.075, 0.125, 0.075
\right] hgauss=0.075, 0.125, 0.0750.125, 0.200, 0.1250.075, 0.125, 0.075

在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.
  • 1
  • 2

例如:

>> h = fspecial('gaussian', 3, 1)

h =

    0.0751    0.1238    0.0751
    0.1238    0.2042    0.1238
    0.0751    0.1238    0.0751
    
>>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.2 函数编写

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3 laplace算子

3.1 原理

用拉普拉斯算子对图像进行滤波,例如:

h l a p l a c e = [ 0 ,      1 ,     0 1 ,   − 4 ,   1 0 ,     1 ,     0 ] h_{laplace} = \left[

0,    1,   01, 4, 10,   1,   0
\right] hlaplace=0,    1,   01, 4, 10,   1,   0

在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].
  • 1
  • 2

例如:

>> fspecial('laplacian', 0)

ans =

     0     1     0
     1    -4     1
     0     1     0

>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.2 函数编写

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4 prewitt算子

4.1 原理

用prewitt算子对图像进行滤波,例如:

h x p = [ − 1    0    1 − 1    0    1 − 1    0    1 ] h^{p}_{x} = \left[

1  0  11  0  11  0  1
\right] hxp=1  0  11  0  11  0  1 h y p = [ 1       1       1 0       0       0 − 1   − 1   − 1 ] h^{p}_{y} = \left[
1     1     10     0     01 1 1
\right]
hyp=1     1     10     0     01 1 1

在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'.
  • 1

例如:

>> 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

>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

4.2 函数编写

function imgPrewitt = prewittfilter(img)
% img: image

% Generate prewitt filter
img = double(img);
prewittFilter = fspecial('prewitt');
% cov
imgPrewitt = convolutionM(img, prewittFilter);
imgPrewitt = uint8(imgPrewitt);

end

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

5 sobel算子

5.1 原理

用sobel算子对图像进行滤波,例如:

h x s = [ − 1    0    1 − 2    0    2 − 1    0    1 ] h^{s}_{x} = \left[

1  0  12  0  21  0  1
\right] hxs=1  0  12  0  21  0  1 h y s = [ 1       2       1 0       0       0 − 1   − 2   − 1 ] h^{s}_{y} = \left[
1     2     10     0     01 2 1
\right]
hys=1     2     10     0     01 2 1

在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'.
  • 1

例如:

>> 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

>> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

5.2 函数编写

function imgSobel = sobelfilter(img)
% img: image

% Generate sobel filter
img = double(img);
sobelFilter = fspecial('sobel');
% cov
imgSobel = convolutionM(img, sobelFilter);
imgSobel = uint8(imgSobel);

end

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6 滤波卷积实现convolutionM函数

首先根据滤波器大小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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

7 main.m文件

调用其他函数文件的主文件为:

% 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");

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

8 效果展示

rgbResult
grayResult

9 源码下载

源码下载地址:点击此处

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/110600
推荐阅读
相关标签
  

闽ICP备14008679号