Computer vision involves analyzing and processing images, but images are often corrupted by noise, which can negatively impact the performance of algorithms used for tasks like image segmentation and object recognition. OpenCV, an open-source computer vision library, offers a diverse set of tools and techniques for image processing and analysis.
Dealing with noise is a significant challenge in image processing, and OpenCV provides various methods to address this issue. In this article, we will explore noise tolerance in OpenCV and demonstrate its implementation through code examples.
What is Noise?
One of the most important studies in the digital processing of images is noise, it is usually the factor that determines the success or failure of any image enhancement or reconstruction/recreation schemes, most of which fail in the presence of significant noise.
We must consider how much of the detected signal can be confirmed and how much is related to random background events resulting from either the detection or transmission process.
These random events are classified under the general topic of noise. This noise can result from various sources, including the discrete nature of radiation, variation in detector sensitivity, photo-graphic grain effects, data transmission errors, properties of imaging systems such as water droplets, and image errors.
In each case, the noise properties are different, as are the image-processing operations that can be applied to reduce their effects.
What is Noise Tolerance in OpenCV?
Noise tolerance in OpenCV refers to an algorithm's ability to withstand the effects of noise in an image.
OpenCV provides denoising techniques to remove noise from images while preserving essential features.
The main goal is to obtain a clean image that can be further processed by other algorithms without being influenced by noise.
By effectively handling noise, computer vision tasks like object recognition and image segmentation can achieve more accurate and reliable results.
Application of Computer Vision
Following are the application of computer vision:
Structure from Motion:
Throw away motion and keep the structure from image-based rendering.
Throw away structure and keep motion: used for mobile robot control.
Image Collections:
Image recovery: finding images containing specific objects like cars and trees.
Image analysis: providing a textual description of objects present in an image.
Finding faces in group pictures or crowded scenes.
Recovering the spoken pose of a person from a video.
Medical Applications:
Image enhancement, brain division, and image registration to compare brains of different people or before/after lesion.
Blood vessel tracking and cell tracking for medical research.
Patient monitoring using medical imaging.
Human-Computer Interaction (HCI):
Eye motion tracking for user interface control.
Recognition of physical gestures for interaction with computers and devices.
Types of Noise
There are several types of noise that can corrupt images and affect their quality and accuracy in computer vision and image processing applications.
Some common types of noise include Gaussian Noise and Salt and Pepper Noise. These two noises and explained below:
Gaussian Noise
This type of noise follows a Gaussian distribution and is characterized by random variations in pixel values, resulting in a smooth and continuous noise distribution.
Implementation
# Import necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import random
# Specify the path to the image
image_path = 'image1.jpg'
# Load the original image
original_image = Image.open(image_path)
# Convert the image to a NumPy array
image_array = np.array(original_image)
# Function to add Gaussian noise to the image
def add_gaussian_noise(image, mean, std_dev):
# Generate Gaussian noise with the specified mean and standard deviation
noise = np.random.normal(mean, std_dev, image.shape).astype(np.uint8)
# Clip the noisy image to ensure pixel values stay within the valid range [0, 245]
noisy_image = np.clip(image + noise, 0, 245)
return noisy_image
# Add Gaussian noise to the image
noisy_image_gaussian = add_gaussian_noise(image_array, mean=0, std_dev=30)
# Convert the NumPy arrays back to images
noisy_image_gaussian = Image.fromarray(noisy_image_gaussian)
# Display the original and corrupted images side by side
fig, axs = plt.subplots(1, 2, figsize=(12, 4))
axs[0].imshow(original_image)
axs[0].set_title('Original Image')
axs[0].axis('off')
axs[1].imshow(noisy_image_gaussian)
axs[1].set_title('Gaussian Noise')
axs[1].axis('off')
# Display the plot
plt.tight_layout()
plt.show()
Output
Salt and Pepper Noise
Also known as impulse noise, this type of noise adds random black and white pixels to the image, simulating the appearance of salt and pepper sprinkled on the image.
Implementation
# Import necessary libraries
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import random
# Specify the path to the image
image_path = 'image2.jpg'
# Load the original image
original_image = Image.open(image_path)
# Convert the image to a NumPy array
image_array = np.array(original_image)
# Function to add salt and pepper noise to the image
def add_salt_and_pepper_noise(image, salt_prob, pepper_prob):
# Create a copy of the image to apply noise
noisy_image = np.copy(image)
height, width, channels = noisy_image.shape
# Loop through each pixel and add noise based on probabilities
for i in range(height):
for j in range(width):
rand = random.random()
if rand < salt_prob:
noisy_image[i, j] = [245, 245, 245] # Add salt noise (white pixel)
elif rand > 1 - pepper_prob:
noisy_image[i, j] = [0, 0, 0] # Add pepper noise (black pixel)
return noisy_image
# Add salt and pepper noise to the image
noisy_image_salt_pepper = add_salt_and_pepper_noise(image_array, salt_prob=0.01, pepper_prob=0.01)
# Convert the NumPy array back to an image
noisy_image_salt_pepper = Image.fromarray(noisy_image_salt_pepper)
# Display the original and corrupted images side by side
fig, axs = plt.subplots(1, 2, figsize=(12, 4))
axs[0].imshow(original_image)
axs[0].set_title('Original Image')
axs[0].axis('off')
axs[1].imshow(noisy_image_salt_pepper)
axs[1].set_title('Salt and Pepper Noise')
axs[1].axis('off')
# Display the plot
plt.tight_layout()
plt.show()
Output
Noise Removal Technique
There are many noise removal techniques, one of which is explained here in detail.
Proposed Algorithm
The following algorithm is proposed to address detecting noisy pixels in an image polluted by random-valued tendency noise, which is more challenging than fixed-valued impulse noise. Standard median-based impulse detection approaches often fail in the presence of random-valued impulse noise because the value of a noisy pixel may not be significantly higher or lower than that of its neighbors.
Algorithm Steps:
Read the input image and introduce Random-Valued Impulse Noise to the image.
Calculate the weighted mean value of the pixel window.
Compute the weighted standard deviation using the weighted mean value. .
Obtain the threshold value from the statistical parameters mentioned above.
Identify noisy pixels by comparing the difference between the center pixel and the weighted mean with the threshold value.
Assign binary flags to pixels based on noise presence:
1: Indicates a noisy pixel.
0: Represents a noise-free pixel.
Further Processing:
Calculate the median value for noise-free pixels.
Determine the absolute difference between each pixel value and the median value.
Compute the rough membership value for each pixel.
Calculate the recovery term using the rough membership value.
This proposed algorithm is designed to effectively detect and differentiate random-valued impulse noise from the rest of the image pixels, providing a more accurate way to handle this challenging noise type in image processing.
Conclusion
To improve the reliability of image processing algorithms, noise tolerance is critical in computer vision. The Wiener filter and proposed techniques are two extensively used noise reduction algorithms.