图像增强(2-卷积操作)——内涵MATLAB代码

接着上节来。。。。。

       卷积操作在图像的处理中是贼重要的一节,是必须要掌握的一部分。

     卷积部分咱不聊啥嘛求蛋蛋的公式,咱只需知道,卷积操作在图像上咋运行的就成。卷积操作就是采用一个卷积核在原图像上的滑动。在图像处理中,具体操作卷积的方式有多种:

         1) filter2(b,A,shape),b为卷积核,A为原图像(假设大小为m×n),shape指定计算的范围,有三种可选:

  • ‘full’在A的边界补上两圈0,卷积输出的结果C的大小将为(m+2)×(n+2);
  • ‘same’在A的边界处补上一圈的0,卷积输出的结果C的大小与A的相同为m×n;
  • ‘valid’不考虑边界补0,只计算有效的输出部分,卷积输出的结果C的大小为(m-2)×(n-2)。

该函数实现的操作是采用相关性进行的卷机操作,也就是直接用卷积核b在A上滑动,得到结果。

        2)  imfilter(A,b,option1,option2,option3),A为输入图像,b为卷积核,

  •  option1:边界选项,可选的有:补充固定的值X(默认都补零),symmetric(镜像反射),replicate(复制外边界值),circular(图像大小通过将图像看成是一个二维周期函数的一个周期来扩展)
  • option2:输出图像大小选项,可选的有same(默认),full
  • option3:可选的卷积滤波方式:‘corr’(使用相关性来进行卷积,默认此法。该法与filter2相同的相关卷积操作);‘conv’(使用卷积的方法进行操作)

       3) conv2(A,b,shape),A为输入图像,b为卷积核,shape与filter2中的shape一致。实现的结果与imfilter的卷积方式选‘conv’一致。

但在该处有一点需注意,在conv2(A,b,shape)进行卷积计算时,将卷积核b旋转了180度,由卷积的定理和性质可以很好的理解这一点。

OK,上面这些就是在图像增强中常用与去噪、平滑或增强边缘的卷积操作,虽然是有三个不同的函数可实现,但相互之间都是有联系的,在学习和使用的过程,用哪一个都可以。

来个小矩阵,来看一下上面三种不同方式的卷积结果。(不了解咋样的计算过程的小伙伴,可以具体的计算一下,有助于更好地理解

假设:

A=\left [ \begin{matrix} 1\2 \3 \4\\ 4 \5 \6\7 \\ 7\ 8\ 9 \5\\ 6 \3\ 5\2 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ]

这里A是我们要处理的矩阵,b在卷积操作中叫做卷积核,整个卷积过程就是利用卷积核b在A上进行滑动,得到一个新的矩阵C。

filter2(b,A,shape):

          情况1:shape = ‘full’情况下(filter2(b,A,'full')):A在进行卷积之前会先在周围补两圈0,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\0\0\\0\0\1\2 \3 \4\0\0\\0\0\ 4 \5 \6\7 \0\0\\ 0\0\7\ 8\ 9 \5\0\0\\ 0\0\6 \3\ 5\2\0\0\\0\0\0\0\0\0\0\0\\0\0\0\0\0\0\0\0 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 3 \ 10 \ 19\ 28 \ 22 \ 8\\ 13 \ 34 \ 52 \ 64 \ 47 \ 18\\ 27 \ 71 \ 109 \ 117\ 84 \ 29\\ 33 \ 82 \ 117 \ 119 \ 92 \ 30\\ 20 \ 67 \101 \ 98 \ 64 \ 17\\ 12 \ 42 \ 46 \ 43 \ 27 \ 6\end{matrix} \right ]

      情况2:shape = ‘same’情况下(filter2(b,A,'same')):A在进行卷积之前会先在周围补一圈0,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\\0\1\2 \3 \4\0\\0\ 4 \5 \6\7 \0\\ 0\7\ 8\ 9 \5\0\\ 0\6 \3\ 5\2\0\\0\0\0\0\0\0 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 34 \ 52 \ 64 \ 47\\ 71 \ 109\ 117\ 84\\ 82 \ 117\ 119 \ 92\\ 67 \ 101 \ 98 \ 64\end{matrix} \right ]

     情况3:shape = ‘valid’情况下(filter2(b,A,'valid')):,如下所示:

