1. 程式人生 > >雙邊濾波

雙邊濾波

ble log 單純 int cast 算法 else font sso

Qt 平臺,雙邊濾波原理代碼例如以下:

#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <cmath>

using namespace cv;
using namespace std;

double Gaussian( double x, double sigma )//高斯核參數計算函數
{
    return exp(-x/(2*sigma*sigma));
}

Mat GaussiCore( int d, double sigma )//高斯核計算
{
    int dHalf = (d-1)/2;
    Mat core( d, d, CV_64FC1, Scalar(0));
    for ( int i = -dHalf; i < dHalf; i ++ )
    {
        double *corePtr = core.ptr<double>(i+dHalf);
        for ( int j = -dHalf; j < dHalf; j ++ )
        {
            corePtr[j+dHalf] = Gaussian((i*i+j*j), sigma);
        }
    }
    return core;
}

int main()
{
    double duration;
    int d = 11;//濾波核直徑
    int dHalf = (d-1)/2;//濾波核半徑
    double sigma_d = 22;//雙邊濾波核高斯系數標準差
    double sigma_r = 11;//雙邊濾波核空間域系數標準差
    double temp1 = 0.0;
    double temp2 = 0.0;
    Mat src = imread("lena.jpg", 0);
    Mat srcBorder( src.rows+d-1, src.cols+d-1, CV_8UC1, Scalar(128));
    int srcRow = src.rows;
    int srcCol = src.cols;
    Mat dst( srcRow, srcCol, CV_8UC1, Scalar(0));
    Mat gaussiCore;
duration = static_cast<double>(getTickCount());
    for ( int i = 0; i < srcRow; i ++ )
    {
        uchar *srcPtr = src.ptr<uchar>(i);
        uchar *srcBorderPtr = srcBorder.ptr<uchar>(i+dHalf);
        for ( int j = 0; j < srcCol; j ++ )
        {
            srcBorderPtr[j+dHalf] = srcPtr[j];
        }
    }

    gaussiCore = GaussiCore( d, sigma_d );

    for ( int i = 0; i < srcRow; i ++ )
    {
        uchar *dstPtr = dst.ptr<uchar>(i);
        uchar *srcBorderPtr1 = srcBorder.ptr<uchar>(i+dHalf);
        for ( int j = 0; j < srcCol; j ++ )
        {
            temp1 = 0.0;
            temp2 = 0.0;
            for ( int n = -dHalf+i, rr = 0; n < dHalf+i; n ++, rr ++ )
            {
                uchar *srcBorderPtr2 = srcBorder.ptr<uchar>(n);
                double *gaussiCorePtr = gaussiCore.ptr<double>(rr);
                for ( int l = -dHalf+j, rl = 0; l < dHalf+j; l ++, rl ++ )
                {
                    temp1 += (double)srcBorderPtr2[l] * gaussiCorePtr[rl]
                            * Gaussian((srcBorderPtr2[l]-srcBorderPtr1[j+dHalf])*(srcBorderPtr2[l]-srcBorderPtr1[j+dHalf]), sigma_r);
                    temp2 +=  gaussiCorePtr[rl]
                            * Gaussian((srcBorderPtr2[l]-srcBorderPtr1[j+dHalf])*(srcBorderPtr2[l]-srcBorderPtr1[j+dHalf]), sigma_r);
                }
            }
            dstPtr[j] = temp1/temp2;
        }
    }

duration = static_cast<double>(getTickCount()) - duration;
duration /= getTickFrequency();
cout << duration << endl;
    namedWindow("src", 0);
    imshow("src", src);
    namedWindow("dst", 0);
    imshow("dst", dst);
    waitKey(0);
    return 0;
}


技術分享


由於代碼單純介紹雙邊濾波原理。所以與 OpenCV 自帶雙邊濾波速度還是有非常大差距,有時間會繼續研究關於雙邊濾波的加速算法。下面是雙邊濾波核計算公式,對於雙邊濾波為什麽能保邊降噪。這裏簡單說明一下:首先降噪是肯定的。由於濾波核有高斯濾波系數。總所周知高斯函數有濾波降噪的作用。而雙邊濾波系數除了有高斯系數之外。還增加了像素領域的值域差系數,即像素領域內的像素值與像素本身的像素值相差越大,依據最後一個總體公式能夠看出,總體系數會變小,從而該像素受到影響也就越小,而僅僅有在圖像的邊緣區域才會有這樣的情況出生,所以在圖像的邊緣附近差點兒不受濾波系數的影響,這樣就起到了保邊的效果。

1、離散卷積公式:

技術分享


2、高斯系數:

技術分享


3、空間域系數:

