欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

OpenCV双边滤波测试

程序员文章站 2022-07-14 23:34:46
...

OpenCV双边滤波测试

测试代码

#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

#define dbg(fmt,...) do{printf("[DEBUG %s:%d] " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)
#define info(fmt,...) do{printf("[INFO  %s:%d] " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)
#define warn(fmt,...) do{printf("[WARN  %s:%d] " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)
#define err(fmt,...) do{printf("[ERROR %s:%d] " fmt "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0)


#define IMG_FILE  "001.png"

static void _TrackbarCallback (int pos, void* userdata)
{
    int* bChanged = (int*)userdata;
    *bChanged = 1;
}

int BilateralFilterTest()
{
    Mat img0, img;
    const char* winName = "BilateralFilterTest";
    const char* tbNameD = "d";
    const char* tbNameSigmaColor = "sigmaColor";
    const char* tbNameSigmaSpace = "sigmaSpace";
    int tbValueD = 3;
    int tbValueSigmaColor = 0;
    int tbValueSigmaSpace = 0;
    int tbChanged = 1;

    img0 = imread(IMG_FILE);
    if (img0.empty()) {
        err("imread %s failed", IMG_FILE);
        goto _exit;
    }
    imshow("原图", img0);

    namedWindow(winName);
    createTrackbar(tbNameD, winName, &tbValueD, 30, _TrackbarCallback, &tbChanged);
    createTrackbar(tbNameSigmaColor, winName, &tbValueSigmaColor, 1000, _TrackbarCallback, &tbChanged);
    createTrackbar(tbNameSigmaSpace, winName, &tbValueSigmaSpace, 1000, _TrackbarCallback, &tbChanged);

    img = img0.clone();
    imshow(winName, img);

    while (1) {
        if (tbChanged) {
            int d = tbValueD / 2 * 2 + 1;
            double sigmaColor = tbValueSigmaColor / 10.0;
            double sigmaSpace = tbValueSigmaSpace / 10.0;
            dbg("bilateralFilter d:%d sigmaColor:%f sigmaSpace:%f", d, sigmaColor, sigmaSpace);
            bilateralFilter(img0, img, d, sigmaColor, sigmaSpace);
            imshow(winName, img);
            tbChanged = 0;
        }
        if (waitKey(100) > 0)
            break;
    }

_exit:
    destroyAllWindows();
    dbg("例程退出");
    getchar();
    return 0;
}

运行效果

OpenCV双边滤波测试

相关标签: 双边滤波