OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Denoising of colored images in OpenCV

Denoising is the process of removing noise from images. Noise can be caused by various sources like electronic interference in camera sensors, poor lighting conditions, or high ISO settings. OpenCV provides a method for denoising colored images using the Non-Local Means Denoising algorithm.

In this tutorial, we will explore how to denoise colored images using OpenCV:

Prerequisites:

  • Install necessary libraries:
pip install opencv-python

Step-by-Step Tutorial:

  • Import necessary libraries:
import cv2
  • Read the Noisy Image:
image = cv2.imread('path_to_noisy_image.jpg')
  • Apply Non-Local Means Denoising:

Using the cv2.fastNlMeansDenoisingColored() function.

denoised_image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)

Here:

  • The second argument is the output destination. None implies that a new array will be created for the output.
  • The third and fourth arguments are filter strength values (h for luminance and hForColorComponents for color images). Higher values will remove noise but also remove image details, producing a blurry image.
  • The fifth argument, templateWindowSize (default value 7), should be odd and affects the performance linearly: greater templateWindowSize means slower computation.
  • The last argument, searchWindowSize (default value 21), must be odd. Greater values will make the algorithm work slower but will produce better results.
  • Display the Images:

You can display both the noisy and denoised images for comparison.

cv2.imshow('Noisy Image', image)
cv2.imshow('Denoised Image', denoised_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • Optionally, Save the Denoised Image:
cv2.imwrite('path_to_save_denoised_image.jpg', denoised_image)

Tips:

  • The parameters for the denoising function (cv2.fastNlMeansDenoisingColored()) can be tweaked based on the type and level of noise in the image. It might require some trial and error to get the best results.

  • For denoising grayscale images, OpenCV provides cv2.fastNlMeansDenoising().

Conclusion:

OpenCV offers an efficient method to denoise colored images using the Non-Local Means algorithm. Proper parameter tuning can provide a balance between noise removal and preserving image details. This tool can be especially useful for post-processing images captured in low-light conditions or with high ISO settings.

  1. Python Code for Denoising RGB Images in OpenCV:

    import cv2
    
    # Load colored image
    img = cv2.imread('colored_image.jpg')
    
    # Denoise using Non-Local Means
    denoised_img = cv2.fastNlMeansDenoisingColored(img, None, h=10, hColor=10, templateWindowSize=7, searchWindowSize=21)
    
    # Display the original and denoised images
    cv2.imshow('Original Image', img)
    cv2.imshow('Denoised Image', denoised_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()