当前位置:网站首页>【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)原图与双边滤波对比
综上:观察发现:方框滤波和均值滤波效果相似,中值滤波对原图颠覆较大,双边滤波几乎和原图没有差别。
边栏推荐
- Electronic Association C language level 1 33, odd even number judgment
- Five skills of adding audio codec to embedded system
- Four basic strategies for migrating cloud computing workloads
- Sword finger offer II 031 Least recently used cache
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- STM32F103——两路PWM控制电机
- Selection of field types for creating tables in MySQL database
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- 【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
- flutter 中間一個元素,最右邊一個元素
猜你喜欢
What are the skills of spot gold analysis?
MySQL如何解决delete大量数据后空间不释放的问题
Discussion on the idea of platform construction
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
Architecture evolution from MVC to DDD
MySQL约束与多表查询实例分析
How to debug apps remotely and online?
Opengauss database backup and recovery guide
A quick understanding of digital electricity
随机推荐
AR增强现实可应用的场景
Ubuntu20.04 PostgreSQL 14 installation configuration record
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
牛客网——华为题库(51~60)
Ar Augmented Reality applicable scenarios
分卷压缩,解压
Ks006 student achievement management system based on SSM
城市选择器组件实现原理
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
golang---锁
Failed to transform file 'xxx' to match attributes
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
Laravel artisan common commands
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Opengauss database backup and recovery guide
Architecture evolution from MVC to DDD
Matlab uses resample to complete resampling
电子协会 C语言 1级 33 、奇偶数判断
Redis环境搭建和使用的方法
Sword finger offer II 031 Least recently used cache