Unit 4 Int345
Unit 4 Int345
Feature detection
• Feature detection is the process of checking the important features of
the image in this case features of the image can be edges, corners,
ridges, and blobs in the images.
• Feature detection is a fundamental concept in computer vision, which
refers to the process of identifying and extracting distinct and
informative patterns or structures from an image or a set of images.
• These features can represent key aspects of the image, such as edges,
corners, blobs, or even more complex structures like object parts or
shapes. Feature detection is crucial for various computer vision tasks,
including image recognition, object tracking, image stitching, and
more.
Can you match the scene points?
o Detection
o Description
o Matching
• Here, Ix and Iy are the gradients in the x and y directions, and w(x,y) is a weighting function (e.g., Gaussian
window) over the local neighborhood.
• Corner Response Function:
• Compute a corner response function to evaluate the likelihood of a pixel being a corner. Harris and
Stephens introduced the following corner response function:
• k is a constant typically set between 0.04 and 0.06. The det() function calculates the determinant, and
trace() calculates the trace of the structure tensor.
Sobel Operator for gradient Magnitude
• The Sobel operator uses convolution with two 3x3 kernels, one for detecting vertical edges
(Sobel_x) and the other for horizontal edges (Sobel_y).
• These kernels are used to compute the gradient of the image in the x and y directions,
respectively.
• Let's consider the following 3x3 grayscale image matrix:
| 100 150 200 |
| 120 180 220 |
| 80 110 140 |
• Sobel_x kernel:
-1 0 1
-2 0 2
-1 0 1
• Sobel_y kernel:
-1 -2 -1
0 0 0
X_gradient:
[[(-1*100 + 0*150 + 1*200) , (-2*100 + 0*150 + 2*200) , (-1*100 + 0*150 + 1*200)]
[(-1*120 + 0*180 + 1*220), (-2*120 + 0*180 + 2*220), (-1*120 + 0*180 + 1*220)]
[(-1*80 + 0*110 + 1*140) , (-2*80 + 0*110 + 2*140), (-1*80 + 0*110 + 1*140)]
Y_gradient:
(-1*100 - 2*150 - 1*200) (0*100 + 0*150 + 0*200) (1*100 + 2*150 + 1*200)
(-1*120 - 2*180 - 1*220) (0*120 + 0*180 + 0*220) (1*120 + 2*180 + 1*220)
(-1*80 - 2*110 - 1*140) (0*80 + 0*110 + 0*140) (1*80 + 2*110 + 1*140)
gradient_magnitude = sqrt((Sobel_x)^2 + (Sobel_y)^2)
• Corner Detection:
• Select pixels where R is above a certain threshold, indicating corners or
interest points in the image.
• Optionally, non-maximum suppression can be applied to retain only the local
maxima in the corner response.
• The Harris Corner Detector identifies regions where there are
significant variations in intensity in multiple directions, indicating the
presence of corners. It's important to note that Harris Corner
Detector is sensitive to noise and changes in scale and rotation.
Numerical Example
• Suppose we have gradients Ix=3 and Iy=5 for a specific pixel, and we
choose k=0.04 and consider threshold =0, identify given pixel is
corner point or not using harris corner operator.
Program Harris Corner Operator
import cv2
import numpy as np
# Threshold for an optimal value, it may vary depending on the image and the Harris detector parameter k
threshold = 0.01 * corners.max()
# Define the Hessian operator masks # Display the images or do further processing based on
mask_x = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) hessian_det
mask_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
# For visualization (optional)
cv2.imshow('Image', image.astype(np.uint8))
# Compute the second-order partial derivatives cv2.imshow('Ix', Ix)
Ix = cv2.filter2D(image, cv2.CV_64F, mask_x) cv2.imshow('Iy', Iy)
Iy = cv2.filter2D(image, cv2.CV_64F, mask_y) cv2.imshow('Ixx', Ixx)
cv2.imshow('Iyy', Iyy)
cv2.imshow('Ixy', Ixy)
cv2.imshow('Hessian Determinant',
hessian_det.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
FAST Features from Accelerated Segment
Test
• SURF is fast when compared to SIFT but not as fast to use with
real-time devices like mobile phones and surveillance cameras.
• So FAST algorithm was introduced with a very fast computing time.
• However FAST gives us only the key points and we may need to
compute descriptors with other algorithms like SIFT and SURF.
• With a Fast algorithm, we can detect corners and also blobs.
• https://docs.opencv.org/3.4/df/d0c/tutorial_py_fast.html
Syntax:
fast = cv2.FastFeatureDetector_create()
fast.setNonmaxSuppression(False)
kp = fast.detect(gray_img, None)
# Create a FAST detector object
fast = cv2.FastFeatureDetector_create()
Cell 1 (top-left):
•Gradient Directions: [135, 90, 135, 90] Cell 2 (top-right):
•Gradient Magnitudes: [5, 10, 5, 10] •Gradient Directions: [135, 90, 135, 90]
Histogram bins (0-20, 20-40, ..., 160-180 degrees): •Gradient Magnitudes: [5, 10, 5, 10]
•Bin 0-20 degrees: 0 (no magnitudes in this range) The histogram for Cell 2 is also [0, 0, 0, 0, 2, 0, 0, 2].
•Bin 20-40 degrees: 0
•Bin 40-60 degrees: 0
•Bin 60-80 degrees: 0
•Bin 80-100 degrees: 2 (from 90-degree gradients)
•Bin 100-120 degrees: 0
•Bin 120-140 degrees: 0
•Bin 140-160 degrees: 2 (from 135-degree gradients)
So, the histogram for Cell 1 is [0, 0, 0, 0, 2, 0, 0, 2].
Step 4: Normalize Histograms within Blocks (2x2)
Now, we group cells into 2x2 blocks and normalize the histograms within each block. In this simplified
example, we have only one block, which consists of Cell 1 and Cell 2. Normalization is often done using
techniques like L2 normalization.