0% found this document useful (0 votes)
89 views13 pages

Laboratory 4. Image Features and Transforms: 4.1 Hough Transform For Lines Detection

This document describes image features and transforms that can be used for computer vision tasks. It discusses the Hough transform for line and circle detection, as well as corner detection algorithms like Harris corner detector and Shi-Tomasi corner detector. It also introduces robust image features such as SIFT and ORB that can be used for tasks like image matching. Code examples are provided to detect lines in an image using Hough transform and circles using the HoughCircles function in OpenCV.

Uploaded by

Iulian Neaga
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)
89 views13 pages

Laboratory 4. Image Features and Transforms: 4.1 Hough Transform For Lines Detection

This document describes image features and transforms that can be used for computer vision tasks. It discusses the Hough transform for line and circle detection, as well as corner detection algorithms like Harris corner detector and Shi-Tomasi corner detector. It also introduces robust image features such as SIFT and ORB that can be used for tasks like image matching. Code examples are provided to detect lines in an image using Hough transform and circles using the HoughCircles function in OpenCV.

Uploaded by

Iulian Neaga
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
You are on page 1/ 13

Fundamentals of Image Processing and Computer Vision – Laboratory 4

Laboratory 4. Image features and transforms

Laboratory 4. Image features and transforms ................................................................................ 1

4.1 Hough transform for lines detection ...................................................................................... 1

4.1.1 Circle detection using the Hough transform ................................................................... 4

4.2. Harris corner detector ........................................................................................................... 5

4.2.1 Shi-Tomasi corner detector ............................................................................................. 8

4.3 Robust image features ............................................................................................................ 9

4.3.1 Scale Invariant Feature Transform (SIFT) .................................................................... 10


4.3.2 Oriented FAST and Rotated BRIEF (ORB).................................................................. 11

Image features are interesting points in an image, also called interest points or key points. They can
be useful in multiple computer vision application as image alignment or image matching.

4.1 Hough transform for lines detection


We can detect lines and circles in an image with the help of a technique called Hough transform.
The Hough transform is a feature extraction method for detecting simple shapes such as circles and lines.
A simple shape is one that can be represented by only a few parameters. For example, a line can be
represented by 2 parameters (slope, intercept) and a circle has three parameters — the coordinates of the
center and the radius (x, y, r). The Hough transform does an excellent job in finding such shapes in an
image. The main advantage of using the Hough transform is that it is insensitive to occlusion.

Figure 1. A line in polar coordinates

The polar form of a line is represented as:


𝜌 = 𝑥 𝑐𝑜𝑠(𝜃) + 𝑦 𝑠𝑖𝑛(𝜃) (1)

1
Fundamentals of Image Processing and Computer Vision – Laboratory 4

where 𝜌 represents the perpendicular distance of the line from the origin in pixels, and 𝜃 is the angle
(measured in radians) which the line makes with the origin as shown in Figure 1. A line in 2D space is
parameterized by 𝜌 and 𝜃 (if we pick any pair (𝜌, 𝜃), it corresponds to a line).
Let us imagine a 2D array where the x-axis has all possible 𝜃 values and the y-axis has all possible
𝜌 values. Any bin in this 2D array corresponds to one line. This 2D array is called an accumulator because
the bins of this array are used to collect evidence about the lines that exist in the image. The top left cell
corresponds to (−𝑅, 0) and the bottom right corresponds to (𝑅, 𝜋).

Figure 2. Accumulator for Hough transform

