OpenCV 模糊图像的简单方法



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


using namespace std;
using namespace cv;

// 模糊处理
// Smooth/Blur 是图像处理中最简单的常用操作
// 用于预处理时候的降噪
// 背后原理是数学的卷积计算
// blur(src, dst, Size(x_radius, y_radius), Point(-1, -1));均值滤波
// GaussianBlur(src, dst, Size(11, 11), sigmax, sigmay); 高斯模糊 基于正态分布掩膜来模糊, 所以会比均值滤波更有对比度一点点
// 必须用奇数掩膜

// 一个有趣的小操作 可以把Size(x, y)设置成单一方向来达到水平或者垂直方向的模糊。
// 中值滤波 针对椒盐噪声有很好的抑制效果
// medianBlur(src, dst, ksize)

// 最小值最大值滤波


// 高斯双边模糊---是边缘保留的滤波方法,避免了边缘信息丢失,保留了图像轮廓不变
// bilateralFilter(src, dst, ksize, sigmaColor, sigmaSpace);
/*
src – Source 8-bit or floating-point, 1-channel or 3-channel image.
dst – Destination image of the same size and type as src .
d – Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace .
sigmaColor – Filter sigma in the color space. A larger value of the parameter means that farther colors within the pixel neighborhood (see sigmaSpace ) will be mixed together, resulting in larger areas of semi-equal color.
sigmaSpace – Filter sigma in the coordinate space. A larger value of the parameter means that farther pixels will influence each other as long as their colors are close enough (see sigmaColor ). When d>0 , it specifies the neighborhood size regardless of sigmaSpace . Otherwise, d is proportional to sigmaSpace .
*/
// 简单说,ksize为掩膜大小;sigmaColor越大,参与模糊的色彩范围就越大,即对比对降低;sigmaSpace越大,意味着更多像素点来互相影响。
// 但是sigmaSpace和ksize相互作用,ksize > 0,sigmaSpace不起作用;ksize < 0,z 则由sigmaSpace来生成ksize。




int main() {
    Mat src1, src2, gray_src, dst;
    src1 = imread("/Users/apple/Desktop/birth.jpg", IMREAD_COLOR);
    //src2 = imread("/Users/apple/Desktop/test2.jpg", IMREAD_COLOR);
    if (src1.empty()) { // if (!src.data())
        cout << "could not load image..." << endl;
        return -1;
    }
    imshow("input", src1);
    
    //blur(src1, dst, Size(15, 15), Point(-1, -1));
    //imshow("blur_output", dst);
    
    GaussianBlur(src1, dst, Size(5, 5), 11);
    //imshow("gaussianblur_output", dst);
    
    //medianBlur(src1, dst, 15);
    //imshow("medianBlur", dst);
    
    bilateralFilter(src1, dst, 3, 30, 100);
    //imshow("bilateral", dst);
    
    Mat kernel = (Mat_<int>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    filter2D(dst, dst, -1, kernel);
    imshow("Final result", dst);
    
    waitKey(0);
    
    return 0;
}



猜你喜欢

转载自blog.csdn.net/ringggr_h/article/details/79985279