当前位置:网站首页>[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 .
边栏推荐
- How to build and use redis environment
- essay structure
- * and & symbols in C language
- The concept, function, characteristics, creation and deletion of MySQL constraints
- 软件开发生命周期 --瀑布模型
- The middle element and the rightmost element of the shutter
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
- Sword finger offer 31 Stack push in and pop-up sequence
- 【深度学习】infomap 人脸聚类 facecluster
- Construction and maintenance of business websites [11]
猜你喜欢

开发工具创新升级,鲲鹏推进计算产业“竹林”式生长

Leetcode face T10 (1-9) array, ByteDance interview sharing

leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)

How to turn off debug information in rtl8189fs

Medical management system (C language course for freshmen)

No programming code technology! Four step easy flower store applet

Webgpu (I): basic concepts

leetcode2309. The best English letters with both upper and lower case (simple, weekly)

leetcode2312. 卖木头块(困难,周赛)

How to use redis ordered collection
随机推荐
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
Sword finger offer 31 Stack push in and pop-up sequence
Volume compression, decompression
如何远程、在线调试app?
大厂裁员潮不断,双非本科出身的我却逆风翻盘挺进阿里
Using mongodb in laravel
Redis环境搭建和使用的方法
There are spaces in the for loop variable in the shell -- IFS variable
开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
【LeetCode 43】236. The nearest common ancestor of binary tree
golang---锁
1069. Division of convex polygons (thinking, interval DP)
软件开发生命周期 --瀑布模型
Software No.1
How to turn off debug information in rtl8189fs
leetcode2305. Fair distribution of biscuits (medium, weekly, shaped pressure DP)
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
essay structure
Open那啥的搭建文档