A=\left [ \begin{matrix} 1\2 \3 \4\\ 4 \5 \6\7 \\ 7\ 8\ 9 \5\\ 6 \3\ 5\2 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 109 \ 117\\ 117 \ 119\end{matrix} \right ]

从上计算结果可以看出,情况2取情况1的中间部分,情况3又仅取情况2的中间部分。

imfilter(A,b,option1,option2,option3)

            为了与filter2()具有对比性,在这里,option1 选择默认的补0方式。

option3 = ‘corr’时:

      情况1:option2 = ‘same’的情况下(imfilter(A, b, 'same', 'corr')):A在进行卷积之前会先在周围补一圈0,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\\0\1\2 \3 \4\0\\0\ 4 \5 \6\7 \0\\ 0\7\ 8\ 9 \5\0\\ 0\6 \3\ 5\2\0\\0\0\0\0\0\0 \end{matrix} \right ]b{}'=\left [ \begin{matrix} 3\4\2\\1\1\1\\2\3\6\end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 34 \ 52 \ 64 \ 47\\ 71 \ 109\ 117\ 84\\ 82 \ 117\ 119 \ 92\\ 67 \ 101 \ 98 \ 64\end{matrix} \right ]

       情况2:option2 = ‘full’的情况下(imfilter(A, b, 'full', 'corr')):A在进行卷积之前会先在周围补两圈0,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\0\0\\0\0\1\2 \3 \4\0\0\\0\0\ 4 \5 \6\7 \0\0\\ 0\0\7\ 8\ 9 \5\0\0\\ 0\0\6 \3\ 5\2\0\0\\0\0\0\0\0\0\0\0\\0\0\0\0\0\0\0\0 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 3 \ 10 \ 19\ 28 \ 22 \ 8\\ 13 \ 34 \ 52 \ 64 \ 47 \ 18\\ 27 \ 71 \ 109 \ 117\ 84 \ 29\\ 33 \ 82 \ 117 \ 119 \ 92 \ 30\\ 20 \ 67 \101 \ 98 \ 64 \ 17\\ 12 \ 42 \ 46 \ 43 \ 27 \ 6\end{matrix} \right ]

这表明,当option1 选择默认的补0方式,option3 = ‘corr’时,imfilter(A,b,option1,option2,option3)与filter2(b,A,shape)时一样的。注意:在imfilter()中,option2没有‘valid’这一选择。

option3 = ‘conv’时:

         情况1:option2 = ‘same’的情况下(imfilter(A, b, 'same', 'conv')):A在进行卷积之前会先在周围补一圈0,将b旋转180度为b',如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\\0\1\2 \3 \4\0\\0\ 4 \5 \6\7 \0\\ 0\7\ 8\ 9 \5\0\\ 0\6 \3\ 5\2\0\\0\0\0\0\0\0 \end{matrix} \right ]b{}'=\left [ \begin{matrix} 3\4\2\\1\1\1\\2\3\6\end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 42 \62 \ 76 \ 61\\ 83 \ 121\ 129 \ 86\\ 86 \113 \ 117\ 82\\ 53 \85 \80 \ 54\end{matrix} \right ]

          情况2:option2 = ‘full’的情况下(imfilter(A, b, 'full', 'conv')):A在进行卷积之前会先在周围补两圈0,将b旋转180度为b‘,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\0\0\\0\0\1\2 \3 \4\0\0\\0\0\ 4 \5 \6\7 \0\0\\ 0\0\7\ 8\ 9 \5\0\0\\ 0\0\6 \3\ 5\2\0\0\\0\0\0\0\0\0\0\0\\0\0\0\0\0\0\0\0 \end{matrix} \right ]b{}'=\left [ \begin{matrix} 3\4\2\\1\1\1\\2\3\6\end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 3 \ 12 \ 23 \ 34 \ 30\ 8\\ 13 \ 42 \ 62 \ 76 \ 61 \ 18\\ 27 \ 83 \121 \ 129 \ 86 \ 29\\ 33 \ 86 \ 113 \ 117 \ 82 \ 30\\ 20 \ 53 \ 85 \ 80 \ 54 \ 17\\ 12 \ 30\ 40 \33 \ 23 \ 6\end{matrix} \right ]

