均值模糊,高斯模糊,中值模糊,雙邊模糊


#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;


const int g_nTrackbarMaxValue = 9;     //定義軌跡條最大值
int g_nTrackbarValue;                   //定義軌跡條初始值
int g_nKernelValue;                     //定義kernel尺寸
Mat src, dst;

//均值濾波
void on_kernelTrackbar(int,void *)
{
    //根據輸入值重新計算kernel尺寸
    g_nKernelValue = g_nTrackbarValue * 2 + 1;

    //均值濾波函數
    blur(src, dst, Size(g_nKernelValue, g_nKernelValue));

    imshow("均值濾波", dst);
}

//高斯濾波
void on_kernelTrackbar2(int, void*)
{
    //根據輸入值重新計算kernel尺寸
    g_nKernelValue = g_nTrackbarValue * 2 + 1;

    //均值濾波函數
    GaussianBlur(src, dst, Size(g_nKernelValue, g_nKernelValue),11,11);

    imshow("均值濾波", dst);
}

int main()
{
    
    //原圖
    src = imread(".//pic//kate.png",IMREAD_UNCHANGED);
    
    if (!src.data)
    {
        cout << "load error" << endl;
        return -1;
    }
    

    namedWindow("均值濾波", WINDOW_AUTOSIZE);   //定義濾波后圖像顯示窗口屬性
    //定義軌跡條名稱和最大值
    char kernelName[20];
    sprintf(kernelName, "kernel尺寸 %d", g_nTrackbarMaxValue);

    //創建軌跡條
    createTrackbar(kernelName, "均值濾波", &g_nTrackbarValue, g_nTrackbarMaxValue, on_kernelTrackbar2);
    on_kernelTrackbar(g_nTrackbarValue, 0);


    waitKey(0);
    return 0;
}

 

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;

Mat src, dst;



int main()
{
    
    //原圖
    src = imread(".//pic//1.png",IMREAD_UNCHANGED);
    
    if (!src.data)
    {
        cout << "load error" << endl;
        return -1;
    }
    
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    imshow("input image", src);

    //中值模糊,7為卷積核大小
    //medianBlur(src, dst, 7);
    
    //雙邊模糊
    //15為計算的半徑
    //150:值域上的方差  這個參數的值越大,
    //就表明該像素鄰域內有更寬廣的顏色會被混合到一起,產生較大的半相等顏色區域。
    //3:空間域的方差   他的數值越大,意味着越遠的像素會相互影響,從而使更大的區域足夠相似的顏色獲取相同的顏色。
    bilateralFilter(src, dst, 15, 150, 3);
    

    imshow("雙邊濾波", dst);

    //邊緣銳化
    Mat resultImg;
    Mat kernel = (Mat_<int>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    filter2D(dst, resultImg, -1, kernel, Point(-1, -1), 0);
    imshow("Final", resultImg);
    waitKey(0);
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM