0% found this document useful (0 votes)
6 views11 pages

Introduction to Object Detection Techniques

The document provides an overview of object detection in computer vision, explaining its definition and differentiation from classification and localization. It covers techniques such as template matching, corner detection, Canny edge detection, grid detection, and contour detection, including their syntax and applications. Each section includes code snippets demonstrating how to implement these techniques using OpenCV.

Uploaded by

Geetha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Introduction to Object Detection Techniques

The document provides an overview of object detection in computer vision, explaining its definition and differentiation from classification and localization. It covers techniques such as template matching, corner detection, Canny edge detection, grid detection, and contour detection, including their syntax and applications. Each section includes code snippets demonstrating how to implement these techniques using OpenCV.

Uploaded by

Geetha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

📝 Introduction to Object Detection

​ What is Object Detection?

Object Detection is a computer vision task where the system identifies what objects are
present in an image and where they are located. It outputs bounding boxes (or masks) with
class labels and confidence scores.

📚 1. Classification Vs Localisation Vs Detection


Object Classification: Only tells what is in the image.
Object Localisation: Only tells where one object is.
Object Detection: Combines both — detects and localises multiple
objects simultaneously.

📚 2. Template Matching
​ Template Matching:

Template Matching is used to locate a smaller image (template) within a larger image.
It works by sliding the template across the main image and calculating a similarity score
at each position.
Produces a heatmap of matching scores, where the highest score corresponds to the
best match.
Limitations: Sensitive to rotation, scaling, and illumination changes.

​ Syntax

[Link](image, template, method)

Parameters

image → Input image (larger scene).


template → Smaller image (object to find).
method → Matching method (commonly used ones):
cv2.TM_CCOEFF_NORMED ✅ Most popular (normalized correlation coefficient).
cv2.TM_CCOEFF , cv2.TM_CCORR , cv2.TM_CCORR_NORMED , cv2.TM_SQDIFF ,
cv2.TM_SQDIFF_NORMED

Returns
result → Similarity map (grayscale values, higher = better match).

Code snippet

import cv2
import [Link] as plt

# Load main image and template


img = [Link]("[Link]", 0) # Larger image
template = [Link]("[Link]", 0) # Template to search

#Apply template matching


result = [Link](img, template, cv2.TM_CCOEFF_NORMED) # Apply
template matching
min_val, max_val, min_loc, max_loc = [Link](result) # Find best match
location

#Corners of the matched location


w, h = [Link][::-1]
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)

# Draw rectangle around detected region


detected = [Link](img, cv2.COLOR_GRAY2BGR)
[Link](detected, top_left, bottom_right, (0, 255, 0), 2)

# Show results as plots


[Link](1,2,1); [Link]("Template"); [Link](template, cmap="gray")
[Link](1,2,2); [Link]("Detected Object"); [Link](detected,
cmap="gray")
[Link]()

📚 2. Corner Detection
​ Corner Detection:

Corners are points in an image where the intensity changes sharply in two or more
directions.
Corners are important for tracking, object recognition, motion detection, and image
registration.
OpenCV provides Harris Corner Detection and Shi-Tomasi Corner Detection (Good
Features to Track).

​ Harris Corner Detection

Based on the structure tensor (second-moment matrix):


2
Ix Ix Iy
M = [ ]
2
Ix Iy Iy

where I x, Iy are image gradients.


Harris response function:
2
R = det(M ) − k ⋅ (trace(M ))

If R is large positive → corner.


If R is negative → edge.
If R is small → flat region.
Typical constant: k ∈ [0.04, 0.06].

​ Shi-Tomasi (Good Features to Track)

Improves Harris by considering the minimum eigenvalue of (M).

R = min(λ 1 , λ 2 )

Corner if (R) is above a threshold.


Advantage: More stable and accurate than Harris.

​ Syntax

Harris Corner Detection

[Link](src, blockSize, ksize, k)

Parameters:

src → Grayscale input image.


blockSize → Neighborhood size for corner detection.
ksize → Aperture parameter for Sobel operator (for gradients).
k → Harris detector free parameter (0.04–0.06).

Returns: Corner response map.

Shi-Tomasi (Good Features to Track)

[Link](image, maxCorners, qualityLevel, minDistance)

Parameters:

image → Grayscale input image.


maxCorners → Maximum number of corners to return.
qualityLevel → Minimum accepted quality (0–1).
minDistance → Minimum distance between detected corners.
Returns: Array of detected corner points.

import cv2
import numpy as np
import [Link] as plt

# Load image and convert to grayscale


img = [Link]("[Link]")
gray = [Link](img, cv2.COLOR_BGR2GRAY) #gray scale image is required

# ---------------- Harris Corner Detection ----------------


harris = [Link](gray, blockSize=2, ksize=3, k=0.04) # returns corner
response map
img_harris = [Link]()
img_harris[harris > 0.01 * [Link]()] = [0, 0, 255] # mark corners in red
after thresholding

