0
0
Computer Visionml~5 mins

Color transforms (brightness, contrast, hue) in Computer Vision

Choose your learning style9 modes available
Introduction

Color transforms help change how an image looks by adjusting brightness, contrast, or hue. This makes images clearer or more interesting for computers to understand.

Making a dark photo brighter so details are easier to see.
Increasing contrast to highlight differences between objects in an image.
Changing the hue to correct colors or create artistic effects.
Preparing images for machine learning models to improve accuracy.
Augmenting data by varying colors to help models learn better.
Syntax
Computer Vision
brightness_image = original_image * brightness_factor
contrast_image = (original_image - mean) * contrast_factor + mean
hue_image = adjust_hue(original_image, hue_factor)

Brightness factor > 1 makes the image brighter; between 0 and 1 makes it darker.

Contrast adjustment usually centers around the mean pixel value.

Examples
This makes the image 20% brighter.
Computer Vision
brightness_image = original_image * 1.2
This increases contrast by 50%, assuming pixel values range 0-255.
Computer Vision
contrast_image = (original_image - 128) * 1.5 + 128
This shifts the hue slightly to change colors.
Computer Vision
hue_image = adjust_hue(original_image, 0.1)
Sample Model

This code creates a small 2x2 color image and applies brightness, contrast, and hue changes. It prints the original and transformed images as arrays.

Computer Vision
import cv2
import numpy as np

def adjust_brightness(image, factor):
    return np.clip(image * factor, 0, 255).astype(np.uint8)

def adjust_contrast(image, factor):
    mean = np.mean(image, axis=(0,1), keepdims=True)
    return np.clip((image - mean) * factor + mean, 0, 255).astype(np.uint8)

def adjust_hue(image, factor):
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV).astype(np.float32)
    hsv[..., 0] = (hsv[..., 0] + factor * 180) % 180
    hsv = hsv.astype(np.uint8)
    return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

# Load sample image
image = np.full((2, 2, 3), [100, 150, 200], dtype=np.uint8)  # simple 2x2 image with color

bright = adjust_brightness(image, 1.5)
contrast = adjust_contrast(image, 1.5)
hue = adjust_hue(image, 0.1)

print("Original image:\n", image)
print("\nBrightened image:\n", bright)
print("\nContrast adjusted image:\n", contrast)
print("\nHue adjusted image:\n", hue)
OutputSuccess
Important Notes

Brightness and contrast changes can cause pixel values to go outside 0-255, so clipping is important.

Hue adjustment works best in HSV color space, which separates color from brightness.

Small images are used here for simplicity; real images are larger but use the same methods.

Summary

Brightness changes make images lighter or darker by scaling pixel values.

Contrast changes stretch or shrink differences between pixel values around their average.

Hue changes shift colors by rotating the color wheel in HSV space.