当前位置:网站首页>[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 .
边栏推荐
- 剑指 Offer 62. 圆圈中最后剩下的数字
- new和malloc的区别
- The middle element and the rightmost element of the shutter
- Openssl3.0 learning XXI provider encoder
- Based on configured schedule, the given trigger will never fire
- Architecture evolution from MVC to DDD
- Sword finger offer 29 Print matrix clockwise
- Calculation (computer) code of suffix expression
- Design and implementation of key value storage engine based on LSM tree
- Construction and maintenance of business websites [11]
猜你喜欢

RTL8189FS如何关闭Debug信息

Golang lock

Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing

leetcode2309. 兼具大小写的最好英文字母(简单,周赛)

MySQL operates the database through the CMD command line, and the image cannot be found during the real machine debugging of fluent

leetcode2310. The one digit number is the sum of integers of K (medium, weekly)

跨域?同源?一次搞懂什么是跨域

CSDN article underlined, font color changed, picture centered, 1 second to understand

【毕业季】研究生学长分享怎样让本科更有意义

Word search applet design report based on cloud development +ppt+ project source code + demonstration video
随机推荐
大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
essay structure
The middle element and the rightmost element of the shutter
研发中台拆分过程的一些心得总结
How to build and use redis environment
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
Which is a good Bluetooth headset of about 300? 2022 high cost performance Bluetooth headset inventory
Five skills of adding audio codec to embedded system
CSDN insertion directory in 1 second
Software development life cycle -- waterfall model
Design and implementation of key value storage engine based on LSM tree
【C#】使用正则校验内容
JPM 2021 most popular paper released (with download)
Makefile simple induction
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
Sword finger offer 29 Print matrix clockwise
The concept, function, characteristics, creation and deletion of MySQL constraints
【深度学习】infomap 人脸聚类 facecluster
How to batch add background and transition effects to videos?
Construction and maintenance of business websites [12]