Brightness Contrast
Overview
The Brightness Contrast sample demonstrates GPU-accelerated brightness and contrast adjustment
using CV-CUDA’s brightness_contrast operator. The operator applies a per-image affine
transform to every pixel:
output = brightness * (contrast * (input − contrast_center) + contrast_center) + brightness_shift
where brightness, contrast, brightness_shift, and contrast_center are
per-image scalar tensors. This formulation separates multiplicative brightness from the
contrast pivot, giving fine-grained control over image appearance.
Usage
Basic Usage
Apply the default brightening/contrast boost to the bundled test image:
python3 brightness_contrast.py
Custom Input
Supply your own image:
python3 brightness_contrast.py -i input.jpg -o output.jpg
Command-Line Arguments
Argument |
Short Form |
Default |
Description |
|---|---|---|---|
|
|
tabby_tiger_cat.jpg |
Input image file path |
|
|
cvcuda/.cache/cat_brightness_contrast.jpg |
Output image file path |
Implementation
Parameter Setup
# The brightness_contrast operator expects per-image scalar parameters
# supplied as 1-D tensors with layout "N" (one element per image in the batch).
# brightness: multiplicative gain applied to the pixel values (>1 brightens).
# contrast: multiplier around contrast_center (<1 compresses, >1 expands).
# brightness_shift: additive offset added after brightness scaling.
# contrast_center: the pivot value around which contrast is computed (default 0.0).
brightness_host = np.array([1.5], dtype=np.float32)
contrast_host = np.array([1.4], dtype=np.float32)
brightness_shift_host = np.array([20.0], dtype=np.float32)
contrast_center_host = np.array([127.0], dtype=np.float32)
brightness = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
contrast = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
brightness_shift = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
contrast_center = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
cuda_memcpy_h2d(brightness_host, brightness.cuda())
cuda_memcpy_h2d(contrast_host, contrast.cuda())
cuda_memcpy_h2d(brightness_shift_host, brightness_shift.cuda())
cuda_memcpy_h2d(contrast_center_host, contrast_center.cuda())
Operator Call
# The brightness_contrast operator expects per-image scalar parameters
# supplied as 1-D tensors with layout "N" (one element per image in the batch).
# brightness: multiplicative gain applied to the pixel values (>1 brightens).
# contrast: multiplier around contrast_center (<1 compresses, >1 expands).
# brightness_shift: additive offset added after brightness scaling.
# contrast_center: the pivot value around which contrast is computed (default 0.0).
brightness_host = np.array([1.5], dtype=np.float32)
contrast_host = np.array([1.4], dtype=np.float32)
brightness_shift_host = np.array([20.0], dtype=np.float32)
contrast_center_host = np.array([127.0], dtype=np.float32)
brightness = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
contrast = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
brightness_shift = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
contrast_center = cvcuda.Tensor((1,), dtype=cvcuda.Type.F32, layout="N")
cuda_memcpy_h2d(brightness_host, brightness.cuda())
cuda_memcpy_h2d(contrast_host, contrast.cuda())
cuda_memcpy_h2d(brightness_shift_host, brightness_shift.cuda())
cuda_memcpy_h2d(contrast_center_host, contrast_center.cuda())
Key points:
Parameter tensors — each of
brightness,contrast,brightness_shift, andcontrast_centeris a 1-D"N"-layout float32 tensor with one element per image.All parameters are optional — you may pass any subset; omitted parameters default to identity values (brightness=1, contrast=1, brightness_shift=0, contrast_center=0).
Dtype preserved — the output tensor has the same dtype and layout as the input, so no post-processing conversion is needed for uint8 HWC images.
contrast_center pivot — setting
contrast_centerto 127.0 for uint8 data places the pivot at mid-gray, which preserves overall luminance while expanding tonal range.
Expected Output
Original Input Image |
Output: Brightness × 1.5, Contrast × 1.4 |
CV-CUDA Operators Used
Operator |
Purpose |
|---|---|
Per-image affine pixel transform for brightness and contrast adjustment |
Common Utilities Used
read_image() - Load image as CV-CUDA tensor
write_image() - Save adjusted image
cuda_memcpy_h2d- Upload per-image scalar parameters from host to device
See Also
Resize Operator - Resize images with CV-CUDA
Common Utilities - Helper functions used by all operator samples