You are on page 1of 4

Aldel Education Trust’s

St. John College of Engineering and Management, Palghar


(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING

Experiment 08
Title: Retrieving Images and Searching.
Aim: Using Image Descriptors, Feature detection algorithms, Defining features,
Detecting features – corners, Feature extraction and description using DoG and SIFT,
Anatomy of a keypoint
Theory :
Similar to the human eyes and brain, OpenCV can detect the main features of an image and extract
these into so-called image descriptors. These features can then be used as a database, enabling
image-based searches. Moreover, we can use key points to stitch images together and compose a
bigger image (think of putting together many pictures to form a 360-degree panorama).
Feature detection algorithms:
There are a number of algorithms that can be used to detect and extract features,
and we will explore most of them. The most common algorithms used in OpenCV
are as follows:
● Harris: This algorithm is useful to detect corners
● SIFT: This algorithm is useful to detect blobs
● SURF: This algorithm is useful to detect blobs
● FAST: This algorithm is useful to detect corners
● BRIEF: This algorithm is useful to detect blobs
● ORB: This algorithm stands for Oriented FAST and Rotated BRIEF

Matching features can be performed with the following methods:


● Brute-Force matching
● FLANN-based matching
Spatial verification can then be performed with homography.

Defining features:
Broadly speaking, a feature is an area of interest in the image that is unique or easily
recognizable. As you can imagine, corners and high-density areas are good features, while
patterns that repeat themselves a lot or low-density areas (such as a blue sky) are not. Edges
are good features as they tend to divide two regions of an image. A blob (an area of an
image that greatly differs from its surrounding areas) is also an interesting feature.
Most feature detection algorithms revolve around the identification of corners, edges, and
blobs, with some, also focusing on the concept of a ridge, which you can conceptualize as
the symmetry axis of an elongated object (think, for example, about
identifying a road in an image).

Om Bhamare BE B 05
Machine Vision Lab
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING
Some algorithms are better at identifying and extracting features of a certain type, so it's
important to know what your input image is so that you can utilize the best tool in your
OpenCV belt.

Feature extraction and description using DoG and SIFT:


The preceding technique, which uses cornerHarris, is great to detect corners and
has a distinct advantage because corners are corners; they are detected even if the
image is rotated.
However, if we reduce (or increase) the size of an image, some parts of the image
may lose or even gain a corner quality.

You will notice how corners are a lot more condensed; however, we didn't only gain
corners, we also lost some! In particular, look at the Variante Ascari chicane that squiggles
at the end of the NW/SE straight part of the track. In the larger version of the image, both
the entrance and the apex of the double bend were detected as corners. In the smaller
image, the apex is not detected as such. The more we reduce the image, the more likely it
is that we're going to lose the entrance to that chicane too.
Program :
Detecting features – corners:
import cv2
import numpy as np
img = cv2.imread('chess_board.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 2, 23, 0.04)
img[dst>0.01 * dst.max()] = [0, 0, 255]
while (True):
cv2.imshow('corners', img)
if cv2.waitKey(1000 / 12) & 0xff == ord("q"):
break
cv2.destroyAllWindows()

Feature extraction and description using DoG and SIFT:


Om Bhamare BE B 05
Machine Vision Lab
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING
# coding:utf-8
from google.colab.patches import cv2_imshow
import cv2
# Read pictures
img = cv2.imread("varese.jpg")
# Transfer to grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Create a SIFT object and calculate the grayscale image
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray, None)
# Draw a key on the image
# DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS represents circles and directions to each key
point
img = cv2.drawKeypoints(image=img, outImage=img, keypoints=keypoints,
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,
color=(51, 163, 236))
cv2_imshow( img)
cv2.waitKey()
cv2.destroyAllWindows()

Output :
Detecting features – corners:

Feature extraction and description using DoG and SIFT:

Om Bhamare BE B 05
Machine Vision Lab
Aldel Education Trust’s
St. John College of Engineering and Management, Palghar
(A Christian Religious Minority Institution)
Approved by AICTE and DTE, Affiliated to University of Mumbai/MSBTE
St. John Technical Campus, Vevoor, Manor Road, Palghar (E), Dist. Palghar, Maharashtra-401404
NAAC Accredited with Grade ‘A’
DEPARTMENT OF COMPUTER ENGINEERING

Conclusion: We learned about detecting features in images and extracting them into descriptors.
We explored a number of algorithms available in OpenCV to accomplish this task, and then applied
them to real-life scenarios to understand a real-world application of the concepts we explored.

Om Bhamare BE B 05
Machine Vision Lab

You might also like