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
相关文章推荐
- OpenCV 之 图像平滑
- wireshark使用心得 centos7安装wireshark: yum install wireshark wireshark-gnome
- wireshark使用心得 centos7安装wireshark: yum install wireshark wireshark-gnome
- OpenCV实践之路——MFC与OpenCV联合打开摄像头
- ubuntu Linux下Codeblocks编译 SDL2.0工程
- 记录在windows下和linux下搭建svn服务器,并通过mac,windows端连接。
- kali linux下几个更新命令的区分
- 玩转变量、环境变量以及数学运算(shell)
- shell学习(二)变量
- shell学习(三)运算符
- Linux下/proc目录简介
- Linux中ifreq 结构体分析和使用
- 解决Jenkins的job启动tomcat时会在job结束时关闭问题
- Linux Kernel 版本区别
- docker在centos7上安装私服镜像各种操蛋修复
- IDEA使用的感触
- node.js项目架构
- linux介绍 开篇
- CentOS中yum源的扩展以及本地备份
- Linux挂载命令mount用法及参数详解