• 10. 对一幅灰度图像进行均值滤波、中值滤波。(自己可适当对原图像增加高频噪声)


    #include <iostream>
    #include <opencv2corecore.hpp>
    #include <opencv2highguihighgui.hpp>
    #include <opencv2imgprocimgproc.hpp>
    using namespace std;
    using namespace cv;
    Mat g_srcImage;     // 全局的源图像
    // 分别对应全局的方框滤波、均值滤波、高斯滤波、中值滤波、双边滤波的输出图像以及内核值/参数值
    Mat g_dstImgBox, g_dstImgBlur, g_dstImgGaussian, g_dstImgMedian, g_dstImgBilateral;
    int g_BoxFilterVal = 5;
    int g_BlurVal = 12;
    int g_GaussianBlurVal = 5;
    int g_MedianBlurVal = 12;
    int g_BilateralFilterVal = 12;
    static void on_BoxFilter(int, void *);
    static void on_Blur(int, void *);
    static void on_GaussianBlur(int, void *);
    static void on_MedianBlur(int, void *);
    static void on_BilateralFilter(int, void*);
    int main()
    {
        // 读取图像到g_srcImage
        g_srcImage = imread("6013202130.jpg");
        if (!g_srcImage.data) {
            printf("读取的图片不存在…… 
    ");
            return false;
        }
    
        // 分别克隆原图到5中滤波所需的图像中,均为Mat类型
        g_dstImgBox = g_srcImage.clone();
        g_dstImgBlur = g_srcImage.clone();
        g_dstImgGaussian = g_srcImage.clone();
        g_dstImgMedian = g_srcImage.clone();
        g_dstImgBilateral = g_srcImage.clone();
    
        // 显示原图
        namedWindow("【原图】", 1);
        imshow("【原图】", g_srcImage);
        namedWindow("【均值滤波】", 1);
        createTrackbar("内核值", "【均值滤波】", &g_BlurVal, 30, on_Blur);
        on_Blur(g_BlurVal, 0);
        namedWindow("【高斯滤波】", 1);
        createTrackbar("内核值", "【高斯滤波】", &g_GaussianBlurVal, 30, on_GaussianBlur);
        on_GaussianBlur(g_GaussianBlurVal, 0);
        namedWindow("【中值滤波】", 1);
        createTrackbar("内核值", "【中值滤波】", &g_MedianBlurVal, 30, on_MedianBlur);
        on_MedianBlur(g_MedianBlurVal, 0);
        cout << "按下“q”键时,程序退出……
    ";
        while (char(waitKey(1)) != 'q') {}
        return 0;
    }
    
    static void on_Blur(int, void *)
    {
        blur(g_srcImage, g_dstImgBlur, Size(g_BlurVal + 1, g_BlurVal + 1),
            Point(-1, -1));
        imshow("【均值滤波】", g_dstImgBlur);
    }
    
    static void on_GaussianBlur(int, void *)
    {
        GaussianBlur(g_srcImage, g_dstImgGaussian, Size(g_GaussianBlurVal * 2 + 1,
            g_GaussianBlurVal * 2 + 1), 0, 0);
        imshow("【高斯滤波】", g_dstImgGaussian);
    }
    
    static void on_MedianBlur(int, void *)
    {
        medianBlur(g_srcImage, g_dstImgMedian, g_MedianBlurVal * 2 + 1);
        imshow("【中值滤波】", g_dstImgMedian);
    }

  • 相关阅读:
    BZOJ1877: [SDOI2009]晨跑
    SPFA的两个优化:SLF与LLL
    BZOJ1858: [Scoi2010]序列操作
    java线程基础巩固---如何捕获线程运行期间的异常
    java线程基础巩固---如何给你的应用程序注入钩子程序
    类的命名空间与卸载详解及jvisualvm使用
    okhttp拦截器之RetryAndFollowUpInterceptor&BridgeInterceptor分析
    okhttp初识拦截器
    类加载器双亲委托机制实例深度剖析
    类加载器重要方法详解
  • 原文地址:https://www.cnblogs.com/zhangfeionline/p/5465217.html
Copyright © 2020-2023  润新知