The value inside the bin (𝜌, 𝜃) will increase as more evidence is gathered about the presence of a line with
parameters 𝜌 and 𝜃. The following steps are performed to detect lines in an image:
Step 1: Initialize Accumulator - create an accumulator array. The number of cells is a design decision,
let’s assume choosing a 10×10 accumulator. It means that 𝜌 can take only 10 distinct values and 𝜃 can take
10 distinct values, so therefore we will be able to detect 100 different kinds of lines. The size of the
accumulator will also depend on the resolution of the image.
Step 2: Detect Edges - if there is a visible line in the image, an edge detector should fire at the boundaries
of that line. These edge pixels provide evidence for the presence of a line. The output of edge detection is
an array of edge pixels [(𝑥1 , 𝑦1 ) , (𝑥2 , 𝑦2 ), . .. , (𝑥𝑛 , 𝑦𝑛 )].
Step 3: Voting by Edge Pixels - For every edge pixel (𝑥𝑖 , 𝑦𝑖 ) in the above array, we vary the values of 𝜃
from 0 to π and plug it in equation (1) to obtain a value for 𝜌. Let us suppose an accumulator of size 20x20.
So, there are 20 distinct values of 𝜃 and so for every edge pixel (𝑥𝑖 , 𝑦𝑖 ), we can calculate 20 (𝜌, 𝜃) pairs by
using equation (1). The bin of the accumulator corresponding to these 20 values of (𝜌, 𝜃) is incremented.
That bin is determined by finding the intersection of all the curves generated by the edge pixels. As an
example, in Figure 3 we vary the parameter 𝜃 for 3 pixels ( represented by the 3 colored curves ), and obtain

2
Fundamentals of Image Processing and Computer Vision – Laboratory 4

the values for 𝜌 using equation (1). These curves intersect at a point indicating that a line with parameters
𝜃 = 1 and 𝜌 = 9.5 is passing through them (so this bin should be incremented).
Doing this for every edge pixel results in an accumulator that has all the evidence about all possible lines
in the image. We can simply select the bins in the accumulator above a certain threshold to find the lines in
the image. If the threshold is higher, fewer strong lines will be detected, and if it is lower, a large number
of lines will be detected, including some weak ones.

Figure 3. 3 edge pixels intersecting in the point 𝜃 = 1 and 𝜌 = 9.5 indicate the line to which they belong.

In OpenCV, line detection using Hough Transform is implemented in the function HoughLines
and HoughLinesP [Probabilistic Hough Transform], with the following syntax:

lines =cv2.HoughLinesP(img, rho, theta, threshold[, minLineLength[,


maxLineGap]]])
lines = cv2.HoughLines(img, rho, theta, threshold[, srn[, stn[, min_theta[,
max_theta]]]]])

The function’s parameters are:


img - 8-bit, single-channel binary source image. The image may be modified by the function.
lines - Output vector of lines. Each line is represented by a 2 or 3 element vector (𝜌, 𝜃) or (𝜌, 𝜃, 𝑣𝑜𝑡𝑒𝑠).
𝜌 is the distance from the coordinate origin (0,0) (top-left corner of the image). 𝜃 is the line
rotation angle in radians (0∼vertical line, π/2∼horizontal line) . 𝑣𝑜𝑡𝑒𝑠 is the value of accumulator.
rho - Distance resolution of the accumulator in pixels.
theta - Angle resolution of the accumulator in radians.
threshold - Accumulator threshold parameter. Only those lines are returned that get enough votes
(votes>threshold).
srn - For the multi-scale Hough transform, it is a divisor for the distance resolution rho. The coarse
accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn. If both

3
Fundamentals of Image Processing and Computer Vision – Laboratory 4

srn=0 and stn=0 , the classical Hough transform is used. Otherwise, both these parameters should
be positive.
stn - For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
min_theta - For standard and multi-scale Hough transform, minimum angle to check for lines. Must fall
between 0 and max_theta.
max_theta - For standard and multi-scale Hough transform, maximum angle to check for lines. Must fall
between min_theta and CV_PI.

The overall quality of detected lines depends heavily on the quality of the edge map. Therefore, in
real world situations, the Hough transform is used when the environment can be controlled and therefore
can lead to consistent edge maps, or when an edge detector can be trained for the specific kind of edges that
are searched for.

