A. Image Gradients & First-Order Partial Derivatives
An edge is a significant local discontinuity in pixel brightness. In calculus, continuous edges correspond to extrema of the first derivative. For discrete 2D digital images, gradients are calculated along horizontal and vertical axes:
\[\nabla f = \begin{bmatrix} G_x \\ G_y \end{bmatrix} = \begin{bmatrix} \frac{\partial f}{\partial x} \\ \frac{\partial f}{\partial y} \end{bmatrix}\]
1. Gradient Magnitude: Measures edge strength at coordinate \((x, y)\):
\[M(x, y) = \|\nabla f\| = \sqrt{G_x^2 + G_y^2} \approx |G_x| + |G_y|\]
2. Gradient Direction (Orientation Angle): Orthogonal to the edge contour direction:
\[\theta(x, y) = \arctan\left(\frac{G_y}{G_x}\right)\]
B. Sobel Gradient Operators
The Sobel operator combines Gaussian smoothing with differential differentiation to provide noise resistance. It uses two orthogonal \(3 \times 3\) convolutional kernels:
\[S_x = \begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix} \quad (\text{Horizontal Gradient } G_x, \text{ Detects Vertical Edges})\]
\[S_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{bmatrix} \quad (\text{Vertical Gradient } G_y, \text{ Detects Horizontal Edges})\]
C. The 5-Stage Canny Edge Detection Pipeline
Developed by John F. Canny in 1986, the Canny edge detector is widely regarded as the optimal edge detector, satisfying three optimality criteria: low error rate, localized edge markers, and single-pixel response:
- Stage 1: Gaussian Filtering: Convolve input image with a Gaussian kernel (\(5 \times 5\), \(\sigma = 1.4\)) to suppress high-frequency image sensor noise.
- Stage 2: Gradient Intensity & Direction: Apply Sobel kernels \(S_x\) and \(S_y\) to compute gradient magnitude \(M(x, y)\) and quantize edge direction \(\theta\) into 4 discrete directions: horizontal (\(0^\circ\)), positive diagonal (\(45^\circ\)), vertical (\(90^\circ\)), and negative diagonal (\(135^\circ\)).
- Stage 3: Non-Maximum Suppression (NMS): Thins wide gradient ridges into 1-pixel wide crisp edges. For each pixel, compare its magnitude \(M(x, y)\) against the two adjacent neighbors along its gradient direction \(\theta\). If \(M(x, y)\) is not strictly greater than both neighbors, suppress it to \(0\).
- Stage 4: Double Thresholding: Classify remaining candidate pixels into three categories using two user-defined thresholds (\(T_{\text{High}}\) and \(T_{\text{Low}}\), typically ratio \(2:1\) or \(3:1\)):
• Strong Edges: \(M(x, y) \ge T_{\text{High}}\) (retained unconditionally).
• Weak Edges: \(T_{\text{Low}} \le M(x, y) < T_{\text{High}}\) (evaluated in Stage 5).
• Non-Edges: \(M(x, y) < T_{\text{Low}}\) (permanently suppressed to 0).
- Stage 5: Edge Tracking by Hysteresis: Weak edge pixels are preserved if and only if they are directly connected in an 8-connected neighborhood to at least one Strong edge pixel. Isolated weak edges caused by noise are eliminated.
Experiment 1: Image Acquisition, Loading, and Display
Aim: Write a Python script using OpenCV to read an image from disk, inspect its dimensions and data type, and display it in the vision canvas.
import cv2
# Read image in default BGR mode
img = cv2.imread('/input.jpg')
if img is not None:
height, width, channels = img.shape
print(f"Image Resolution: {width}x{height} | Channels: {channels} | Dtype: {img.dtype}")
cv2_imshow(img)
else:
print("Error: Could not load image from filesystem.")
Experiment 2: RGB to Grayscale Conversion
Aim: Convert a 3-channel BGR color image to single-channel 8-bit grayscale using ITU-R BT.601 luminous weighting.
import cv2
img = cv2.imread('/input.jpg')
# Convert BGR color space to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(f"Original shape: {img.shape} -> Grayscale shape: {gray.shape}")
cv2_imshow(gray)
Experiment 3: Gaussian Blur Smoothing & Noise Reduction
Aim: Apply a 2D Gaussian smoothing filter to suppress high-frequency image noise prior to edge detection.
import cv2
img = cv2.imread('/input.jpg')
# Gaussian Blur with 7x7 kernel and sigma = 1.5
blurred = cv2.GaussianBlur(img, (7, 7), sigmaX=1.5)
cv2_imshow(blurred)
Experiment 4: Binary Thresholding & Otsu Binarization
Aim: Segment foreground objects from background using automated Otsu variance thresholding.
import cv2
gray = cv2.cvtColor(cv2.imread('/input.jpg'), cv2.COLOR_BGR2GRAY)
# Automated Otsu binarization
optimal_thresh, binary_mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
print(f"Otsu Optimal Threshold calculated: {optimal_thresh:.1f}")
cv2_imshow(binary_mask)
Experiment 5: Canny Multi-Stage Edge Detection
Aim: Detect structural edge boundaries using the optimal 5-stage Canny algorithm with hysteresis thresholding.
import cv2
gray = cv2.cvtColor(cv2.imread('/input.jpg'), cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 1.2)
# Canny with Low Threshold = 50, High Threshold = 150
edges = cv2.Canny(blurred, threshold1=50, threshold2=150)
cv2_imshow(edges)
Experiment 6: Sobel Gradient Vector Field
Aim: Compute horizontal and vertical spatial derivatives using \(3 \times 3\) Sobel kernels.
import cv2
import numpy as np
gray = cv2.cvtColor(cv2.imread('/input.jpg'), cv2.COLOR_BGR2GRAY)
# 64-bit float gradients to capture negative slopes
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
# Combine gradient magnitudes
magnitude = cv2.magnitude(sobel_x, sobel_y)
sobel_combined = np.uint8(np.clip(magnitude, 0, 255))
cv2_imshow(sobel_combined)
Experiment 7: Contour Detection & Perimeter Analysis
Aim: Extract closed external contours from a binary image and calculate geometric properties.
import cv2
img = cv2.imread('/input.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(f"Total Contours Detected: {len(contours)}")
# Draw detected contours in green
annotated = img.copy()
cv2.drawContours(annotated, contours, -1, (0, 255, 0), 2)
cv2_imshow(annotated)
Experiment 8: Morphological Erosion & Dilation
Aim: Demonstrate noise reduction and boundary expansion using structuring elements.
import cv2
import numpy as np
gray = cv2.cvtColor(cv2.imread('/input.jpg'), cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
kernel = np.ones((5, 5), np.uint8)
# Opening (Erosion followed by Dilation) to remove background specks
opened = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
cv2_imshow(opened)
Experiment 9: Geometric Affine Transformation (Rotate & Scale)
Aim: Rotate an image by 45 degrees around its geometric center using an affine transformation matrix.
import cv2
img = cv2.imread('/input.jpg')
h, w = img.shape[:2]
center = (w // 2, h // 2)
# Compute 2x3 rotation matrix for 45 degrees rotation, scaled by 1.0
rot_matrix = cv2.getRotationMatrix2D(center, angle=45, scale=1.0)
rotated = cv2.warpAffine(img, rot_matrix, (w, h))
cv2_imshow(rotated)
Experiment 10: Drawing Bounding Boxes & Visual Annotations
Aim: Programmatically compute bounding boxes for detected objects and draw labeled overlays.
import cv2
img = cv2.imread('/input.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
canvas = img.copy()
for cnt in contours:
if cv2.contourArea(cnt) > 200:
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(canvas, (x, y), (x + w, y + h), (0, 165, 255), 2)
cv2.putText(canvas, f"Object ({w}x{h})", (x, y - 8),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 165, 255), 1)
cv2_imshow(canvas)
Standard University Viva Voce Examination Questions:
• Q: Why does OpenCV load images in BGR format instead of RGB?
A: Early computer vision libraries and camera framegrabber manufacturers in the 1990s stored framebuffer pixels in little-endian order (Blue, Green, Red). OpenCV preserved BGR to maintain backward compatibility.
• Q: Why must Sobel gradient filters use 64-bit float (cv2.CV_64F) rather than 8-bit unsigned integer (uint8)?
A: Light transitions from black to white yield positive derivatives (\(+255\)), but white-to-black transitions yield negative derivatives (\(-255\)). If stored in uint8, all negative slopes are truncated to zero, causing half of all image edges to vanish. Gradients must be computed in signed floats before taking absolute magnitudes.
• Q: What is the primary operational advantage of the Canny detector over the Sobel filter?
A: Sobel produces thick, fuzzy gradient ridges that vary with threshold. Canny incorporates Non-Maximum Suppression (NMS) to thin all edges to exactly 1 pixel width, and hysteresis double thresholding to prevent edge fragmentation.