Color Manipulation Techniques
Advanced color transformations go beyond simple conversions, enabling complex image processing and visual effects.
graph TD
A[Advanced Color Transformations] --> B[Color Adjustment]
A --> C[Color Filtering]
A --> D[Color Quantization]
A --> E[Color Mapping]
Color Adjustment Strategies
Brightness and Contrast Manipulation
import numpy as np
import cv2
def adjust_brightness_contrast(image, brightness=0, contrast=1.0):
"""
Adjust image brightness and contrast
:param image: Input image
:param brightness: Brightness adjustment (-255 to 255)
:param contrast: Contrast adjustment (0.0 to 3.0)
:return: Transformed image
"""
adjusted = np.clip(
contrast * image + brightness,
0, 255
).astype(np.uint8)
return adjusted
## Example usage
image = cv2.imread('sample.jpg')
bright_image = adjust_brightness_contrast(image, brightness=50, contrast=1.2)
Color Filtering Techniques
Color Masking
def color_mask(image, lower_bound, upper_bound):
"""
Create a color mask for specific color ranges
:param image: Input image in BGR format
:param lower_bound: Lower color boundary
:param upper_bound: Upper color boundary
:return: Masked image
"""
mask = cv2.inRange(image, lower_bound, upper_bound)
return mask
## Example: Isolate red colors
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
red_mask = color_mask(hsv_image, lower_red, upper_red)
Color Quantization Methods
Technique |
Description |
Use Case |
K-Means Clustering |
Reduce color palette |
Image compression |
Median Cut |
Divide color space |
Color reduction |
Octree Quantization |
Hierarchical color reduction |
Graphics rendering |
K-Means Color Quantization
from sklearn.cluster import KMeans
def quantize_colors(image, n_colors=8):
"""
Reduce image colors using K-Means clustering
:param image: Input image
:param n_colors: Number of colors to reduce to
:return: Quantized image
"""
pixels = image.reshape(-1, 3)
kmeans = KMeans(n_clusters=n_colors, random_state=42)
kmeans.fit(pixels)
labels = kmeans.predict(pixels)
quantized = kmeans.cluster_centers_[labels].reshape(image.shape)
return quantized.astype(np.uint8)
## Quantize image to 8 colors
quantized_image = quantize_colors(image, n_colors=8)
Gradient Color Mapping
def create_color_gradient(size, start_color, end_color):
"""
Generate a color gradient
:param size: Gradient size
:param start_color: Starting color (RGB)
:param end_color: Ending color (RGB)
:return: Gradient image
"""
gradient = np.zeros((size, 3), dtype=np.uint8)
for i in range(size):
ratio = i / (size - 1)
gradient[i] = [
int(start_color[j] + ratio * (end_color[j] - start_color[j]))
for j in range(3)
]
return gradient.reshape((size, 1, 3))
## Create a red to blue gradient
gradient = create_color_gradient(256, (255, 0, 0), (0, 0, 255))
- Choose appropriate color spaces for specific tasks
- Consider computational complexity
- Validate transformation results
- LabEx recommends experimenting with different techniques
Key Takeaways
- Advanced color transformations enable complex image processing
- Multiple techniques exist for color manipulation
- Understanding color spaces is crucial
- Practical implementation requires careful parameter tuning