OpenCv图像处理之均值滤波-线性滤波(二)

OpenCv图像处理之均值滤波-线性滤波


上一篇博文讲解了均值滤波的一般形式-方框滤波,这次我们来说下什么是均值滤波。
均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板(邻域),该模板包括了其 周围的临近像素(以目标像素为中心的周围8个像素,构成一个滤波模板,即包括目标像素本身),再用模板中的全体像素的平均值来代替原来像素值。均值滤波是一个低通滤波器(通低频,阻高频)
低频指的是目标像素和周边像素相差不大。
高频指的是目标像素和周边像素相差较大。
该滤波器的 优点在于效率高,思路简单。 缺点是计算均值会将图像中的边缘信息以及特征信息模糊掉,会丢失很多特征。而且只能微弱的减弱噪声,而无法完全去掉噪声。
均值滤波对 高斯噪声的抑制较好,处理后的图像边缘模糊较少,但对 椒盐噪声的影响不大,这是因为在消弱噪声的同时图像总体的特征也变得模糊,其噪声仍然存在。

看一下源码中对cv::blur的声明和描述

CV_EXPORTS_W void blur(InputArray src, OutputArray dst,Size ksize, Point anchor=Point(-1, -1),int borderType=BORDER_DEFAULT);

//blur和归一化下的方框滤波效果是一样
The call `blur(src, dst, ksize, anchor, borderType)` is equivalent to `boxFilter(src, dst, src.type(), ksize,
anchor, true, borderType)`.
//输入矩阵可以有任意的通道数,各通道单独处理,但是深度必须是CV_8U,CV_16U,CV_16S,CV_32F,CV_64F这些类型中的一种
@param src input image; it can have any number of channels, which are processed independently, but
the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
//输出矩阵要和src拥有相同的尺寸和类型
@param dst output image of the same size and type as src.
//滤波核的尺寸,值越大越模糊
@param ksize blurring kernel size.
//目标点位于核的中心点
@param anchor anchor point; default value Point(-1,-1) means that the anchor is at the kernel
center.
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes. #BORDER_WRAP is not supported.
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    
    

    Mat img, clone_img;
    img = imread("D:/cat.jpg", IMREAD_COLOR);
    if (img.empty()) {
    
    
        return -1;
    }
    clone_img = img.clone();
    double scale = 0.5;
    int width = int(clone_img.cols * scale);
    int height = int(clone_img.rows * scale);
    resize(clone_img, clone_img, Size(width, height), 0, 0, INTER_AREA);
    Mat dst = Mat::zeros(Size(width, height), clone_img.type());
    blur(clone_img, dst, Size(5, 5), Point(-1, -1));
    imshow("clone_img", clone_img);
    imshow("dst", dst);
    waitKey(0);
    return 0;
}

效果显示

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_35140742/article/details/120061747