Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Changing Contrast
Contrast adjustment makes the difference between light and dark areas more (or less) pronounced. High contrast helps edge detection; low contrast reduces noise sensitivity.
8 min•By Priygop Team•Updated 2026
Contrast Adjustment
Contrast Adjustment
import cv2
import numpy as np
image = cv2.imread("photo.jpg")
# Method 1: Scale pixel values (alpha = contrast multiplier)
# alpha > 1.0: higher contrast
# alpha < 1.0: lower contrast (0.5 = half contrast)
# alpha = 1.0: unchanged
high_contrast = cv2.convertScaleAbs(image, alpha=2.0, beta=0)
low_contrast = cv2.convertScaleAbs(image, alpha=0.5, beta=0)
# Method 2: Histogram Equalisation (auto-contrast for grayscale)
# Redistributes pixel values to use the full 0-255 range
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
equalized = cv2.equalizeHist(gray)
# Method 3: CLAHE (better than simple equalisation for local contrast)
# Works well for medical images, low-light photos
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_result = clahe.apply(gray)
cv2.imwrite("high_contrast.jpg", high_contrast)
cv2.imwrite("low_contrast.jpg", low_contrast)
cv2.imwrite("equalized.jpg", equalized)
cv2.imwrite("clahe.jpg", clahe_result)
print("Contrast adjustments complete")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment