1 线性滤波API函数
1.1 方框滤波函数:boxFilter函数
<1> . boxFilter()函数的作用:使用方框滤波来模糊一张图像。
<2> . 函数原型:void boxFilter(InputArray src, OutputArray dst, int ddepth, Size ksize,Point anchor=Point(-1, -1), boolnormalize=true, int borderType=BORDER_DEFAULT);
<3>. 调用代码的示例:
Mat image = imread("2.jpg");
Mat out;
boxFilter(image, out, -1, Size(5, 5));
<4> 实例
实例代码:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("../1.jpg");
namedWindow("原图");
namedWindow("均值滤波效果图");
imshow("原图", image);
Mat out;
boxFilter(image, out, -1, Size(3, 3));
imshow("均值滤波效果图", out);
// waitKey(0);
while(char (waitKey(1)) != 'q') {}
}
运行效果:
1.2 均值滤波:blur()函数
<1> blur函数的作用:对图像进行均值滤波后输出;
<2> 函数原型:void blur(InputArray src, OutputArraydst, Size ksize, Point anchor=Point(-1, -1), int borderType=BORDER_DEFAULT);
<3> 调用代码示例:
Mat image = imread("1.jpg");
Mat out;
blur(image, out, Size(7, 7));
<4> 实例
实例代码:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
Mat image = imread("../1.jpg");
Mat out;
namedWindow("原图");
namedWindow("均值滤波图");
blur(image, out, Size(3, 3));
imshow("原图", image);
imshow("均值滤波图", out);
while (char (waitKey(1)) != 'q') {}
}
运行效果:
1.3 高斯滤波:GaussianBlur()函数
<1>GaussianBlur函数作用:模糊一张图像;
<2> 原型:void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, intborderType=BORDER_DEFAULT);
<3> 调用示例
Mat image= imread("1.jpg");
Mat out;
GaussianBlur(image, out, Size(5, 5), 0, 0);
<4> 实例
实例代码:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
int main()
{
Mat image = imread("../1.jpg");
Mat out;
namedWindow("原图");
namedWindow("高斯滤波图");
GaussianBlur(image, out, Size(3, 3), 0, 0);
imshow("原图", image);
imshow("高斯滤波图", out);
while (char (waitKey(1)) != 'q') {}
}
运行效果:
1.4 图像滤波综合实例化
使用滑动条来控制三种线性滤波的核参数值,通过滑动条,就可以控制图像的在不同的滤波方法下的模糊度。
实例代码:
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
Mat g_srcImage, g_dstImage1, g_dstImage2, g_dstImage3;
int g_nBoxFilterValue = 3; //方框滤波参数值
int g_nMeanBlurValue = 3;
int g_nGaussianValue = 3;
static void On_BoxFilter(int, void*); // 方框滤波
static void On_MeanBlur(int , void*);
static void On_GaussianBlur(int, void*);
int main()
{
g_srcImage = imread("../1.jpg");
if(!g_srcImage.data){
printf("获取图像错误!\n");
return false;
}
//复制原图到三个Mat 中
g_dstImage1 = g_srcImage.clone();
g_dstImage2 = g_srcImage.clone();
g_dstImage3 = g_srcImage.clone();
//显示原图
namedWindow("原图", 1);
imshow("原图", g_srcImage);
//方框滤波
namedWindow("方框滤波");
createTrackbar("内核值:", "方框滤波", &g_nBoxFilterValue, 40, On_BoxFilter);
On_BoxFilter(g_nBoxFilterValue, 0);
//均值滤波
namedWindow("均值滤波");
createTrackbar("内核值:", "均值滤波", &g_nMeanBlurValue, 40, On_MeanBlur);
On_MeanBlur(g_nMeanBlurValue, 0);
//高斯滤波
namedWindow("高斯滤波");
createTrackbar("内核值:", "高斯滤波", &g_nGaussianValue, 40, On_GaussianBlur);
while (char (waitKey(1)) != 'q') {}
return 0;
}
static void On_BoxFilter(int, void*)
{
boxFilter(g_srcImage, g_dstImage1, -1, Size(g_nBoxFilterValue + 1, g_nBoxFilterValue + 1));
imshow("方框滤波", g_dstImage1);
}
static void On_MeanBlur(int , void*)
{
blur(g_srcImage, g_dstImage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1), Point(-1, -1));
imshow("均值滤波", g_dstImage2);
}
static void On_GaussianBlur(int, void*)
{
GaussianBlur(g_srcImage, g_dstImage3, Size(g_nGaussianValue * 2 + 1, g_nGaussianValue * 2 + 1), 0, 0); //一定是正数和奇数
imshow("高斯滤波", g_dstImage3);
}