Updated April 19, 2023
Introduction to OpenCV SIFT
In order to perform detection of features and matching, we make use of a function called sift function or Scale invariant Feature Transform function in OpenCV using which the vector representations from the image’s visual content are extracted to perform mathematical operations on them and sift function is protected by patent and we are not allowed to use sift function for any commercial purposes for free of cost and the module opencv-contrib must be installed to be able to make use of sift function in our program and key points and key point descriptors are provided by using sift function where the key point descriptor is used to describe the key point.
Syntax to define SIFT function in OpenCV:
Sift_object = cv.SIFT_create()
keypoint = sift.detect(gray,None)
Where,
- SIFT_create() is a function used to create a sift object Sift_object.
- sift.detect() function is used to detect the key points in the image.
Working of SIFT Function in OpenCV
- In order to perform detection of features and matching, we make use of a function called sift function or scale invariant feature transform function in OpenCV.
- Scale invariant feature transform function or sift function in OpenCV is used to extract the vector representations from the image’s visual content to perform mathematical operations on them.
- Scale invariant feature transform function or sift function in OpenCV is protected by patent and we are not allowed to use sift function for any commercial purposes for free of cost.
- The module opencv-contrib must be installed to be able to make use of sift function or scale invariant feature transform function in our program.
- The key points and key point descriptors are provided by using sift function or scale invariant feature transform function where the key point descriptor is used to describe the key point at a given scale and rotation along with the image gradients.
Examples of OpenCV SIFT
Given below are the examples of OpenCV SIFT:
Example #1
OpenCV program in python to demonstrate sift function or scale invariant feature transform function using which we are going to determine the keypoints and orientation of the key points in a given image and display the resulting image as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image using imread() function from cv2 module and converting it into gray image
readimage = cv2.imread('C:/Users/admin/Desktop/logo.png')
grayimage = cv2.cvtColor(readimage, cv2.COLOR_BGR2GRAY)
#creating a sift object and using detectandcompute() function to detect the keypoints and descriptor from the image
siftobject = cv2.xfeatures2d.SIFT_create()
keypoint, descriptor = siftobject.detectAndCompute(grayimage, None)
#drawing the keypoints and orientation of the keypoints in the image and then displaying the image as the output on the screen
keypointimage = cv2.drawKeypoints(readimage, keypoint, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT', keypointimage)
cv2.waitKey()
Output:
In the above program, we are importing the module cv2. Then we are reading the image using imread() function from cv2 module and converting it into gray image. Then we are creating a sift object and then we are using detectandcompute() function to detect the keypoints and descriptor from the image. Then we are drawing the keypoints and orientation of the keypoints in the image and then displaying the image as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate sift function or scale invariant feature transform function using which we are going to determine the keypoints and orientation of the key points in a given image and display the resulting image as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image using imread() function from cv2 module and converting it into gray image
readimage = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
grayimage = cv2.cvtColor(readimage, cv2.COLOR_BGR2GRAY)
#creating a sift object and using detectandcompute() function to detect the keypoints and descriptor from the image
siftobject = cv2.xfeatures2d.SIFT_create()
keypoint, descriptor = siftobject.detectAndCompute(grayimage, None)
#drawing the keypoints and orientation of the keypoints in the image and then displaying the image as the output on the screen
keypointimage = cv2.drawKeypoints(readimage, keypoint, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT', keypointimage)
cv2.waitKey()
Output:
In the above program, we are importing the module cv2. Then we are reading the image using imread() function from cv2 module and converting it into gray image. Then we are creating a sift object and then we are using detectandcompute() function to detect the keypoints and descriptor from the image. Then we are drawing the keypoints and orientation of the keypoints in the image and then displaying the image as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate sift function or scale invariant feature transform function using which we are going to determine the keypoints and orientation of the key points in a given image and display the resulting image as the output on the screen.
Code:
#importing the module cv2
import cv2
#reading the image using imread() function from cv2 module and converting it into gray image
readimage = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
grayimage = cv2.cvtColor(readimage, cv2.COLOR_BGR2GRAY)
#creating a sift object and using detectandcompute() function to detect the keypoints and descriptor from the image
siftobject = cv2.xfeatures2d.SIFT_create()
keypoint, descriptor = siftobject.detectAndCompute(grayimage, None)
#drawing the keypoints and orientation of the keypoints in the image and then displaying the image as the output on the screen
keypointimage = cv2.drawKeypoints(readimage, keypoint, None, color=(0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow('SIFT', keypointimage)
cv2.waitKey()
Output:
In the above program, we are importing the module cv2. Then we are reading the image using imread() function from cv2 module and converting it into gray image. Then we are creating a sift object and then we are using detectandcompute() function to detect the keypoints and descriptor from the image. Then we are drawing the keypoints and orientation of the keypoints in the image and then displaying the image as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “OpenCV SIFT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.