Add noise with varying grain size in python

Adding noise to an image can be a useful technique in various applications such as image processing, computer vision, and machine learning. In Python, there are several ways to add noise with varying grain size to an image. In this article, we will explore three different approaches to solve this problem.

Approach 1: Using the NumPy library

The NumPy library provides a function called random.normal that can be used to generate random numbers from a normal distribution. We can utilize this function to add noise to an image. Here is a sample code that demonstrates this approach:

import numpy as np
import cv2

def add_noise(image, grain_size):
    noise = np.random.normal(0, grain_size, image.shape)
    noisy_image = cv2.add(image, noise)
    return noisy_image

# Example usage
image = cv2.imread('image.jpg')
noisy_image = add_noise(image, 10)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

This approach uses the np.random.normal function to generate random numbers with a mean of 0 and a standard deviation equal to the specified grain size. The generated noise is then added to the original image using the cv2.add function from the OpenCV library.

Approach 2: Using the scikit-image library

The scikit-image library provides a function called util.random_noise that can be used to add various types of noise to an image, including Gaussian noise. Here is a sample code that demonstrates this approach:

import cv2
from skimage.util import random_noise

def add_noise(image, grain_size):
    noisy_image = random_noise(image, mode='gaussian', var=grain_size)
    noisy_image = np.array(255 * noisy_image, dtype=np.uint8)
    return noisy_image

# Example usage
image = cv2.imread('image.jpg')
noisy_image = add_noise(image, 0.01)
cv2.imshow('Noisy Image', noisy_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

This approach uses the random_noise function from the scikit-image library to add Gaussian noise to the image. The var parameter specifies the variance of the Gaussian distribution, which controls the grain size of the noise. The resulting noisy image is then converted back to the uint8 data type for display using the np.array function.

Approach 3: Using the Pillow library

The Pillow library provides a function called ImageOps.posterize that can be used to posterize an image by reducing the number of colors. By reducing the number of colors to a small value, we can simulate the effect of adding noise with varying grain size. Here is a sample code that demonstrates this approach:

from PIL import Image, ImageOps

def add_noise(image, grain_size):
    noisy_image = ImageOps.posterize(image, grain_size)
    return noisy_image

# Example usage
image = Image.open('image.jpg')
noisy_image = add_noise(image, 4)
noisy_image.show()

This approach uses the ImageOps.posterize function from the Pillow library to reduce the number of colors in the image to the specified grain size. The resulting noisy image is then displayed using the show method.

After evaluating these three approaches, it can be concluded that Approach 2, which utilizes the scikit-image library, is the better option. This is because the scikit-image library provides a dedicated function for adding noise to an image, allowing for more flexibility and control over the type and parameters of the noise. Additionally, scikit-image is a widely used library in the field of image processing and computer vision, making it a reliable choice for such tasks.

Rate this post

3 Responses

  1. Approach 2 with scikit-image library seems more user-friendly and gives better control over variations. Thoughts? 🧐

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents