雙邊濾波
阿新 • • 發佈:2017-06-03
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; }
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; }
雙邊濾波