您的位置:首页 > 运维架构

OpenCV 之 图像平滑

2016-05-09 00:33 423 查看

OpenCV 之 图像平滑

1 图像平滑

图像平滑,可用来对图像进行去噪 (noise reduction) 或 模糊化处理 (blurring),实际上图像平滑仍然属于图像空间滤波的一种 (低通滤波)

既然是滤波,则图像中任一点 (x, y),经过平滑滤波后的输出 g(x, y) 如下:

g(x,y)=∑s=−aa∑t=−bbw(s,t)f(x+s,y+t)g(x,y)=∑s=−aa∑t=−bbw(s,t)f(x+s,y+t)

以 3X3 的滤波器为例 (即 a=b=1),则矩阵 Mx 和 Mf 对应的元素乘积之和,就是 g(x, y)

其中,Mx=⎡⎣⎢w(−1,−1)w(0,−1)w(1,−1)w(−1,0)w(0,0)w(1,0)w(−1,1)w(1,1)w(1,1)⎤⎦⎥Mf=⎡⎣⎢f(x−1,y−1)f(x,y−1)f(x+1,y−1)f(x−1,y)f(x,y)f(x+1,y)f(x−1,y+1)f(x+1,y+1)f(x+1,y+1)⎤⎦⎥Mx=[w(−1,−1)w(−1,0)w(−1,1)w(0,−1)w(0,0)w(1,1)w(1,−1)w(1,0)w(1,1)]Mf=[f(x−1,y−1)f(x−1,y)f(x−1,y+1)f(x,y−1)f(x,y)f(x+1,y+1)f(x+1,y−1)f(x+1,y)f(x+1,y+1)]

2 OpenCV 函数

OpenCV 中主要有四个函数涉及到图像平滑,分别是盒式滤波 (box),高斯滤波 (Gaussian),中值滤波 (median),双边滤波 (bilateral)

2.1 盒式滤波

输出图像的任一像素灰度值,等于其所有邻域像素灰度值的平均值

模糊化核为,K=α⎡⎣⎢⎢⎢111111............111111⎤⎦⎥⎥⎥K=α[11...1111...11...11...11] 其中,α={1ksize.weidth∗ksize.height1when normalize = trueotherwiseα={1ksize.weidth∗ksize.heightwhen normalize = true1otherwise

View Code
3.2 滤波对比

实际中,可直接调用以上四个滤波函数,代码如下:

1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3
4 using namespace std;
5 using namespace cv;
6
7 int main()
8 {
9     Mat src = imread("E:/smooth/bird.jpg");
10     if(src.empty())    return -1;
11
12     namedWindow("original", CV_WINDOW_AUTOSIZE);
13     namedWindow("blur", CV_WINDOW_AUTOSIZE);
14     namedWindow("GaussianBlur", CV_WINDOW_AUTOSIZE);
15     namedWindow("medianBlur", CV_WINDOW_AUTOSIZE);
16     namedWindow("bilateralFilter", CV_WINDOW_AUTOSIZE);
17
18     imshow("original", src);
19
20     Mat dst;
21
22     blur(src, dst, Size(3,3));
23     imshow("blur", dst);
24
25     medianBlur(src,dst,3);
26     imshow("medianBlur",dst);
27
28     GaussianBlur(src,dst,Size(3,3),0);
29     imshow("GaussianBlur",dst);
30
31     bilateralFilter(src,dst,9,50,50);
32     imshow("bilateralFilter",dst);
33
34     waitKey(0);
35     return 0;
36 }


四种滤波方法的效果图,如下所示:



参考资料

<Digital Image Processing_3rd> chapter 3

<Learning OpenCV_2nd>

<OpenCV Tutorials> imgproc module - Smoothing Images
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: