opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()

概念介绍

opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第1张图片
opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第2张图片
opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第3张图片
opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第4张图片
opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第5张图片

1,中值滤波函数medianBlur()

opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第6张图片

2,双边滤波函数bilateralFilter()

opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第7张图片
opencv学习(十六)非线性滤波//中值滤波Medianblur()//双边滤波biteralfilter()_第8张图片

实例

#include
using namespace std;
using namespace cv;

//全局变量声明
Mat g_srcimage, g_dstimage1, g_dstimage2, g_dstimage3, g_dstimage4, g_dstimage5;
int g_nBoxFliterValue = 3;//方框,均值,高斯滤波参数值
int g_nMeanBlurValue = 3;
int g_nGaussianBlurValue = 3;
int g_nMedianBlurValue = 3;
int g_nBeliteralValue = 3;


//轨迹条的回调函数(静态函数,只适用于本文件)
static void on_BoxFliter(int, void*);
static void on_MeanBlur(int, void*);
static void on_GaussianBlur(int, void*);
static void on_Medianblur(int, void*);
static void on_Bilateral(int, void*);

//主函数
int main()
{
    system("color 5E");
    g_srcimage = imread("D://1.jpg", 1);
    if (!g_srcimage.data) { printf("错错错\n"); return false; }

    g_dstimage1 = g_srcimage.clone();
    g_dstimage2 = g_srcimage.clone();
    g_dstimage3 = g_srcimage.clone();
    g_dstimage4 = g_srcimage.clone();
    g_dstimage5 = g_srcimage.clone();

    //显示原图
    namedWindow("原图窗口", 1);
    imshow("原图窗口", g_srcimage);

    //1方框滤波
    namedWindow("方框滤波", 1);
    createTrackbar("内核值:","方框滤波",&g_nBoxFliterValue,40,on_BoxFliter);
    on_BoxFliter(g_nBoxFliterValue, 0);


    //2均值滤波
    namedWindow("均值滤波", 1);
    createTrackbar("内核值:", "均值滤波", &g_nMeanBlurValue, 40, on_MeanBlur);
    on_MeanBlur(g_nMeanBlurValue, 0);


    //3高斯滤波
    namedWindow("高斯滤波", 1);
    createTrackbar("内核值:", "高斯滤波", &g_nGaussianBlurValue, 40, on_GaussianBlur);
    on_GaussianBlur(g_nGaussianBlurValue, 0);

    //4中值滤波
    namedWindow("中值滤波", 1);
    createTrackbar("参数值:", "中值滤波", &g_nMedianBlurValue ,40, on_Medianblur);
    on_Medianblur(g_nMedianBlurValue, 0);

    //5双边滤波
    namedWindow("双边滤波", 1);
    createTrackbar("参数值:", "双边滤波", &g_nBeliteralValue, 40, on_Bilateral);
    on_Bilateral(g_nBeliteralValue, 0);



    //输出提示信息
    cout << endl << "滑动滚动条观察图像效果\n\n"
        << "按下q键退出";
    while (char(waitKey(1)) != 'q') {}
    return 0;



}
//BoxFliter,blur,GassianBlur函数定义
static void on_BoxFliter(int, void*)
{
    boxFilter(g_srcimage, g_dstimage1, -1, Size(g_nBoxFliterValue + 1, g_nBoxFliterValue + 1));
    imshow("方框滤波", g_dstimage1);//与上面的2,3,对比发现,只有将imshow函数置于此才能拖动滑动条改变内核值(2,3默认展示了滤波内核的初始值,无法随滑动条改变而改变)
}
static void on_MeanBlur(int, void*)
{
    blur(g_srcimage, g_dstimage2, Size(g_nMeanBlurValue + 1, g_nMeanBlurValue + 1),Point(-1,-1));
    imshow("均值滤波", g_dstimage2);
}
static void on_GaussianBlur(int, void*)
{
    GaussianBlur(g_srcimage, g_dstimage3, Size(g_nGaussianBlurValue*2 + 1, g_nGaussianBlurValue*2+ 1),0,0);
    imshow("高斯滤波", g_dstimage3);
}
static void on_Medianblur(int, void*)
{
    medianBlur(g_srcimage, g_dstimage4, g_nMedianBlurValue * 2 + 1);
    imshow("中值滤波", g_dstimage4);
}
static void on_Bilateral(int, void*)
{
    bilateralFilter(g_srcimage, g_dstimage5, g_nBeliteralValue, g_nBeliteralValue*2, g_nBeliteralValue/2);
    imshow("双边滤波", g_dstimage5);
}

你可能感兴趣的:(opencv学习)