Denoising a photo using non-local means algorithms
In this recipe, you will learn how to remove noise from images using non-local means algorithms. This functionality is useful when photos suffer from excessive noise and it's necessary to remove it to get a better looking image.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV version 3.3 (or greater) Python API package.
How to do it
You need to complete the following steps:
- Import the necessary modules:
import cv2 import numpy as np import matplotlib.pyplot as plt
- Load the test image:
img = cv2.imread('../data/Lena.png')
- Generate a random Gaussian noise:
noise = 30 * np.random.randn(*img.shape) img = np.uint8(np.clip(img + noise, 0, 255))
- Perform denoising using the non-local means algorithm:
denoised_nlm = cv2.fastNlMeansDenoisingColored(img, None, 10)
- Visualize the results:
plt.figure(0, figsize=(10,6)) plt.subplot(121) plt.axis('off') plt.title('original') plt.imshow(img[:,:,[2,1,0]]) plt.subplot(122) plt.axis('off...