图像模糊:为了降低图像噪声。
常见的为Smooth, blur,它们利用卷积计算实现,这些卷积算子都是线性操作,所以又称为线性滤波。
均值滤波核的权重都为1,最终像素取个结果和的均值,最终导致图像各个区域的像素值比较均匀,无法克服边缘像素信息丢失。
高斯滤波核的权重是一个高斯分布的值,滤波后较大的像素值仍然较大,因此图像会保留原有的特点,但只是部分地克服了边缘信息的丢失,无法完全避免,因为没有考虑像素值的不同。
均值滤波(模糊):blur(src, dst, Size(5, 5));
高斯滤波(模糊):GaussianBlur(src, dst2, Size(5, 5), 11);
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
int main(){
Mat src, dst;
src = imread("/Users/ming/Documents/test.jpg");
if (!src.data){
printf("cannot load image ...");
return -1;
}
namedWindow("src img", CV_WINDOW_AUTOSIZE);
imshow("src img", src);
blur(src, dst, Size(5, 5)); //均值模糊
namedWindow("blur img", CV_WINDOW_AUTOSIZE);
imshow("blur img", dst);
Mat dst2;
GaussianBlur(src, dst2, Size(5, 5), 11); //高斯模糊
namedWindow("gaussianblur img", CV_WINDOW_AUTOSIZE);
imshow("gaussianblur img", dst2);
waitKey(0);
return 0;
}
中值滤波(模糊):是一种统计排序滤波器,因为在核对应的范围中,给中心点赋予核范围内中间数值,对椒盐噪点有很好点抑制作用。函数:medianBlur(src, dst, 5); //5为卷积核的大小
高斯双边滤波(模糊):是边缘保留的滤波方法,避免边缘像素信息丢失,保留了图像轮廓不变。可以用于美颜中。
扫描二维码关注公众号,回复:
3399481 查看本文章
bilateralFilter
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
int main(){
Mat src, dst;
src = imread("/Users/ming/Documents/test.jpg");
if (!src.data){
printf("cannot load image ...");
return -1;
}
namedWindow("src img", CV_WINDOW_AUTOSIZE);
imshow("src img", src);
medianBlur(src, dst, 5); //中值滤波
imshow("medianblur", dst);
Mat dst1;
GaussianBlur(src, dst1, Size(5,5), 11); //高斯滤波
imshow("gaussianblur", dst1);
Mat dst2;
bilateralFilter(src, dst2, 15, 150, 10); //高斯双边滤波。15表示计算区域的半径,150表示像素差值在150内才进行计算
imshow("bilateralFilter", dst2);
Mat dst2_1;
Mat kernel = (Mat_<int>(3,3) << 0,-1,0,-1,5,-1,0,-1,0);
filter2D(dst2, dst2_1, -1, kernel); //提高对比度
imshow("bilateralFilter + filter2D", dst2_1);
waitKey(0);
return 0;
}