• Ex. 4.1 Read the image ‘highway.jpg’ and transform it to grayscale. Apply the Canny edge detector
and experiment with multiple types of thresholding, in order to extract mainly the road boundaries (less
boundaries from the vegetation). Use the function cv2.HoughLinesP to identify the lines in the
image. Draw the lines on the original image using a blue color (cv2.line) and display the results.

4.1.1 Circle detection using the Hough transform


In the case of line detection using the Hough transform, we required 2 parameters, (𝜌, 𝜃), but to
detect circles, we need 3 parameters: (𝑥, 𝑦) the coordinates of the circle’s center and the radius. A circle
detector will then require a 3D accumulator — one dimension for each parameter.
The equation of a circle is given by:
(𝑥 − 𝑥0 )2 + (𝑦 − 𝑦0 )2 = 𝑟 2
The following steps are needed to detect circles in an image:
1. Find the edges in the given image with the help of edge detectors (Canny).
2. To detect circles, set a threshold for the maximum and minimum value of the radius.
3. Evidence is collected in a 3D accumulator array for the presence of circles with different centers and
radii.
The function HoughCircles is used in OpenCV to detect the circles in a grayscale image.

circles = cv2.HoughCircles( image, method, dp, minDist[, param1[, param2[,


minRadius[, maxRadius]]]]])

with parameters:
image - 8-bit, single-channel binary source image. The image may be modified by the function.

4
Fundamentals of Image Processing and Computer Vision – Laboratory 4

circles - Output vector of found circles. Each vector is encoded as 3 or 4 element floating-point vector
(x,y,radius) or (x,y,radius,votes) .
method - Detection method. Currently, the only implemented method is HOUGH_GRADIENT
dp - Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1, the
accumulator has the same resolution as the input image. If dp=2, the accumulator has half as big
width and height.
minDist - Minimum distance between the centers of the detected circles. If the parameter is too small,
multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some
circles may be missed.
param1 - First method-specific parameter. In case of HOUGH_GRADIENT , it is the higher threshold of the
two passed to the Canny edge detector (the lower one is twice smaller).
param2 - Second method-specific parameter. In case of HOUGH_GRADIENT , it is the accumulator threshold
for the circle centers at the detection stage. The smaller it is, the more false circles may be detected.
Circles, corresponding to the larger accumulator values, will be returned first.
minRadius - Minimum circle radius.
maxRadius - Maximum circle radius. If <= 0, uses the maximum image dimension. If < 0, returns centers
without finding the radius.

❖ Ex. 4.2 Read the image ‘circles.jpg’ and transform it to grayscale. Apply a median blur filter on the
gray image and then detect Hough circles:

circles = cv2.HoughCircles(img_blur, cv2.HOUGH_GRADIENT, 1, 50, param1=450,


param2=10, minRadius=30, maxRadius=40)

Draw in green the detected circles. Experiment with other parameters values.

4.2. Harris corner detector


Intuitively, corners represent junctions of contours. Corners are considered features more stable
over variations in viewpoint, so they are good features for matching. A corner can be identified by searching
for large variations in the neighborhood of a point, in all directions. The main idea used in corner detection
is looking at intensity values within a small window, as suggested in Figure 4. Shifting the window in any
direction should yield a large change in appearance.

5
Fundamentals of Image Processing and Computer Vision – Laboratory 4

Figure 4. Corner detection based on neighborhood analysis

The basic idea in Harris corner detection is to identify the 3 possible situations displayed in Figure
5.

uniform (flat) area – no change edge – no change along the edge corner – significant change in
in all directions direction all directions
Figure 5. The Harris algorithm identifies each case mathematically.

The algorithm starts by analyzing the change in intensity for a shift displacement of (𝑢, 𝑣):

𝐸(𝑢, 𝑣) = ∑ 𝑤(𝑥, 𝑦) [𝐼(𝑥 + 𝑢, 𝑦 + 𝑣) − 𝐼(𝑥, 𝑦)]2 (2)


𝑥,𝑦