技術分享


4、總體雙邊濾波系數:

技術分享


做了一點加速。比原來的快幾十毫秒,代碼例如以下:

#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <cmath>

using namespace cv;
using namespace std;

double Gaussian( double x, double sigma )//高斯核參數計算函數
{
    return exp(-x/(2*sigma*sigma));
}

Mat GaussiCore( int d, double sigma )//高斯核計算
{
    int dHalf = (d-1)/2;
    int row = d;
    int col = d;
    Mat core( row, col, CV_64FC1, Scalar(0));
    if ( core.isContinuous() )
    {
        double *corePtr = core.ptr<double>(0);
        for ( int i = 0; i < row; i ++ )
        {
            for ( int j = 0; j < col; j ++ )
            {
                corePtr[i*col+j] = Gaussian(((i-dHalf)*(i-dHalf)+(j-dHalf)*(j-dHalf)), sigma);
            }
        }
    }
    else
    {
        for ( int i = -dHalf; i < dHalf; i ++ )
        {
            double *corePtr = core.ptr<double>(i+dHalf);
            for ( int j = -dHalf; j < dHalf; j ++ )
            {
                corePtr[j+dHalf] = Gaussian((i*i+j*j), sigma);
            }
        }
    }
    return core;
}

int main()
{
    double duration;
    int d = 11;//濾波核直徑
    int dHalf = (d-1)/2;//濾波核半徑
    double sigma_d = 22;//雙邊濾波核高斯系數標準差
    double sigma_r = 11;//雙邊濾波核空間域系數標準差
    double temp1 = 0.0;
    double temp2 = 0.0;
    Mat src = imread("lena.jpg", 0);
    Mat srcBorder( src.rows+d-1, src.cols+d-1, CV_8UC1, Scalar(128));
    int srcRow = src.rows;
    int srcCol = src.cols;
    Mat dst( srcRow, srcCol, CV_8UC1, Scalar(0));
    Mat gaussiCore;
duration = static_cast<double>(getTickCount());

    if ( src.isContinuous() && srcBorder.isContinuous() )
    {
        uchar *srcPtr = src.ptr<uchar>(0);
        uchar *srcBorderPtr = srcBorder.ptr<uchar>(0);
        for ( int i = 0; i < srcRow; i ++ )
        {
            for ( int j = 0; j < srcCol; j ++ )
            {
                srcBorderPtr[(i+dHalf)*(srcCol+d-1)+j+dHalf] = srcPtr[i*srcCol+j];
            }
        }
    }
    else
    {
        for ( int i = 0; i < srcRow; i ++ )
        {
            uchar *srcPtr = src.ptr<uchar>(i);
            uchar *srcBorderPtr = srcBorder.ptr<uchar>(i+dHalf);
            for ( int j = 0; j < srcCol; j ++ )
            {
                srcBorderPtr[j+dHalf] = srcPtr[j];
            }
        }
    }

    gaussiCore = GaussiCore( d, sigma_d );

    for ( int i = 0; i < srcRow; i ++ )
    {
        uchar *dstPtr = dst.ptr<uchar>(i);
        uchar *srcBorderPtr1 = srcBorder.ptr<uchar>(i+dHalf);
        for ( int j = 0; j < srcCol; j ++ )
        {
            temp1 = 0.0;
            temp2 = 0.0;
            for ( int n = -dHalf+i, rr = 0; n < dHalf+i; n ++, rr ++ )
            {
                uchar *srcBorderPtr2 = srcBorder.ptr<uchar>(n);
                double *gaussiCorePtr = gaussiCore.ptr<double>(rr);
                for ( int l = -dHalf+j, rl = 0; l < dHalf+j; l ++, rl ++ )
                {
                    temp1 += (double)srcBorderPtr2[l] * gaussiCorePtr[rl]
                            * Gaussian((srcBorderPtr2[l]-srcBorderPtr1[j+dHalf])*(srcBorderPtr2[l]-srcBorderPtr1[j+dHalf]), sigma_r);
                    temp2 +=  gaussiCorePtr[rl]
                            * Gaussian((srcBorderPtr2[l]-srcBorderPtr1[j+dHalf])*(srcBorderPtr2[l]-srcBorderPtr1[j+dHalf]), sigma_r);
                }
            }
            dstPtr[j] = temp1/temp2;
        }
    }

duration = static_cast<double>(getTickCount()) - duration;
duration /= getTickFrequency();
cout << duration << endl;
    namedWindow("src", 0);
    imshow("src", src);
    namedWindow("dst", 0);
    imshow("dst", dst);
    waitKey(0);
    return 0;
}


雙邊濾波