Module II - Basic Image Processing
Operations in OpenCV
I. Color Models: RGB, HSV, HSL
1. Introduction to Color Models
A color model is a mathematical representation of colors.
It defines how colors can be represented as tuples of values.
Used in image processing, computer vision, graphics, and printing.
2. RGB Color Model
Definition: Based on additive color mixing – Red, Green, and Blue channels.
Range: Each channel: 0–255 (8-bit) or 0–1 (normalized).
Usage: Displays (monitors, TVs), cameras, image storage (JPEG, PNG).
Properties:
Combines three channels to produce colors.
White: (255, 255, 255), Black: (0, 0, 0)
Pure Red: (255, 0, 0), Pure Green: (0, 255, 0), Pure Blue: (0, 0, 255)
Limitations:
Not perceptually uniform (small changes in RGB don't always look the same to the human eye).
Difficult to manipulate in terms of hue and saturation.
3. HSV Color Model
HSV: Hue, Saturation, Value.
Components:
Hue (H): Represents color type (0–360° or 0–179 in OpenCV).
Saturation (S): Intensity or purity of the color (0–1 or 0–255).
Value (V): Brightness (0–1 or 0–255).
Advantages:
More intuitive for humans to describe colors (e.g., "bright red", "dark blue").
Good for color-based segmentation and filtering in OpenCV.
Module II - Basic Image Processing Operations in OpenCV 1
4. HSL Color Model
HSL: Hue, Saturation, Lightness.
Similar to HSV but Lightness represents the average of the brightest and darkest parts.
Components:
Hue (H): Same as HSV.
Saturation (S): Color intensity (differs slightly from HSV formula).
Lightness (L): Measures how light/dark the color is.
Usage: Web design (CSS uses HSL), image adjustments.
5. OpenCV – Color Model Conversions
Some algorithms work best in HSV or grayscale (e.g., object tracking, thresholding).
OpenCV defaults to BGR (not RGB), so conversions are often necessary.
BGR ↔ RGB:
img_rgb = [Link](img_bgr, cv2.COLOR_BGR2RGB)
img_bgr = [Link](img_rgb, cv2.COLOR_RGB2BGR)
BGR ↔ HSV:
img_hsv = [Link](img_bgr, cv2.COLOR_BGR2HSV)
img_bgr = [Link](img_hsv, cv2.COLOR_HSV2BGR)
BGR ↔ HLS:
img_hls = [Link](img_bgr, cv2.COLOR_BGR2HLS)
img_bgr = [Link](img_hls, cv2.COLOR_HLS2BGR)
BGR ↔ Grayscale:
img_gray = [Link](img_bgr, cv2.COLOR_BGR2GRAY)
II. Blending Images
1. What is Image Blending?
Definition: Combining two images to produce a composite image.
Purpose:
Module II - Basic Image Processing Operations in OpenCV 2
Add a watermark or logo.
Create artistic effects.
Merge two scenes smoothly.
2. Methods of Image Blending
There are two major approaches:
A. Weighted Addition (Linear Blending)
Uses the formula:
Blended Image = α ⋅ I1 + β ⋅ I2 + γ
Where:
αand β are blending weights (typically α + β = 1).
γ is an optional scalar added to the result, say for brightness adjustment.
Example Code:
import cv2
img1 = [Link]('[Link]')
img2 = [Link]('[Link]')
# Resize images to same size
img2 = [Link](img2, ([Link][1], [Link][0]))
# Weighted blending
blended = [Link](img1, 0.7, img2, 0.3, 0)
[Link](blended)
B. Bitwise Operations with Masks
Used when blending a logo or transparent object onto a background.
Steps:
1. Convert logo to grayscale.
2. Invert the mask for the background region.
3. Use bitwise_and() to extract the ROI from the background and logo.
4. Add both images together
Example Code:
Module II - Basic Image Processing Operations in OpenCV 3
import cv2
import [Link] as plt
import numpy as np
#Read and resize images
img1 = [Link]('dog_backpack.jpg')
img2 = [Link]('watermark_no_copy.png')
img1 = [Link](img1, cv2.COLOR_BGR2RGB)
img2 = [Link](img2, cv2.COLOR_BGR2RGB)
img2 = [Link](img2, (600,600))
# Step 1: Define ROI
x_start = [Link][1] - [Link][1]
y_start = [Link][0] - [Link][0]
x_end = [Link][1]
y_end = [Link][0]
roi = img1[y_start:y_end, x_start:x_end]
# Step 2: Convert logo to grayscale
logo_gray = [Link](img2, cv2.COLOR_RGB2GRAY)
# Step 3: Create mask
mask_inv = cv2.bitwise_not(logo_gray)
# Step 4: Extract non-zero region in logo (creating transparency)
fg = cv2.bitwise_or(img2, img2, mask=mask_inv)
# Step 5: combining roi and fg
img_roi = cv2.bitwise_or(roi, fg)
# Step 6: replace img roi region in orignal image
img1[y_start:y_end, x_start:x_end] = img_roi
[Link](img1)
3. Why Bitwise Operations?
Needed when one image has transparent regions or when we only want to blend specific parts.
Bitwise operations:
bitwise_or() → combines regions.
bitwise_not() → inverts masks.
Module II - Basic Image Processing Operations in OpenCV 4
4. Difference Between Weighted Blending and Bitwise Blending
Feature Weighted Blending Bitwise Blending
Use Case Smooth transitions, overlays Logo insertion, transparency handling
Formula α ⋅ I1 + β ⋅ I2 + γ
Mask-based pixel selection
Transparency Not handled Yes, using masks
5. Common Errors
Shape mismatch → resize images before blending.
Mask size mismatch → mask must match ROI size.
Wrong color order → OpenCV uses BGR, not RGB.
III. Thresholding in OpenCV
Thresholding is used to convert a grayscale image into a binary image by applying a threshold value.
Each pixel is compared to this threshold, and based on the type of thresholding, it is either set to a
specified maximum value or modified accordingly.
Syntax
retval, thresholded_image = [Link](src, thresh, maxval, type)
src: Input image (must be grayscale).
thresh: Threshold value.
maxval: Maximum value assigned to the pixels that meet the threshold condition (used in binary
thresholding).
type: Type of thresholding. Common types:
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
retval: Threshold value used (can be ignored in simple cases).
thresholded_image: Output image after thresholding.
1. Thresholding Types with Examples
1. Binary Thresholding
Module II - Basic Image Processing Operations in OpenCV 5
If pixel value > threshold, set to maxval, else set to 0.
import cv2
import numpy as np
img = [Link]('gray_image.jpg', 0)
_, thresh_binary = [Link](img, 127, 255, cv2.THRESH_BINARY)
[Link]("Binary", thresh_binary)
[Link](0)
2. Binary Inverted
If pixel value > threshold, set to 0, else set to maxval.
_, thresh_binary_inv = [Link](img, 127, 255, cv2.THRESH_BINARY_INV)
3. Truncate
If pixel value > threshold, set to threshold, else keep original.
_, thresh_trunc = [Link](img, 127, 255, cv2.THRESH_TRUNC)
4. To Zero
If pixel value > threshold, keep original, else set to 0.
_, thresh_tozero = [Link](img, 127, 255, cv2.THRESH_TOZERO)
5. To Zero Inverted
If pixel value > threshold, set to 0, else keep original.
_, thresh_tozero_inv = [Link](img, 127, 255, cv2.THRESH_TOZERO_INV)
Threshold Type Formula Description
Output(x, y) =
Pixels > thresh →
{
cv2.THRESH_BINARY maxval, if pixel(x, y) > thresh
maxval; others → 0
0, otherwise
Output(x, y) = Inverted: Pixels >
{
cv2.THRESH_BINARY_INV 0, if pixel(x, y) > thresh thresh → 0; others →
maxval, otherwise maxval
Output(x, y) = Pixels > thresh →
{
cv2.THRESH_TRUNC thresh, if pixel(x, y) > thresh thresh; others remain
pixel(x, y), otherwise same
Module II - Basic Image Processing Operations in OpenCV 6
Threshold Type Formula Description
Output(x, y) =
Pixels > thresh →
{
cv2.THRESH_TOZERO pixel(x, y), if pixel(x, y) > thresh
unchanged; others → 0
0, otherwise
Output(x, y) = Inverted: Pixels >
{
cv2.THRESH_TOZERO_INV 0, if pixel(x, y) > thresh thresh → 0; others
pixel(x, y), otherwise remain same
2. Ways to Choose Threshold
(a) Manual / Fixed Threshold / Basic Global thresholding
User manually selects a threshold T.
Example: T = 127for an 8-bit grayscale image.
(b) Iterative Global Thresholding
One threshold value for the entire image.
Suitable when lighting conditions are uniform.
Algorithm (Iterative Thresholding):
1. Choose an initial threshold T (mean intensity is common).
2. Segment image:
Group 1: pixels with intensity > T
Group 2: pixels with intensity ≤ T
3. Compute mean intensities m1 , m2 of both groups.
4. Compute new threshold:
m1 + m2
Tnew =
2
5. Repeat steps 2–4 until ∣Tnew − Told ∣ < ϵ(small value).
(c) Adaptive Thresholding
When applying basic thresholding we had to manually supply a threshold value, T, to segment our
foreground and our background. However, basic thresholding methods are global thresholding
techniques, implying that the same value of T is used to test all pixels in the input image, thereby
segmenting them into foreground and background.
The problem here is that having just one value of T may not suffice. Due to variations in
lighting conditions, shadowing, etc., it may be that one value of T will work for a certain part of the
Module II - Basic Image Processing Operations in OpenCV 7
input image but will utterly fail on a different segment.
Adaptive thresholding is a method where the threshold value is not fixed for the entire image.
Instead, the threshold is calculated locally for each pixel based on the intensities of its neighboring
pixels.
It is particularly useful for images with:
Uneven illumination
Shadows
Varying contrast
Two common methods:
1. Adaptive Mean Thresholding
a. Threshold = Mean of the local neighborhood −C
∑i,j∈block I(i, j)
T (x, y) = −C
N
2. Adaptive Gaussian Thresholding
Threshold = Gaussian weighted sum of the local neighborhood −C
T (x, y) = ∑ G(i, j) ∗ I(i, j) − C
i,j∈block
Where G(i, j)is the Gaussian kernel.
A commonly used Gaussian kernel:
1 2 1
1
G= 16
2 4 2
1 2 1
where:
I(i, j): pixel intensity in the neighborhood
N : number of pixels in the block
C : constant subtracted to fine-tune the result
Parameters for Adaptive Thresholding (OpenCV)
[Link](src, maxValue, adaptiveMethod, thresholdType, blockSize, C)
src: Grayscale input image
maxValue: Value to assign to pixels exceeding the threshold (usually 255)
adaptiveMethod:
cv2.ADAPTIVE_THRESH_MEAN_C → Mean thresholding
Module II - Basic Image Processing Operations in OpenCV 8
cv2.ADAPTIVE_THRESH_GAUSSIAN_C → Gaussian weighted thresholding
thresholdType:
Usually cv2.THRESH_BINARY or cv2.THRESH_BINARY_INV
blockSize: Size of the local region (odd number: 3, 5, 7, 11...)
C: Constant subtracted from the mean or weighted mean
Example Code – Adaptive Thresholding
import cv2
import [Link] as plt
# Read image in grayscale
img = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
# Adaptive Mean Thresholding
th_mean = [Link](img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, blockSize=11, C=2)
# Adaptive Gaussian Thresholding
th_gaussian = [Link](img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN
_C,
cv2.THRESH_BINARY, blockSize=11, C=2)
# Display results
[Link](figsize=(12, 6))
[Link](1, 3, 1), [Link](img, cmap='gray'), [Link]('Original Image')
[Link](1, 3, 2), [Link](th_mean, cmap='gray'), [Link]('Adaptive Mean')
[Link](1, 3, 3), [Link](th_gaussian, cmap='gray'), [Link]('Adaptive Gaussia
n')
[Link]()
IV. Blurring or Smoothing in OpenCV
1. Introduction
Blurring (also called smoothing) is a key image preprocessing technique in computer vision used to:
Reduce image noise
Suppress fine details or textures
Improve the performance of edge detection and segmentation
Module II - Basic Image Processing Operations in OpenCV 9
Prepare images for further analysis
Gamma correction is a separate but related image intensity adjustment technique used to control the
overall brightness and contrast of an image in a non-linear manner.
2. Blurring and Smoothing
Blurring works by replacing each pixel value with a weighted combination of its neighboring pixels.
This is achieved through a convolution operation with a defined kernel.
The kernel determines how much influence each neighboring pixel has on the center pixel.
(a) Averaging (Mean Filter)
Simplest form of blurring.
The kernel is also called as box filter
Each pixel is replaced with the mean of its neighborhood.
Removes noise but also removes edges significantly.
Function:
blurred = [Link](image, (k, k))
(k, k) = kernel size (e.g., 3×3, 5×5).
Kernel example for 3×3:
1 1 1
1
9
1 1 1
1 1 1
(b) Gaussian Blurring
Uses a Gaussian kernel where pixels closer to the center have higher weights.
Produces a natural, visually pleasing blur.
Commonly used as a preprocessing step for edge detection.
Function:
blurred = [Link](image, (k, k), sigmaX=0,sigmaY=0)
k = kernel size (odd, e.g., 3×3, 5×5, 7×7)
sigmaX = standard deviation in the X direction.
Gaussian kernel example (3×3):
Module II - Basic Image Processing Operations in OpenCV 10
1 2 1
1
16 2 4 2
1 2 1
(c) Median Blurring
Each pixel is replaced with the median intensity of its neighborhood.
Very effective for salt-and-pepper noise.
Better edge preservation than mean filtering.
Function:
blurred = [Link](image, k)
k = kernel size (odd integer).
2.3 Comparison of Blurring Techniques
Method Noise Reduction Edge Preservation Common Uses
Averaging Moderate Poor General noise smoothing
Gaussian Blur High Moderate Preprocessing before edge detection
Median Blur High Good Salt-and-pepper noise removal
3. Gamma Correction
What is Gamma Correction?
Gamma correction is a non-linear intensity transformation applied to adjust the brightness and
contrast of an image.
Human vision does not perceive brightness linearly; it is closer to a power-law response. Gamma
correction compensates for this by applying:
Iout = 255 ∗ ( 255
)
Iin γ
Where:
Iin = input intensity (normalized between 0 and 1)
Iout = output intensity
γ = gamma value (positive real number)
Effects of Gamma Values
γ < 1→ Brightens the image (useful for dark images)
Module II - Basic Image Processing Operations in OpenCV 11
γ = 1→ No change
γ > 1→ Darkens the image (useful for overexposed images)
Implementation in OpenCV
import cv2
import numpy as np
import [Link] as plt
# Load image
image = [Link]('[Link]')
# Define gamma value
gamma = 2.2
# Apply gamma correction
gamma_corrected = [Link](255 * (image / 255) ** gamma, dtype=np.uint8)
# Display result
[Link]('Original', image)
[Link](gamma_corrected)
Applications of Gamma Correction
Adjusting images for display on monitors
Preprocessing medical or satellite images
Enhancing underexposed or overexposed images
Preparing data for machine learning models requiring normalized contrast
Module II - Basic Image Processing Operations in OpenCV 12