I am trying to remove noise by opencv fastNlMeansDenoising() function. But My output image is same like original noised image.
Input image:
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:
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
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.
1. cv.fastNlMeansDenoisingColored() As mentioned above it is used to remove noise from color images. ( Noise is expected to be gaussian).
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With