# ---------------- Shi-Tomasi Detection ----------------


corners = [Link](gray, maxCorners=50, qualityLevel=0.01,
minDistance=10) #returns array of corners
corners = np.int32(corners)
img_shi = [Link]()
for c in corners:
x, y = [Link]() #flatten the array
[Link](img_shi, (x, y), 3, (0, 255, 0), -1) # mark corners in green

# Show results
[Link](1,3,1); [Link]("Original"); [Link]([Link](img,
cv2.COLOR_BGR2RGB))
[Link](1,3,2); [Link]("Harris Corners");
[Link]([Link](img_harris, cv2.COLOR_BGR2RGB))
[Link](1,3,3); [Link]("Shi-Tomasi Corners");
[Link]([Link](img_shi, cv2.COLOR_BGR2RGB))
[Link]()

Applications

Object recognition and tracking.


Motion detection.
Panorama stitching (feature extraction).
Robotics and SLAM (Simultaneous Localization and Mapping).

📚 3. Canny Edge Detection


​ Motivation: Why Canny?

A good edge detector must satisfy three requirements:

1. Good Detection → Detect real edges, minimize false edges.


Sobel Y kernel:

Compute gradients:

Gradient magnitude:

Gradient direction:
G =

Sy =

⎢⎥
2. Good Localization → Edges should be as close as possible to actual edges.
3. Minimal Response → Only one response per edge (avoid double edges).

🔹 The Canny Edge Detector achieves this through a 5-step process.

Steps in Canny Edge Detection

Step 1: Noise Reduction (Gaussian Blur)

Requirement: Reduce noise to avoid false edges.


Kernel example (3×3, σ=1):

16


−1

Gx = I ∗ Sx ,
1

Effect: Smooths the image before edge detection.

Step 2: Gradient Calculation (Sobel Filters)

−1

+1

θ = arctan (

Effect: Finds both edge strength and orientation.

Step 3: Non-Maximum Suppression (NMS)

Requirement: Minimal response (thin edges).


Keep only the local maximum in gradient direction.
Effect: Produces 1-pixel thin edges.


1

Requirement: Detect intensity changes (potential edges).


Sobel X kernel:

Sx =

−1

−2

2
G = √G x + G y
0

−2

+2
2

Gy

Gx
1

+1

+2

+1

−1

+1

Gy = I ∗ Sy

)


Step 4: Double Thresholding

Requirement: Distinguish strong vs weak edges.


Strong edges → above high threshold.
Weak edges → between low and high threshold.
Suppressed → below low threshold.
Effect: Reduces false edges while keeping candidates.

Step 5: Edge Tracking by Hysteresis


Requirement: Ensure continuous edges.
Weak edges connected to strong edges are preserved. (See the above diagram)
Effect: Final clean edge map.

​ Syntax

[Link](image, threshold1, threshold2)

Parameters

image → Grayscale input image.


threshold1 → Lower threshold of canny edge detector's hysteresis
threshold2 → Upper threshold of canny edge detector hysteresis

Returns

Binary edge map (edges = white, background = black).

import cv2
import [Link] as plt

# Load and convert to grayscale


img = [Link]("[Link]")
gray = [Link](img, cv2.COLOR_BGR2GRAY)

# Step 1: Gaussian blur to reduce noise


blur = [Link](gray, (5,5), 1.4)

# Step 2–5: Canny Edge Detection


edges = [Link](blur, threshold1=100, threshold2=200)

# Show results
[Link](1,2,1); [Link]("Original"); [Link]([Link](img,
cv2.COLOR_BGR2RGB))
[Link](1,2,2); [Link]("Canny Edges"); [Link](edges, cmap="gray")
[Link]()

🎯 Applications
Detecting object boundaries.
Preprocessing for contour detection.
Used in medical imaging, OCR preprocessing, lane detection.

📚 4. Grid Detection (Chessboard & Circle Grids)


​ Grid Detection:

Grid patterns are widely used in computer vision for camera calibration and geometric
alignment.
Two main grid types in OpenCV:
1. Chessboard Grids → detect inner corners of checkerboard squares.
2. Circle (Dot) Grids → detect centers of circular dots, either symmetric or
asymmetric.

Why Grid Detection in Certain Tasks (such as Camera Calibration)

Motivation:
Camera calibration requires precisely located feature points.
Grids provide a known geometric structure that is easy to detect.
Requirements for good calibration targets:
High contrast (black–white or dots on bright background).
Regular spacing.
Easy to localize corners or dot centers.

​ Syntax

Chessboard Grid Detection

[Link](image, patternSize, flags)


Parameters:

image → Grayscale image.


patternSize → Number of inner corners per row and column (e.g., (7,7)).
flags → Optional (e.g., cv2.CALIB_CB_ADAPTIVE_THRESH ).

Returns: ret, corners → True if grid is detected + array of detected corners.

