Gaussian Noise

Overview

The Gaussian Noise sample demonstrates how to add per-image Gaussian noise to an image using CV-CUDA’s GPU-accelerated gaussiannoise operator. The operator accepts per-image mean (mu) and standard deviation (sigma) tensors, making it straightforward to apply different noise levels to images in a batch.

Usage

Basic Usage

Add Gaussian noise with default settings:

python3 gaussiannoise.py -i input.jpg

Custom Output Path

Save the noisy image to a specific location:

python3 gaussiannoise.py -i input.jpg -o noisy_cat.jpg

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

--output

-o

cvcuda/.cache/cat_gaussiannoise.jpg

Output image file path

Implementation

Applying Gaussian Noise

# Wrap the HWC single image in a batch dimension (N=1) so the
# gaussiannoise operator can receive per-image mu/sigma tensors.
nhwc_image: cvcuda.Tensor = input_image.reshape((1, *input_image.shape), "NHWC")

# mu and sigma are per-image scalars with layout "N".
# mu=0 means zero-mean (no brightness shift), sigma controls noise strength.
sigma_value = 25.0  # visible but not destructive for an 8-bit image
mu_host = np.array([0.0], dtype=np.float32)
sigma_host = np.array([sigma_value], dtype=np.float32)

mu_tensor = cvcuda.Tensor((1,), cvcuda.Type.F32, "N")
sigma_tensor = cvcuda.Tensor((1,), cvcuda.Type.F32, "N")
cuda_memcpy_h2d(mu_host, mu_tensor.cuda())
cuda_memcpy_h2d(sigma_host, sigma_tensor.cuda())

Key points:

  1. Per-image parameters: mu and sigma are rank-1 tensors with layout "N", one scalar per image in the batch.

  2. per_channel flag: When False the same noise sample is applied to every colour channel; set to True for independent per-channel noise.

  3. Reproducibility: The seed parameter pins the PRNG state so results are deterministic across runs.

  4. Data type preservation: The output tensor keeps the same dtype and layout as the input; no implicit conversion occurs.

  5. Clipping: For U8 inputs the operator automatically clamps the noisy values to [0, 255].

Expected Output

The output shows the image with visible Gaussian noise (sigma=25):

../../_images/tabby_tiger_cat.jpg

Original Input Image

../../_images/cat_gaussiannoise.jpg

Output: Image with Gaussian Noise (sigma=25)

CV-CUDA Operators Used

Operator

Purpose

cvcuda.gaussiannoise()

Add per-image Gaussian noise with configurable mu, sigma, and per-channel control

Common Utilities Used

  • read_image() - Load image as CV-CUDA tensor

  • write_image() - Save noisy image

  • cuda_memcpy_h2d - Upload mu/sigma parameter arrays to GPU

See Also