1. 程式人生 > >openCV學習筆記(二十) —— 影象濾波 —— 線性濾波(方框濾波、均值濾波、高斯濾波)

openCV學習筆記(二十) —— 影象濾波 —— 線性濾波(方框濾波、均值濾波、高斯濾波)

影象濾波簡介

方框濾波——boxFilter() 

原理

方框濾波程式 

#include<opencv2/opencv.hpp>
#include <vector>
#include <time.h>

using namespace std;
using namespace cv;

#define BOX_FILTER_ORIGINAL_WINDOW_NAME	"方框濾波【原圖】"
#define BOX_FILTER_RESULT_WINDOW_NAME	"方框濾波【效果圖】"

int main()
{
	//載入原圖
	Mat image = imread("test.jpg");

	//建立視窗
	namedWindow(BOX_FILTER_ORIGINAL_WINDOW_NAME);
	namedWindow(BOX_FILTER_RESULT_WINDOW_NAME);

	//顯示原圖
	imshow(BOX_FILTER_ORIGINAL_WINDOW_NAME, image);

	//進行濾波操作
	Mat out;
	boxFilter(image, out, -1, Size(5, 5));

	//顯示效果圖
	imshow(BOX_FILTER_RESULT_WINDOW_NAME, out);

	waitKey(0);

	return 0;
}

均值濾波 —— blur()

原理

 

 均值濾波程式

#include<opencv2/opencv.hpp>
#include <vector>
#include <time.h>

using namespace std;
using namespace cv;

#define BOX_FILTER_ORIGINAL_WINDOW_NAME	"均值濾波【原圖】"
#define BOX_FILTER_RESULT_WINDOW_NAME	"均值濾波【效果圖】"

int main()
{
	//載入原圖
	Mat image = imread("test.jpg");

	//建立視窗
	namedWindow(BOX_FILTER_ORIGINAL_WINDOW_NAME);
	namedWindow(BOX_FILTER_RESULT_WINDOW_NAME);

	//顯示原圖
	imshow(BOX_FILTER_ORIGINAL_WINDOW_NAME, image);

	//進行濾波操作
	Mat out;
	blur(image, out, Size(5, 5));

	//顯示效果圖
	imshow(BOX_FILTER_RESULT_WINDOW_NAME, out);

	waitKey(0);

	return 0;
}

高斯濾波——GaussianBlur()

原理

 

高斯濾波程式 

#include<opencv2/opencv.hpp>
#include <vector>
#include <time.h>

using namespace std;
using namespace cv;

#define BOX_FILTER_ORIGINAL_WINDOW_NAME	"高斯濾波【原圖】"
#define BOX_FILTER_RESULT_WINDOW_NAME	"高斯濾波【效果圖】"

int main()
{
	//載入原圖
	Mat image = imread("test.jpg");

	//建立視窗
	namedWindow(BOX_FILTER_ORIGINAL_WINDOW_NAME);
	namedWindow(BOX_FILTER_RESULT_WINDOW_NAME);

	//顯示原圖
	imshow(BOX_FILTER_ORIGINAL_WINDOW_NAME, image);

	//進行濾波操作
	Mat out;
	GaussianBlur(image, out, Size(5, 5), 0, 0);

	//顯示效果圖
	imshow(BOX_FILTER_RESULT_WINDOW_NAME, out);

	waitKey(0);

	return 0;
}

影象線性濾波綜合示例程式

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

#define BOX_FILTER_WINDOW	"方框濾波"
#define MEAN_BLUR_WINDOW	"均值濾波"
#define GAUSSIAN_BLUR_WINDOW	"高斯濾波"

Mat g_srcImage, g_dstImage1, g_dstImage2, g_dstImage3;	//儲存圖片的Mat型別
int g_nBoxFilterValue = 3;	//方框濾波引數值
int g_nMeanBlurValue = 3;	//均值濾波引數值
int g_nGaussianBlurValue = 3;	//高斯濾波引數值

//軌跡條的回撥函式
static void on_BoxFilter(int, void *);	//方框濾波
static void on_MeanBlur(int, void *);	//均值濾波
static void on_GaussianBlur(int, void *);	//高斯濾波

int main()
{
	//改變console字型顏色
	system("color5E");

	//載入原圖
	g_srcImage = imread("test.jpg");
	if (!g_srcImage.data)
	{
		printf("讀取srcImage錯誤!\n");
		return -1;
	}

	//複製原圖到三個Mat型別中
	g_dstImage1 = g_srcImage.clone();
	g_dstImage2 = g_srcImage.clone();
	g_dstImage3 = g_srcImage.clone();

	//顯示原圖
	imshow("原圖視窗", g_srcImage);

	//1.方框濾波
	//建立視窗
	namedWindow(BOX_FILTER_WINDOW);
	//建立軌跡條
	createTrackbar("核心值:", BOX_FILTER_WINDOW, &g_nBoxFilterValue, 40, on_BoxFilter);
	on_BoxFilter(g_nBoxFilterValue, 0);
	imshow(BOX_FILTER_WINDOW, g_dstImage1);

	//2.均值濾波
	//建立視窗
	namedWindow(MEAN_BLUR_WINDOW);
	//建立軌跡條
	createTrackbar("核心值:", MEAN_BLUR_WINDOW, &g_nMeanBlurValue, 40, on_MeanBlur);
	on_MeanBlur(g_nMeanBlurValue, 0);

	//3.高斯濾波
	//建立視窗
	namedWindow(GAUSSIAN_BLUR_WINDOW);
	createTrackbar("核心值:", GAUSSIAN_BLUR_WINDOW, &g_nGaussianBlurValue, 40, on_GaussianBlur);
	on_GaussianBlur(g_nGaussianBlurValue, 0);

	//輸出一些幫助資訊
	cout << endl << "\t請調整滾動條觀察影象效果\n"
		<< "\t按下“q”鍵時,程式退出!\n";

	//按下"q"鍵時,程式退出
	while (char(waitKey(1)) != 'q');
	destroyAllWindows();

	return 0;
}

static void on_BoxFilter(int, void *)
{
	boxFilter(g_srcImage, g_dstImage1, -1, Size(g_nBoxFilterValue + 1, g_nBoxFilterValue + 1));
	imshow("方框濾波", g_dstImage1);
}

static void on_MeanBlur(int, void *)
{
	blur(g_srcImage, g_dstImage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1), Point(-1, -1));
	imshow("均值濾波", g_dstImage2);
}

static void on_GaussianBlur(int, void *)
{
	GaussianBlur(g_srcImage, g_dstImage3, Size(g_nGaussianBlurValue*2 + 1, g_nGaussianBlurValue*2 + 1), 0, 0);
	imshow("高斯濾波", g_dstImage3);
}