conv2(A,b,shape)

         情况1:shape = ‘full’情况下(conv2(A,b,'full')):A在进行卷积之前会先在周围补两圈0,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\0\0\\0\0\1\2 \3 \4\0\0\\0\0\ 4 \5 \6\7 \0\0\\ 0\0\7\ 8\ 9 \5\0\0\\ 0\0\6 \3\ 5\2\0\0\\0\0\0\0\0\0\0\0\\0\0\0\0\0\0\0\0 \end{matrix} \right ]b{}'=\left [ \begin{matrix} 3\4\2\\1\1\1\\2\3\6\end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 3 \ 12 \ 23 \ 34 \ 30\ 8\\ 13 \ 42 \ 62 \ 76 \ 61 \ 18\\ 27 \ 83 \121 \ 129 \ 86 \ 29\\ 33 \ 86 \ 113 \ 117 \ 82 \ 30\\ 20 \ 53 \ 85 \ 80 \ 54 \ 17\\ 12 \ 30\ 40 \33 \ 23 \ 6\end{matrix} \right ]

      情况2:shape = ‘same’情况下(conv2(A,b,'same')):A在进行卷积之前会先在周围补一圈0,如下所示:

A=\left [ \begin{matrix} 0\0\0\0\0\0\\0\1\2 \3 \4\0\\0\ 4 \5 \6\7 \0\\ 0\7\ 8\ 9 \5\0\\ 0\6 \3\ 5\2\0\\0\0\0\0\0\0 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix} 42 \62 \ 76 \ 61\\ 83 \ 121\ 129 \ 86\\ 86 \113 \ 117\ 82\\ 53 \85 \80 \ 54\end{matrix} \right ]

     情况3:shape = ‘valid’情况下(conv2(A,b,'valid')):,如下所示:

A=\left [ \begin{matrix} 1\2 \3 \4\\ 4 \5 \6\7 \\ 7\ 8\ 9 \5\\ 6 \3\ 5\2 \end{matrix} \right ]b=\left [ \begin{matrix} 3\6 \2 \\ 1 \1 \1 \\ 2\ 4\ 3\ \end{matrix} \right ],卷积后的结果为:C=\left [ \begin{matrix}121 \129\\ 113 \ 117\end{matrix} \right ]

这表明,当option1 选择默认的补0方式,option3 = ‘conv’时,imfilter(A,b,option1,option2,option3)与conv2(A,b,shape)时一样的。注意:在imfilter()中,option2没有‘valid’这一选择。

从上面的计算结果可以看出,这三种函数相互之间的联系,在应用中你喜欢哪种,就用哪种咯。

OK,卷积咋算的知道了,那下面直接开干,在MATLAB中卷积核也就叫滤波器模板,可以用fspecial('type', parameters)来生成

