2016-06-20 79 views
1

我想通過opencv fastNlMeansDenoising()函數去除噪音。 但我的輸出圖像與原始噪聲圖像相同。fastNlMeansDenoising()不過濾掉噪音

輸入圖像:

enter image description here

代碼:

#include <iostream> 

#include <opencv2/opencv.hpp> 

#include <opencv2/highgui/highgui.hpp> 

#include <opencv2/imgproc/imgproc.hpp> 

using namespace std; 

using namespace cv; 


int main() { 

    Mat img = imread("noisy.jpg"); 

    if (!img.data) { 
     cout << "File not found" << endl; 
     return -1; 
    } 

    // first copy the image 
    Mat img_gray = img.clone(); 
    cvtColor(img, img_gray, CV_RGB2GRAY); 

    Mat img1; 
    //fastNlMeansDenoising(img_gray, img1, 3.0, 7, 21); 
    cv::fastNlMeansDenoising(img_gray, img1, 3.0, 7, 21); 

    imshow("img1", img1); 

    waitKey(); 

    return 0; 
} 

輸出圖像:

enter image description here

我看不到任何平滑效果。我不明白它的原因。 請幫助我使用此功能來消除噪音。感謝

+0

不知道它可能與你的問題,但你不必做'img.clone()'。 'cvtColor'會爲你創建輸出圖像。 – boaz001

+0

我已更正它。但仍然輸出圖像不是去噪圖像。 – Abc

回答

2

在OpenCV中,該函數被定義如下

void fastNlMeansDenoising(InputArray src, OutputArray dst, float h=3, int templateWindowSize=7, int searchWindowSize=21) 

其中

Parameters: 
src – Input 8-bit 1-channel, 2-channel or 3-channel image. 
dst – Output image with the same size and type as src . 
templateWindowSize – Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels 
searchWindowSize – Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels 
h – Parameter regulating filter strength. Big h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise 

因此,爲了去除噪聲,我必須增加濾波器強度參數h,大H值完全消除噪音,但較小的h值保留了細節並且還保留了一些噪音。

所以我通過使用這樣的功能完美地消除了噪音。

fastNlMeansDenoising(img_gray, img1, 30.0, 7, 21); 

輸出:

denoise image with filter strength 30

注:這個函數執行時間是在調試模式太慢。對於快一點的執行時間,最好在釋放模式下運行它。

希望這有助於 乾杯