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 |
|---|---|---|---|
|
|
tabby_tiger_cat.jpg |
Input image file path |
|
|
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:
Per-image parameters:
muandsigmaare rank-1 tensors with layout"N", one scalar per image in the batch.per_channel flag: When
Falsethe same noise sample is applied to every colour channel; set toTruefor independent per-channel noise.Reproducibility: The
seedparameter pins the PRNG state so results are deterministic across runs.Data type preservation: The output tensor keeps the same dtype and layout as the input; no implicit conversion occurs.
Clipping: For
U8inputs the operator automatically clamps the noisy values to[0, 255].
Expected Output
The output shows the image with visible Gaussian noise (sigma=25):
Original Input Image |
Output: Image with Gaussian Noise (sigma=25) |
CV-CUDA Operators Used
Operator |
Purpose |
|---|---|
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
Resize Operator - Basic image transformation
Common Utilities - Helper functions