当前位置:网站首页>[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 .
边栏推荐
- No programming code technology! Four step easy flower store applet
- JPM 2021 most popular paper released (with download)
- [question] - why is optical flow not good for static scenes
- The middle element and the rightmost element of the shutter
- 剑指 Offer 31. 栈的压入、弹出序列
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- Sword finger offer 29 Print matrix clockwise
- 321. Chessboard segmentation (2D interval DP)
- leetcode373. 查找和最小的 K 对数字(中等)
- 研发中台拆分过程的一些心得总结
猜你喜欢

What is the MySQL column to row function

附加:信息脱敏;

Data analysis on the disaster of Titanic

MySQL主从延迟问题怎么解决

Number of palindromes in C language (leetcode)

What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
![[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

321. Chessboard segmentation (2D interval DP)

Five skills of adding audio codec to embedded system

Software development life cycle -- waterfall model
随机推荐
MySQL如何解决delete大量数据后空间不释放的问题
Pytest testing framework
A quick understanding of analog electricity
剑指 Offer 31. 栈的压入、弹出序列
Selection of field types for creating tables in MySQL database
STM32F103 - two circuit PWM control motor
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
How to turn off debug information in rtl8189fs
MySQL约束与多表查询实例分析
leetcode373. 查找和最小的 K 对数字(中等)
How to batch add background and transition effects to videos?
How to execute an SQL in MySQL
Calculation (computer) code of suffix expression
Regular expression learning notes
leetcode2312. 卖木头块(困难,周赛)
软件开发生命周期 --瀑布模型
Start from scratch - Web Host - 01
SQL server calculates the daily average and annual average of the whole province
An analysis of circuit for quick understanding
1222. Password dropping (interval DP, bracket matching)