当前位置:网站首页>【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)原图与双边滤波对比
综上:观察发现:方框滤波和均值滤波效果相似,中值滤波对原图颠覆较大,双边滤波几乎和原图没有差别。
边栏推荐
- OpenCASCADE7.6编译
- How to use redis ordered collection
- 2022 Q2 - 提升技能的技巧总结
- Openssl3.0 learning XXI provider encoder
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- 【LeetCode 43】236. The nearest common ancestor of binary tree
- Open that kind of construction document
- Iterative unified writing method of binary tree
- How to use a product to promote "brand thrill"?
- 1218 square or round
猜你喜欢

1222. Password dropping (interval DP, bracket matching)

golang---锁

The concept, function, characteristics, creation and deletion of MySQL constraints

PR second training

Redis有序集合如何使用

Data analysis on the disaster of Titanic
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

Cross domain? Homology? Understand what is cross domain at once

A quick understanding of digital electricity

MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
随机推荐
Matlab uses resample to complete resampling
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
flutter 中間一個元素,最右邊一個元素
Four basic strategies for migrating cloud computing workloads
An analysis of circuit for quick understanding
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
Openssl3.0 learning XXI provider encoder
479. Additive binary tree (interval DP on the tree)
【毕业季】研究生学长分享怎样让本科更有意义
A quick understanding of digital electricity
1217 supermarket coin processor
CSDN article underlined, font color changed, picture centered, 1 second to understand
Data analysis on the disaster of Titanic
Open that kind of construction document
Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
Deep learning: a solution to over fitting in deep neural networks
What style of Bluetooth headset is easy to use? High quality Bluetooth headset ranking
Sword finger offer 47 Maximum value of gifts
How to build and use redis environment