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

#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.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("读取的图片不存在…… \n");
        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”键时,程序退出……\n";
    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);
}

posted @ 2016-05-06 12:54  张飞online  阅读(1342)  评论(0编辑  收藏  举报