#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespace cv; using namespace std; const int g_nTrackbarMaxValue = 9; //定义轨迹条最大值 int g_nTrackbarValue; //定义轨迹条初始值 int g_nKernelValue; //定义kernel尺寸 Mat src, dst; //均值滤波 void on_kernelTrackbar(int,void *) { //根据输入值重新计算kernel尺寸 g_nKernelValue = g_nTrackbarValue * 2 + 1; //均值滤波函数 blur(src, dst, Size(g_nKernelValue, g_nKernelValue)); imshow("均值滤波", dst); } //高斯滤波 void on_kernelTrackbar2(int, void*) { //根据输入值重新计算kernel尺寸 g_nKernelValue = g_nTrackbarValue * 2 + 1; //均值滤波函数 GaussianBlur(src, dst, Size(g_nKernelValue, g_nKernelValue),11,11); imshow("均值滤波", dst); } int main() { //原图 src = imread(".//pic//kate.png",IMREAD_UNCHANGED); if (!src.data) { cout << "load error" << endl; return -1; } namedWindow("均值滤波", WINDOW_AUTOSIZE); //定义滤波后图像显示窗口属性 //定义轨迹条名称和最大值 char kernelName[20]; sprintf(kernelName, "kernel尺寸 %d", g_nTrackbarMaxValue); //创建轨迹条 createTrackbar(kernelName, "均值滤波", &g_nTrackbarValue, g_nTrackbarMaxValue, on_kernelTrackbar2); on_kernelTrackbar(g_nTrackbarValue, 0); waitKey(0); return 0; }