The window function 𝑤(𝑥, 𝑦) can be a simple 2D gate (1 inside the window, 0 outside) or a Gaussian
window (higher weight in the middle of the window, lower weights towards the boundaries). The sum of
squared differences (SSD) of intensities in equation 2 will be near 0 for a uniform area, and larger for a
distinctive patch (edge or corner). The search for corners becomes then a search for large 𝐸(𝑢, 𝑣) values.
Using a 1st order approximation from Taylor series, equation (2) becomes
2
𝐸(𝑢, 𝑣) ≈ ∑ 𝑤(𝑥, 𝑦) [𝐼(𝑥, 𝑦) + 𝑢𝐼𝑥 + 𝑣𝐼𝑦 − 𝐼(𝑥, 𝑦)]
𝑥,𝑦

𝐼𝑥2 𝐼𝑥 𝐼𝑦 𝑢
𝐸(𝑢, 𝑣) ≅ [𝑢 𝑣] (∑ [ 2 ]) [𝑣 ]
𝐼𝑥 𝐼𝑦 𝐼𝑦
𝑢
= [𝑢 𝑣] 𝑴 [ ]
𝑣
where the matrix 𝑴 is computed from the image derivatives
𝐼𝑥2 𝐼𝑥 𝐼𝑦 (3)
𝑴 = ∑ 𝑤(𝑥, 𝑦) [ ]
𝐼𝑥 𝐼𝑦 𝐼𝑦2
𝑥,𝑦

The points in the image will then be classified using the eigenvalues 𝜆1 , 𝜆2 of matrix 𝑴:

6
Fundamentals of Image Processing and Computer Vision – Laboratory 4

- uniform region: 𝜆1 and 𝜆2 are small, 𝐸 is almost constant in all directions


- edge: 𝜆1 ≫ 𝜆2 or 𝜆2 ≫ 𝜆1
- corner: 𝜆1 and 𝜆2 are both large, 𝐸 increases in all directions
The corner response measure is introduced:
𝑅 = 𝑑𝑒𝑡 𝑴 − 𝑘(𝑡𝑟𝑎𝑐𝑒 𝑴)2
where: 𝑑𝑒𝑡 𝑴 = 𝜆1 𝜆2 and 𝑡𝑟𝑎𝑐𝑒 𝑴 = 𝜆1 + 𝜆2 . The constant value 𝑘 is chosen between 0.04 and 0.06.
According to the corner measure, we will then have areas:
- uniform, |𝑅| is small
- with an edge, 𝑅 is negative with a large magnitude
- with a corner, 𝑅 is large.
The Harris corner detection algorithm has the following steps:
Step 1. Compute the image gradients 𝐼𝑥 and 𝐼𝑦 . Compute products of derivatives at every pixel: 𝐼𝑥2 , 𝐼𝑦2 , and
𝐼𝑥 𝐼𝑦 .
Step 2. Compute the sums of products for derivatives at every pixel: 𝑆𝑥2 = ∑ 𝐼𝑥2 , 𝑆𝑦2 = ∑ 𝐼𝑦2 , and 𝑆𝑥𝑦 =
∑ 𝐼𝑥 𝐼𝑦
Step 3. Define the matrix 𝐻 at each pixel
𝑆𝑥2 𝑆𝑥𝑦
𝑯=[ ]
𝑆𝑥𝑦 𝑆𝑦2
Step. 4 Compute the response of the detector at each pixel
2
𝑅 = 𝐷𝑒𝑡(𝐻) − 𝑘(𝑇𝑟𝑎𝑐𝑒(𝐻))
Step 5. Threshold the value of 𝑅. Perform non-maximum suppression.
Remark! The Harris detector is not scale-invariant: a corner may become an edge if the scale
changes!
The OpenCV function that implements Harris algorithm is

dst = cv2.cornerHarris( src, blockSize, ksize, k[, dst[, borderType]])

with parameters:

src Input single-channel 8-bit or floating-point image.


dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same size as src .
blockSize Neighborhood size.
ksize Aperture parameter for the Sobel operator.
k Harris detector free parameter. See the formula above.
borderType Pixel extrapolation method.

7
Fundamentals of Image Processing and Computer Vision – Laboratory 4

❖ Ex. 4.3 Read the input image table.jpg, convert it to grayscale. The gray image must be on float32.
Apply Harris corner detector and dilate the output image to make the corners more visible. Analyze the
detected corners depending on the selected threshold value.

dst = cv2.cornerHarris(gray, 2, 3, 0.04)


dst = cv2.dilate(dst,None)

# This value vary depending on the image and how many corners you want to detect
# Try changing this free parameter, 0.1, to be larger or smaller and see what
happens
thresh = 0.1*dst.max()

# Create an image copy to draw corners on


corner_image = np.copy(img)

# Iterate through all the corners and draw them on the image (if they pass the
threshold)
for j in range(0, dst.shape[0]):
for i in range(0, dst.shape[1]):
if(dst[j,i] > thresh):
# image, center pt, radius, color, thickness
cv2.circle( corner_image, (i, j), 1, (0,255,0), 1)
plt.figure()
plt.imshow(corner_image)

4.2.1 Shi-Tomasi corner detector


J. Shi and C. Tomasi made a small modification to the Harris algorithm. They replaced the response
of the detector
𝑅 = 𝜆1 𝜆2 − 𝑘(𝜆1 + 𝜆2 )2
with
𝑅 = 𝑚𝑖𝑛{𝜆1 , 𝜆2 }
If 𝑅 is greater then a threshold value, then it is considered a corner. OpenCV has the function
cv2.goodFeaturesToTrack that finds N strongest corners in the image by Shi-Tomasi method (or Harris
Corner Detection, if specified).

corners = cv2.goodFeaturesToTrack(image, maxCorners, qualityLevel,


minDistance[, mask[, blockSize[, useHarrisDetector[, k]]]]] )

corners = cv2.goodFeaturesToTrack(image, maxCorners, qualityLevel,


minDistance, mask, blockSize, gradientSize[, useHarrisDetector[, k]]]

The function’s parameters are:


image Input 8-bit or floating-point 32-bit, single-channel image.
corners Output vector of detected corners.

8
Fundamentals of Image Processing and Computer Vision – Laboratory 4

maxCorners Maximum number of corners to return. If there are more corners than are found, the
strongest of them are returned. maxCorners <= 0 implies that no limit on the maximum is set and
all detected corners are returned.
qualityLevel Parameter characterizing the minimal accepted quality of image corners. The
parameter value is multiplied by the best corner quality measure, which is the minimal eigenvalue
(see cornerMinEigenVal ) or the Harris function response (see cornerHarris ). The corners
with the quality measure less than the product are rejected. For example, if the best corner has the
quality measure = 1500, and the qualityLevel=0.01 , then all the corners with the quality
measure less than 15 are rejected.
minDistance Minimum possible Euclidean distance between the returned corners. The function throws
away each corner for which there is a stronger corner at a distance less than maxDistance
mask Optional region of interest. If the image is not empty (it needs to have the type CV_8UC1 and the
same size as image ), it specifies the region in which the corners are detected.
blockSize Size of an average block for computing a derivative covariation matrix over each pixel
neighborhood.
useHarrisDetector Parameter indicating whether to use a Harris detector (see cornerHarris) or
cornerMinEigenVal.
k Free parameter of the Harris detector.

❖ Ex. 4.4 Review exercise 4.3 using the Shi-Tomasi corner detector. Compare the results.

4.3 Robust image features


