当前位置:网站首页>[opencv] - comprehensive examples of five image filters
[opencv] - comprehensive examples of five image filters
2022-07-02 02:12:00 【I love to learn food】
preface : This article is about the previous study Linear filtering and Nonlinear filtering A review of . Before that, we introduced the filter's processing of pictures in chapters , In this section, the knowledge points introduced above are carried by code , Let's show you , Use the sliding bar to control all kinds of filtering learned ( Box filtering 、 Mean filtering 、 Gauss filtering 、 median filtering 、 Bilateral filtering ) Parameter values for . Through the scroll bar to control the blur of the image under various smoothing processing , Not only can we see the effect by comparing with the original picture , And it also has certain playability , ha-ha .
Don't talk much , Code up
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
//---------------------------------
// Global variable declaration
//-------------------------------
Mat g_srcIamge, g_dstImage1, g_dstImage2, g_dstImage3,g_dstImage4, g_dstImage5;// Store pictures Mat type
int g_nBoxFilterValue = 3;// Block filter parameter value
int g_nMeanBlurValue = 3;// Mean filter parameter value
int g_nGaussianBlurValue = 3;// Gaussian filter parameter value
int g_nMedianBlurValue = 10;// Median filter parameter value
int g_nBilateralFilterValue = 10;// Bilateral filtering parameter value
//---------------------------------
// Global function declaration
//-------------------------------
static void on_BoxFilter(int, void *);// Box filtering
static void on_MeanBlur(int, void *);// Mean filtering
static void on_GaussianBlur(int, void *);// Gauss filtering
static void on_MedianBlur(int, void *);// Median filter
static void on_BilateralFilter(int, void *);// Bilateral filter
int main()
{
// change console The font color
system("color 5E");
// Load original
g_srcIamge = imread("E:\\Pec\\shihao.jpg", 1);
if (!g_srcIamge.data)
{
printf(" Error reading picture \n");
return false;
}
// Copy the original image to three Mat Type in the
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();
// Show the original
namedWindow("【<0> Original window 】", 1);
imshow("【<0> Original window 】", g_srcIamge);
//=========1、 Box filtering ===========
// create a window
namedWindow("【<1> Box filtering 】", 1);
// Create a trackbar
createTrackbar(" Kernel value :", "【<1> Box filtering 】", &g_nBoxFilterValue, 40, on_BoxFilter);
on_MeanBlur(g_nBoxFilterValue, 0);
//======2、 Mean filtering ===========
namedWindow("【<2> Mean filtering 】", 1);
// Create a trackbar
createTrackbar(" Kernel value :", "【<2> Mean filtering 】", &g_nMeanBlurValue, 40, on_BoxFilter);
on_MeanBlur(g_nMeanBlurValue, 0);
//=====3、 Gauss filtering ===============
namedWindow("【<3> Gauss filtering 】", 1);
// Create a trackbar
createTrackbar(" Kernel value :", "【<3> Gauss filtering 】", &g_nGaussianBlurValue, 40, on_BoxFilter);
on_GaussianBlur(g_nGaussianBlurValue, 0);
//=====4、 median filtering ===============
namedWindow("【<4> median filtering 】", 1);
// Create a trackbar
createTrackbar(" Kernel value :", "【<4> median filtering 】", &g_nMedianBlurValue, 50, on_MeanBlur);
on_MedianBlur(g_nMedianBlurValue, 0);
//=====5、 Bilateral filtering ===============
namedWindow("【<5> Bilateral filtering 】", 1);
// Create a trackbar
createTrackbar(" Kernel value :", "【<5> Bilateral filtering 】", &g_nBilateralFilterValue, 50, on_BilateralFilter);
on_BilateralFilter(g_nBilateralFilterValue, 0);
// Output some help information
cout << endl << " Please adjust the scroll bar to observe the image effect ~\n\n" << "\t Press down “q” Key time , Program exit ~\n";
// stay waitKey(1) Then enter a value
while (char(waitKey(1)) != 'q') {
}
return 0;
}
//----------------------
// Callback function of block filter operation
//---------------------------
static void on_BoxFilter(int, void *)// Box filtering
{
boxFilter(g_srcIamge, g_dstImage1, -1, Size(g_nBoxFilterValue + 1, g_nBoxFilterValue + 1));
imshow("【<1> Box filtering 】", g_dstImage1);
}
static void on_MeanBlur(int, void *)// Mean filtering
{
blur(g_srcIamge, g_dstImage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1), Point(-1, -1));
imshow("【<2> Mean filtering 】", g_dstImage2);
}
static void on_GaussianBlur(int, void *)// Gauss filtering
{
GaussianBlur(g_srcIamge, g_dstImage3,
Size(g_nGaussianBlurValue * 2 + 1, g_nGaussianBlurValue * 2 + 1), 0, 0);
imshow("【<3> Gauss filtering 】", g_dstImage3);
}
static void on_MedianBlur(int, void *)// Median filter
{
medianBlur(g_srcIamge, g_dstImage4, g_nMedianBlurValue * 2 + 1);
imshow("【<4> median filtering 】", g_dstImage4);
}
static void on_BilateralFilter(int, void *)// Bilateral filter
{
bilateralFilter(g_srcIamge, g_dstImage5, g_nBilateralFilterValue, g_nBilateralFilterValue * 2,
g_nBilateralFilterValue / 2);
imshow("【<5> Bilateral filtering 】", g_dstImage5);
}
(1) Compare the original image with the block filter
(2) Comparison between original image and mean filtering
(3) Comparison between original image and Gaussian filter
(4) Comparison between original image and median filter
(5) The original image is compared with bilateral filtering
Sum up : Observation found that : The effect of block filtering and mean filtering is similar , Median filtering has a great impact on the original image , Bilateral filtering is almost no different from the original image .
边栏推荐
- Ar Augmented Reality applicable scenarios
- Golang lock
- 软件开发生命周期 --瀑布模型
- 大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
- Sword finger offer 31 Stack push in and pop-up sequence
- 1069. Division of convex polygons (thinking, interval DP)
- leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
- An analysis of circuit for quick understanding
- How to use a product to promote "brand thrill"?
- 剑指 Offer 47. 礼物的最大价值
猜你喜欢
跨域?同源?一次搞懂什么是跨域
MySQL如何解决delete大量数据后空间不释放的问题
分卷压缩,解压
How to solve MySQL master-slave delay problem
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
5g/4g pole gateway_ Smart pole gateway
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
【LeetCode 43】236. The nearest common ancestor of binary tree
随机推荐
Openssl3.0 learning XXI provider encoder
mysql列转行函数指的是什么
Iterative unified writing method of binary tree
how to add one row in the dataframe?
Medical management system (C language course for freshmen)
leetcode2312. 卖木头块(困难,周赛)
2022 Q2 - résumé des compétences pour améliorer les compétences
Exception handling of class C in yyds dry goods inventory
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
What is the MySQL column to row function
An analysis of circuit for quick understanding
How to turn off debug information in rtl8189fs
Ar Augmented Reality applicable scenarios
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
There are spaces in the for loop variable in the shell -- IFS variable
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Based on configured schedule, the given trigger will never fire
flutter 中間一個元素,最右邊一個元素
Regular expression learning notes
剑指 Offer 47. 礼物的最大价值