我们比较常用的type有:

  • average(均值滤波): fspecial('average', [h,w]),[h,w]代表模板尺寸,默认的是[3 3]
  • disk(圆形区域均值滤波):fspecial('disk', radius),radius代表模板半径,默认的是5
  • gaussian(高斯低通滤波):fspecial(‘guassian', [h, w], signa),[h,w]代表模板尺寸,默认的是[3 3],sigma为滤波器的标准值,单位为像素,默认值为0.5。
  • laplacian(拉普拉斯算子):fspecial('laplacian', alpha),alpha用于控制拉普拉斯算子的形状,取值范围0~1,默认值0.2.
  • log(拉普拉斯高斯算子):fspecial(‘log', [h, w], signa),[h,w]代表模板尺寸,默认的是[3 3],sigma为滤波器的标准值,单位为像素,默认值为0.5。
  • motion(运动模糊算子):fspecial(‘motion', len, theta),表示摄像物体逆时针方向以theta角度运动了len个像素,len的默认值为9,theta的默认值为0。
  • prewitt(prewitt算子):fspecial('prewitt'),主要用于边缘增强,大小为[3 3]。
  • sobel(sobel算子):fspecial('sobel'),用于边缘增强、提取,大小为[3 3],分两个方向,另一方向为fspecial('sobel')的转置。
  • unsharp(对比度增强滤波器),fspecial('unsharp', alpha),alpha用于控制滤波器的形状,范围为【0,1】,默认值为0.2。

       此外,还有中值滤波函数medfilt2(A, [h,w])二维统计顺序滤波函数ordfilt2(A, order, [h, w], S),order表示顺序中的第几个数起作用,S时大小[h, w]的矩阵,它对应[h, w]中非0位置的输出偏置;自适应滤波器J = wiener2(A,[h, w], noise)表示使用h×w大小的临域局部图像的均值与偏差,采用像素式自适应滤波器对图像A进行滤波;或者(J, noise) = wiener2(A,[h, w]),在滤波器前估计附加噪声的能量。

来一波处理结果(附加源码):

均值滤波:

close all; clear all; clc
A1 = imread('cameraman.tif');
A = imnoise(A1,'gaussian', 0.05);

%均值滤波
b_average1 = fspecial('average');
b_average2 = fspecial('average',[5 5]);
b_average3 = fspecial('average', [9 9]);
C_average1_corr = imfilter(A,b_average1);
C_average2_corr = imfilter(A,b_average2);
C_average3_corr = imfilter(A,b_average3);
C_average1_conv2 = imfilter(A,b_average1,'conv');
C_average2_conv2 = imfilter(A,b_average2,'conv');
C_average3_conv2 = imfilter(A,b_average3,'conv');
figure;
subplot(331);imshow(A1);title('原图')
subplot(332);imshow(A);title('添加gaussian后的图像')
subplot(334);imshow(C_average1_corr);title('3×3corr均值滤波')
subplot(335);imshow(C_average2_corr);title('5×5corr均值滤波')
subplot(336);imshow(C_average3_corr);title('9×9corr均值滤波')
subplot(337);imshow(C_average1_conv2);title('3×3conv2均值滤波')
subplot(338);imshow(C_average2_conv2);title('5×5conv2均值滤波')
subplot(339);imshow(C_average3_conv2);title('9×9conv2均值滤波')

中值滤波:

close all; clear all; clc
A1 = imread('cameraman.tif');
A = imnoise(A1,'gaussian', 0.05);

%中值滤波
C_medfilt1 = medfilt2(A,[3 3]);
C_medfilt2 = medfilt2(A,[5 5]);
C_medfilt3 = medfilt2(A,[9 9]);

figure;
subplot(231);imshow(A1);title('原图')
subplot(232);imshow(A);title('添加gaussian后的图像')
subplot(234);imshow(C_medfilt1);title('3×3中值滤波')
subplot(235);imshow(C_medfilt2);title('5×5中值滤波')
subplot(236);imshow(C_medfilt3);title('9×9中值滤波')

拉普拉斯算子:

close all; clear all; clc
A1 = imread('cameraman.tif');
A = imnoise(A1,'gaussian', 0.05);

%拉普拉斯
b_laplacian1 = fspecial('laplacian');
b_laplacian2 = fspecial('laplacian',0.5);
b_laplacian3 = fspecial('laplacian', 0.7);
C_laplacian1 = imfilter(A,b_laplacian1);
C_laplacian2 = imfilter(A,b_laplacian2);
C_laplacian3 = imfilter(A,b_laplacian3);

figure;
subplot(231);imshow(A1);title('原图')
subplot(232);imshow(A);title('添加gaussian后的图像')
subplot(234);imshow(C_laplacian1);title('0.2的laplacian滤波')
subplot(235);imshow(C_laplacian2);title('0.5的laplacian滤波')
subplot(236);imshow(C_laplacian3);title('0.7的laplacian滤波')

sobel算子:

close all; clear all; clc
A = imread('cameraman.tif');

%sobel
b_sobel = fspecial('sobel');
C_sobel1 = imfilter(A,b_sobel);
C_sobel2 = imfilter(A,b_sobel');
C_sobel3 = imfilter(C_sobel1,b_sobel');


figure;
subplot(221);imshow(A);title('原图')
subplot(222);imshow(C_sobel1);title('水平方向的sobel滤波')
subplot(223);imshow(C_sobel2);title('垂直方向的sobel滤波')
subplot(224);imshow(C_sobel3);title('水平+垂直方向的sobel滤波')

其余的就不一一实现了,有兴趣的小伙伴,可以自己弄弄。

空域中的增强,到这里暂时告一段落。。。。。下节频率里的。。。。

发布了15 篇原创文章 · 获赞 5 · 访问量 4395

猜你喜欢

转载自blog.csdn.net/qq_33668060/article/details/98784645