There are three main algorithms most used in OpenCV to extract image features: SIFT, SURF and
ORB. An image feature at this stage is like the DNA of the image. Image features are used to align
documents, create panorama images, find known objects in images, and other computer vision algorithms.
In order to be able to match corresponding points between distinct images, we should identify the
most interesting points and try to match them. Such points are usually corners. A feature has 2 components:
a detector and a descriptor. The detector outputs the location of the feature, so it is designed to look for
corners. A descriptor describes the region around the feature, so we can match a feature in one image to a
feature in another image. The descriptor outputs a vector that serves as a signature for the region containing
the feature.

9
Fundamentals of Image Processing and Computer Vision – Laboratory 4

4.3.1 Scale Invariant Feature Transform (SIFT)


SIFT was released in 2004 by researcher D. Lowe and it was used on a large scale in computer
vision applications. The algorithm is quite slow, compared to SURF and ORB, still it remains the starting
point for matching features across different images. SIFT is not only scale invariant, but also invariant to
rotation, illumination and viewpoint.
Step 1. Constructing a scale space. This is the initial preparation, when the image is subsequently
blurred with Gaussian convolution using increasing standard deviation, and then downsampled by 2. The
process is repeated until the image is too small to continue, thus simulating different scales of observation
and suppressing fine-scale structures. The blurred images from one scale are called octave. The collection
of downsampled images are called a Gaussian pyramid.
Step 2. LoG Approximation. The Laplacian of the scale-space images is great for finding interesting
points (or key points) in an image. Since it is computationally expensive, it is approximated by Difference
of Gaussians.
Step 3. Finding keypoints. The keypoints are maxima and minima in the Difference of Gaussian
images calculate in step 2.
Step 4. Remove bad key points. Edges and low contrast regions are bad keypoints. Eliminating
these makes the algorithm efficient and robust – only strong points remain. A technique similar to the Harris
Corner Detector is used here.
Step 5. Assigning an orientation to the keypoints. An orientation is calculated for each key point,
based on all gradients in the direct neighborhood of such a point: if many of them have approximately the
same direction, that will be assigned to the keypoint. Any further calculations are done relative to this
orientation, making it rotation invariant.
Step 6. Generate a SIFT descriptor. Finally, each keypoint has a location, scale, orientation. A
descriptor is computed for the local 16x16 image neighborhood around the keypoint. It is divided into 16
sub-blocks of 4x4 size. For each sub-block, an 8-bin orientation histogram is created. So, a total of 128 bin
values are available. It is represented as a vector to form a keypoint descriptor. In addition, several measures
are taken to achieve robustness against illumination changes and rotation.
SIFT is available in OpenCV through the functions:

❖ siftObject = cv2.SIFT_create( [, nfeatures[, nOctaveLayers[,


contrastThreshold[, edgeThreshold[, sigma]]]]] )

with the following parameters. Generally, their default values work best, so the function is normally
called without input arguments.

10
Fundamentals of Image Processing and Computer Vision – Laboratory 4

nfeatures The number of best features to retain. The features are ranked by their scores (measured in
SIFT algorithm as the local contrast)
nOctaveLayers The number of layers in each octave. 3 is the value used in D. Lowe paper. The
number of octaves is computed automatically from the image resolution.
contrastThreshold The contrast threshold used to filter out weak features in semi-uniform (low-
contrast) regions. The larger the threshold, the less features are produced by the detector.
edgeThreshold The threshold used to filter out edge-like features. Note that the its meaning is
different from the contrastThreshold, i.e. the larger the edgeThreshold, the less features are filtered
out (more features are retained).
sigma The sigma of the Gaussian applied to the input image at the octave #0. If your image is captured
with a weak camera with soft lenses, you might want to reduce the number.

❖ siftObject.detect is a function that finds the keypoints in an image. Each keypoint is a special
structure whith many attributes like its (x,y) coordinates, size of the meaningful neighborhood, angle
which specifies its orientation, response that specifies strength of keypoints etc.
❖ cv2.drawKeyPoints – function which draws small circles on the locations of the keypoints.
❖ siftObject.compute – computes descriptors from the keypoints found by siftObject.detect.
❖ siftObject.detectAndCompute – directly find keypoints and compute descriptors in a single step.

