Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastNlMeansDenoising() does not filter out noise

Tags:

c++

opencv

I am trying to remove noise by opencv fastNlMeansDenoising() function. But My output image is same like original noised image.

Input image:

enter image description here

Code:

#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;
}

Output Image:

enter image description here

I can not see any effect of smoothing. I do not understand reason of it. Please help me to use this function for removing noise. thanks

like image 886
Abc Avatar asked Jun 20 '16 05:06

Abc


People also ask

How do I remove noise from an image in Python?

Mean Filter The mean filter is used to blur an image in order to remove noise. It involves determining the mean of the pixel values within a n x n kernel. The pixel intensity of the center element is then replaced by the mean. This eliminates some of the noise in the image and smooths the edges of the image.

What is fastNlMeansDenoisingColored?

1. cv.fastNlMeansDenoisingColored() As mentioned above it is used to remove noise from color images. ( Noise is expected to be gaussian).

What is Opencv noise?

Noise is a random variable with its mean as 0. Let us consider a noisy pixel P = p + n where p is the pixel value, and n is the noise.


1 Answers

In OpenCV, the function is defined as follows

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

where

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

Therefore, in order to remove noise, I had to increase the filter strength parameter h, big h value perfectly removes noise, but a smaller h value preserves details and also preserve some noise.

So I perfectly removed the noise by using the function like this:

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

Output:

denoise image with filter strength 30

Note: This function's execution time is too slow in debug mode. For a little bit faster execution time, better to run it in release mode.

like image 103
Abc Avatar answered Sep 19 '22 14:09

Abc