当前位置:网站首页>【OpenCV】-5种图像滤波的综合示例
【OpenCV】-5种图像滤波的综合示例
2022-07-02 02:04:00 【我菜就爱学】
序言: 这一篇文章是对之前学习线性滤波和非线性滤波的回顾。之前都是分章节介绍滤波器对图片的处理,这一小节将前文介绍的知识点以代码为载体,展现给大家,用滑动条的方式来控制学习到的各种滤波(方框滤波、均值滤波、高斯滤波、中值滤波、双边滤波)的参数值。通过滚动条来控制图像在各种平滑处理下的模糊度,不仅可以与原图对比看出效果,而且也有一定的可玩性,哈哈。
话不多说,上代码
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
//---------------------------------
//全局变量声明
//-------------------------------
Mat g_srcIamge, g_dstImage1, g_dstImage2, g_dstImage3,g_dstImage4, g_dstImage5;//存储图片的Mat类型
int g_nBoxFilterValue = 3;//方框滤波参数值
int g_nMeanBlurValue = 3;//均值滤波参数值
int g_nGaussianBlurValue = 3;//高斯滤波参数值
int g_nMedianBlurValue = 10;//中值滤波参数值
int g_nBilateralFilterValue = 10;//双边滤波参数值
//---------------------------------
//全局函数声明
//-------------------------------
static void on_BoxFilter(int, void *);//方框滤波
static void on_MeanBlur(int, void *);//均值滤波
static void on_GaussianBlur(int, void *);//高斯滤波
static void on_MedianBlur(int, void *);//中值滤波器
static void on_BilateralFilter(int, void *);//双边滤波器
int main()
{
//改变console字体颜色
system("color 5E");
//载入原图
g_srcIamge = imread("E:\\Pec\\shihao.jpg", 1);
if (!g_srcIamge.data)
{
printf("读取图片错误\n");
return false;
}
//复制原图到三个Mat类型中
g_dstImage1 = g_srcIamge.clone();
g_dstImage2 = g_srcIamge.clone();
g_dstImage3 = g_srcIamge.clone();
g_dstImage4 = g_srcIamge.clone();
g_dstImage5 = g_srcIamge.clone();
//显示原图
namedWindow("【<0>原图窗口】", 1);
imshow("【<0>原图窗口】", g_srcIamge);
//=========1、方框滤波===========
//创建窗口
namedWindow("【<1>方框滤波】", 1);
//创建轨迹条
createTrackbar("内核值:", "【<1>方框滤波】", &g_nBoxFilterValue, 40, on_BoxFilter);
on_MeanBlur(g_nBoxFilterValue, 0);
//======2、均值滤波===========
namedWindow("【<2>均值滤波】", 1);
//创建轨迹条
createTrackbar("内核值:", "【<2>均值滤波】", &g_nMeanBlurValue, 40, on_BoxFilter);
on_MeanBlur(g_nMeanBlurValue, 0);
//=====3、高斯滤波===============
namedWindow("【<3>高斯滤波】", 1);
//创建轨迹条
createTrackbar("内核值:", "【<3>高斯滤波】", &g_nGaussianBlurValue, 40, on_BoxFilter);
on_GaussianBlur(g_nGaussianBlurValue, 0);
//=====4、中值滤波===============
namedWindow("【<4>中值滤波】", 1);
//创建轨迹条
createTrackbar("内核值:", "【<4>中值滤波】", &g_nMedianBlurValue, 50, on_MeanBlur);
on_MedianBlur(g_nMedianBlurValue, 0);
//=====5、双边滤波===============
namedWindow("【<5>双边滤波】", 1);
//创建轨迹条
createTrackbar("内核值:", "【<5>双边滤波】", &g_nBilateralFilterValue, 50, on_BilateralFilter);
on_BilateralFilter(g_nBilateralFilterValue, 0);
//输出一些帮助信息
cout << endl << "请调整滚动条观察图像效果~\n\n" << "\t按下“q”键时,程序退出~\n";
//在waitKey(1)之后输入一个值
while (char(waitKey(1)) != 'q') {
}
return 0;
}
//----------------------
//方框滤波操作的回调函数
//---------------------------
static void on_BoxFilter(int, void *)//方框滤波
{
boxFilter(g_srcIamge, g_dstImage1, -1, Size(g_nBoxFilterValue + 1, g_nBoxFilterValue + 1));
imshow("【<1>方框滤波】", g_dstImage1);
}
static void on_MeanBlur(int, void *)//均值滤波
{
blur(g_srcIamge, g_dstImage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1), Point(-1, -1));
imshow("【<2>均值滤波】", g_dstImage2);
}
static void on_GaussianBlur(int, void *)//高斯滤波
{
GaussianBlur(g_srcIamge, g_dstImage3,
Size(g_nGaussianBlurValue * 2 + 1, g_nGaussianBlurValue * 2 + 1), 0, 0);
imshow("【<3>高斯滤波】", g_dstImage3);
}
static void on_MedianBlur(int, void *)//中值滤波器
{
medianBlur(g_srcIamge, g_dstImage4, g_nMedianBlurValue * 2 + 1);
imshow("【<4>中值滤波】", g_dstImage4);
}
static void on_BilateralFilter(int, void *)//双边滤波器
{
bilateralFilter(g_srcIamge, g_dstImage5, g_nBilateralFilterValue, g_nBilateralFilterValue * 2,
g_nBilateralFilterValue / 2);
imshow("【<5>双边滤波】", g_dstImage5);
}
(1)原图与方框滤波对比
(2)原图与均值滤波对比
(3)原图与高斯滤波对比
(4)原图与中值滤波对比
(5)原图与双边滤波对比
综上:观察发现:方框滤波和均值滤波效果相似,中值滤波对原图颠覆较大,双边滤波几乎和原图没有差别。
边栏推荐
- 分卷压缩,解压
- There are spaces in the for loop variable in the shell -- IFS variable
- Makefile simple induction
- 5g/4g pole gateway_ Smart pole gateway
- How to solve MySQL master-slave delay problem
- Laravel artisan common commands
- How to turn off debug information in rtl8189fs
- Sword finger offer 62 The last remaining number in the circle
- 2022 Q2 - Summary of skills to improve skills
- MySQL constraints and multi table query example analysis
猜你喜欢

What are the skills of spot gold analysis?

花一个星期时间呕心沥血整理出高频软件测试/自动化测试面试题和答案

New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
![[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术](/img/2d/299fa5c76416f74bd1a693c433dd09.png)
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术

Implementation of Weibo system based on SSM

MySQL constraints and multi table query example analysis

This is the form of the K-line diagram (pithy formula)

Redis环境搭建和使用的方法
![[graduation season] graduate seniors share how to make undergraduate more meaningful](/img/03/9adc44476e87b2499aa0ebb11cb247.png)
[graduation season] graduate seniors share how to make undergraduate more meaningful

医药管理系统(大一下C语言课设)
随机推荐
Laravel artisan common commands
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
Five skills of adding audio codec to embedded system
What is AQS and its principle
The concept, function, characteristics, creation and deletion of MySQL constraints
JMeter (II) - install the custom thread groups plug-in
剑指 Offer 47. 礼物的最大价值
How to execute an SQL in MySQL
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
Deep learning: a solution to over fitting in deep neural networks
Construction and maintenance of business websites [12]
Opencascade7.6 compilation
1069. Division of convex polygons (thinking, interval DP)
MySQL主从延迟问题怎么解决
Construction and maintenance of business websites [15]
Automatically browse pinduoduo products
Should enterprises choose server free computing?
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
An analysis of circuit for quick understanding
Learn basic K-line diagram knowledge in three minutes