• Ex. 4.5 Read the image mac3.jpg, convert it to grayscale and display both images. Initiate a SIFT
detector object. Find the keypoints with SIFT and compute the descriptor. Draw the keypoints
location on the original color image. Analyze the first 70, 80,… features generated by a SIFT
detector, observe their locations! How many features are detected by default for this image?
• Ex. 4.6 Experiment also with the images mac1.jpg and mac2.jpeg, allowing the detectors to track
as many features as possible.

4.3.2 Oriented FAST and Rotated BRIEF (ORB)


ORB was released in 2011, it is faster than SIFT and SURF and also free for commercial use (SIFT
and SURF are patented). An ORB feature contains also a detector and a descriptor. The detector inside
ORB is called FAST (Features from Accelerated Segment Test). The descriptor used in ORB is called
BRIEF (Binary Robust Independent Elementary Features).
Step 1. To start with, it uses FAST algorithm to examine a patch around every pixel and check for
a potential feature point. After locating the keypoints, then apply Harris corner measure to find top N points
among them. ORB also uses Gaussian pyramids to produce multiscale-features.

11
Fundamentals of Image Processing and Computer Vision – Laboratory 4

Step 2. Calculate the feature orientation using Centroid. It computes the intensity weighted centroid
of the patch with a located corner at the center. The direction of the vector from this corner point to the
centroid gives the orientation. To improve the rotation invariance, moments are computed with x and y
which should be in a circular region of radius r, where r is the size of the patch.
Step 3. Use BRIEF to compute a descriptor. The points used to compute BRIEF are rotated based
on the orientation of the patch containing the feature. This makes the descriptor invariant to rotation.

The OpenCV function needed to create an ORB detector is cv2.ORB_create with a similar set
of parameters as cv2.SIFT_create. Generally, the parameters default values work best, so the function
is normally called without input arguments.

orbObject = cv2.ORB_create()

The newly created object, orbObject, has the following important functions:

❖ keypoints = orbObject.detect(image[, mask] )

where
image – input image.
keypoints - The detected keypoints.
mask - Mask specifying where to look for keypoints (optional). It must be a 8-bit integer matrix with non-
zero values in the region of interest.

❖ keypoints, descriptors = orbObject.compute(image, keypoints[, descriptors])

keypoints - Input collection of keypoints. Keypoints for which a descriptor cannot be computed are
removed. Sometimes new keypoints can be added, for example: SIFT duplicates keypoint with
several dominant orientations (for each orientation).
descriptors - Computed descriptors.

❖ keypoints, descriptors = orbObject.detectAndCompute(image, mask[,


descriptors[, useProvidedKeypoints]] )
❖ outImage = cv2.drawKeypoints(image, keypoints, outImage[, color[, flags]] )

image - Source image.


keypoints - Keypoints from the source image.
outImage - Output image. Its content depends on the flags value defining what is drawn in the output
image. See possible flags bit values below.
color - Color of keypoints.

12
Fundamentals of Image Processing and Computer Vision – Laboratory 4

flags - Flags setting drawing features. Possible flags bit values are defined by DrawMatchesFlags.

• Ex. 4.7 Read the images cvbook1.jpg and cvbook2.jpg, then convert both to grayscale and display
them. Initiate 2 ORB detector objects. Find the keypoints with ORB and compute the descriptors. Draw the
keypoints location, size and orientation on the original color images. Compare both output images and
comment on the features locations. Compare also the first 100 detected features from both images.
• Ex. 4.8 For the same input images from exercise 4.6, add a SIFT detector object, find the keypoints
with SIFT and compute the descriptor. Draw the keypoints location on the original color images and
compare SIFT features with ORB features. How many features are detected by each algorithm? What are
the descriptors sizes? How long does it take for each algorithm to detect and compute ? Use time:

import time

t0 = time.time()
# processing
t1 = time.time()

total = t1-t0

13

You might also like