当前位置:网站首页>[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 .
边栏推荐
- new和malloc的区别
- Infix expression to suffix expression (computer) code
- How does MySQL solve the problem of not releasing space after deleting a large amount of data
- Golang lock
- STM32F103 - two circuit PWM control motor
- 【LeetCode 43】236. The nearest common ancestor of binary tree
- Types of exhibition items available in the multimedia interactive exhibition hall
- leetcode2312. Selling wood blocks (difficult, weekly race)
- Webgpu (I): basic concepts
- 牛客网——华为题库(51~60)
猜你喜欢

leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)

An analysis of circuit for quick understanding

How to use a product to promote "brand thrill"?

JMeter (II) - install the custom thread groups plug-in

The smart Park "ZhongGuanCun No.1" subverts your understanding of the park

Golang lock

Word search applet design report based on cloud development +ppt+ project source code + demonstration video

If you want to rewind the video picture, what simple methods can you use?

From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years

SQLite 3 of embedded database
随机推荐
The concept, function, characteristics, creation and deletion of MySQL constraints
Data analysis on the disaster of Titanic
The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
Five skills of adding audio codec to embedded system
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
321. Chessboard segmentation (2D interval DP)
Post infiltration flow encryption
2022 Q2 - Summary of skills to improve skills
剑指 Offer 31. 栈的压入、弹出序列
trading
How to use a product to promote "brand thrill"?
Open那啥的搭建文档
SQLite 3 of embedded database
Construction and maintenance of business websites [15]
【OpenCV】-5种图像滤波的综合示例
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
Construction and maintenance of business websites [12]
Construction and maintenance of business websites [11]
What is the MySQL column to row function
As a software testing engineer, will you choose the bank post? Laolao bank test post