opencv再学习之路(二)---滤波算法

 1 #include"stdafx.h"
 2 #include<opencv2opencv.hpp>
 3 #include<opencv2highguihighgui.hpp>
 4 #include<iostream>
 5 #include<Windows.h>
 6 #include<time.h>
 7 
 8 using namespace std;
 9 using namespace cv;
10 
11 // 常见数据结构使用方法
12 int main()
13 {
14     Mat sourceimage = imread("1.jpg");
15 
16     //  线性滤波:均值滤波  方框滤波
17 
18     Mat image1;
19     long starttime1 = GetTickCount();
20     blur(sourceimage,image1,Size(5,5));
21     namedWindow("均值滤波",WINDOW_NORMAL);
22     imshow("均值滤波",image1);
23     long endtime1 = GetTickCount();
24     cout << "均值滤波时间:" << (endtime1 - starttime1) << endl;
25 
26     Mat image2;
27     long starttime2 = GetTickCount();
28     boxFilter(sourceimage,image2,-1,Size(5,5));  // 参数 -1 代表采用原图深度
29     namedWindow("方框滤波",WINDOW_NORMAL);
30     imshow("方框滤波",image2);
31     long endtime2 = GetTickCount();
32     cout << "方框滤波时间:" << (endtime2 - starttime2) << endl;
33 
34     //  非线性滤波: 高斯滤波  双边滤波  中值滤波
35 
36     Mat image3;
37     long starttime3 = GetTickCount();
38     GaussianBlur(sourceimage,image3,Size(3,3),0,0);  // 0,0 为参数 sigx 和 sigy 的值
39     namedWindow("高斯滤波",WINDOW_NORMAL);
40     imshow("高斯滤波",image3);
41     long endtime3 = GetTickCount();
42     cout << "高斯滤波时间:" << (endtime3 - starttime3) << endl;
43 
44     Mat image4;
45     long starttime4 = GetTickCount();
46     bilateralFilter(sourceimage,image4,25,25*2,35/2);
47     namedWindow("双边滤波",WINDOW_NORMAL);
48     imshow("双边滤波",image4);
49     long endtime4 = GetTickCount();
50     cout << "双边滤波时间:" << (endtime4 - starttime4) << endl;
51 
52     Mat image5;
53     long starttime5 = GetTickCount();
54     medianBlur(sourceimage,image5,5);
55     namedWindow("中值滤波",WINDOW_NORMAL);
56     imshow("中值滤波",image5);
57     long endtime5 = GetTickCount();
58     cout << "中值滤波时间:" << (endtime5 - starttime5) << endl;
59 
60     waitKey(0);
61     return 0;
62 
63 }

自定义滤波模板:

  

 1 void main()
 2 {
 3     Mat srcImage =imread("01.jpg");
 4 
 5     // 基于拉普拉斯算子的图像锐化
 6     // 拉普拉斯滤波核 3*3
 7     // 0 -1  0
 8     //-1  5 -1
 9     // 0 -1  0
10     Mat kernel = (Mat_<float>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
11     Mat sharpen_laplace;
12     filter2D(srcImage, sharpen_laplace, srcImage.depth(), kernel);
13 
14     imshow("show", sharpen_laplace);
15 
16     waitKey(0);
17 }
原文地址:https://www.cnblogs.com/zhp218/p/8530355.html