OpenCV-学习历程9- 图像模糊2(中值滤波+双边滤波)

OPENCV系列博客主要记录自己学习OPENCV的历程,以及存储已经实现的代码,以备后续回顾使用,代码中包含了主要的备注。

 一.中值和均值滤波原理 (消除椒盐噪声)

        1.中值滤波:对椒盐噪声有比较好的抑制作用(因为椒盐噪声的灰度相比其他的像素点,差别非常大)

        2.算法概述: 用一个3x3的mask,依次遍历图像中每个像素点;

                              将这个中心像素点和周围共9个像素点强度进行排序,将这9个像素点的中值,放在当前中心像素点位置

        3. Tips:  均值滤波/最小值滤波/最大值滤波与中值滤波原理相似,基本上都是当前的像素与周围像素比较,然后用比较得到的值,替换当前中心像素。

                                     

. 双边滤波原理 (避免了像素的确实,并能确保边缘保存) (可以被用在保留边缘磨皮美颜操作)

                                                      

                                                              

. 相关API (中值模糊+双边模糊)

. 代码实现

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


using namespace std;
using namespace cv;


int main(int argc, char** argv) {

	//Step1 读取图片
	Mat src, dst;
	src = imread("E:/OpenCVLearning/Project/source_image/sample.jpg"); //注意斜线方向
	if (!src.data) {
		cout << "Could not load the image ...." << endl;
		return -1;
	}

	//Step2 自定义输入输出窗口
	char input_title[] = "input image";
	char output_title1[] = "Mid Filter image";
	char output_title2[] = "Bila filter image";
	namedWindow(input_title, CV_WINDOW_AUTOSIZE);
	namedWindow(output_title1, CV_WINDOW_AUTOSIZE);
	namedWindow(output_title2, CV_WINDOW_AUTOSIZE);
	imshow(input_title, src);

	//Step3 测试中值滤波!!! (去除椒盐噪声)

	medianBlur(src, dst, 3);   //3x3 mask
	imshow(output_title1, dst);


	//Step3 测试双边滤波!!! (保留边缘平顺)

	Mat bila_image;
	bilateralFilter(src, bila_image, 15,100,5); //
	imshow(output_title2, bila_image);

	//Extra comparison 比较高斯模糊效果
	Mat gauss_test;
	GaussianBlur(src, gauss_test,Size(15,15),3,3);
	imshow("Gauss Compare", gauss_test);


	waitKey(0);
	return 0;
}

五.效果

(高斯滤波过于模糊,双边滤波可以很好得保持边缘,轮廓)

(双边滤波后的结果可以使用增强对比度,提升效果)

猜你喜欢

转载自blog.csdn.net/weixin_42503785/article/details/113925168