Circle Grid Detection

[Link](image, patternSize, flags)

Parameters:

patternSize → (cols, rows) in the grid.


flags :
cv2.CALIB_CB_SYMMETRIC_GRID → Regular dot grid.
cv2.CALIB_CB_ASYMMETRIC_GRID → Staggered hexagonal dot grid.

Returns: ret, centers → True if grid is detected + array of detected dot centers.

Code Snippet

import cv2
import [Link] as plt

# ---------------- Chessboard Grid ----------------


img = [Link]("[Link]")
gray = [Link](img, cv2.COLOR_BGR2GRAY)
pattern_size = (7, 7) # inner corners
ret, corners = [Link](gray, pattern_size, None)
img_chess = [Link]()
if ret:
[Link](img_chess, pattern_size, corners, ret)

# ---------------- Circle Grid (Symmetric) ----------------


img2 = [Link]("[Link]")
gray2 = [Link](img2, cv2.COLOR_BGR2GRAY)
pattern_size = (4, 11) # example size
ret2, centers = [Link](gray2, pattern_size,
flags=cv2.CALIB_CB_SYMMETRIC_GRID)
img_circle = [Link]()
if ret2:
[Link](img_circle, pattern_size, centers, ret2)

# Show results
[Link](1,2,1); [Link]("Chessboard Grid");
[Link]([Link](img_chess, cv2.COLOR_BGR2RGB))
[Link](1,2,2); [Link]("Circle Grid");
[Link]([Link](img_circle, cv2.COLOR_BGR2RGB))
[Link]()

🎯 Applications
Chessboard Grids → Camera calibration, 3D pose estimation.
Circle Grids → Calibration targets, biology (well plates), robotics.

📚 5. Contour Detection
​ Contour Detection:

Contours are continuous curves that join all points with the same intensity (boundaries of
objects).
Useful for object detection, shape analysis, counting, segmentation.
In OpenCV, contours are detected from a binary image (after thresholding or edge
detection).

​ Syntax

contours, hierarchy = [Link](image, mode, method)

Parameters

image → Binary (thresholded or edge-detected) image.


mode → Contour retrieval mode (defines how contours and hierarchy are retrieved):
cv2.RETR_EXTERNAL
Retrieves only the outermost contours.
Ignores child/inner contours.
Example: Detecting only the outlines of coins, ignoring holes.
cv2.RETR_LIST
Retrieves all contours.
Does not build parent–child relationships (no hierarchy).
Example: Detecting all objects as a flat list.
cv2.RETR_TREE
Retrieves all contours.
Builds a full hierarchy of nested contours (parent–child relationships).
cv2.RETR_CCOMP
Retrieves all contours and organizes them into two levels only:
Outer boundary → Level 1.
Holes/inside contours → Level 2.
Example: Segmentation tasks where only 2-level hierarchy is needed.
method → Contour approximation method:
cv2.CHAIN_APPROX_NONE → Stores all contour points (heavy, detailed).
cv2.CHAIN_APPROX_SIMPLE → Stores only endpoints of straight segments
(saves memory).

Returns

contours → List of contour points.


hierarchy → NumPy array with info about contour relationships.

​ Contour Hierarchy

The hierarchy array has shape (1, N, 4), where N = number of contours.
For each contour i , hierarchy[0][i] = [Next, Previous, First_Child, Parent]

Next → Index of the next contour at the same hierarchical level.


Previous → Index of the previous contour at the same level.
First_Child → Index of the first child contour.
Parent → Index of the parent contour.

Example:

hierarchy = [ [1, -1, -1, -1],


[2, 0, -1, 3],
[-1, 1, -1, -1] ]

Contour 0 → Next contour is 1, Previous - none, no child and no parent --> external
contour
Contour 1 → Next is 2, previous 0, child = none, parent is 3

🧑‍💻 Code Snippet


import cv2
import [Link] as plt

# Load image
img = [Link]("[Link]")
gray = [Link](img, cv2.COLOR_BGR2GRAY)

# Threshold
_, thresh = [Link](gray, 127, 255, cv2.THRESH_BINARY)

# Find contours with hierarchy


contours, hierarchy = [Link](thresh, cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_SIMPLE)

# Print hierarchy info


print("Hierarchy values:\n", hierarchy)

# Draw contours
img_contours = [Link]()
[Link](img_contours, contours, -1, (0,255,0), 2) # -1 = all contours,
2 = line thickness

# Show results
[Link](1,2,1); [Link]("Thresholded"); [Link](thresh, cmap="gray")
[Link](1,2,2); [Link]("Contours with Hierarchy");
[Link]([Link](img_contours, cv2.COLOR_BGR2RGB))
[Link]()

🎯 Applications
Object detection (coins, cells, shapes).
Shape analysis (contour area, perimeter, convex hull).
Image segmentation (separating objects from background).
Hierarchy-based detection (detecting objects inside objects, e.g., holes).

You might also like