Updated May 29, 2023
Introduction to OpenCV findhomography()
OpenCV findhomography() Is a function present in the OpenCV Public Library that contains various commands that aid in programming related to computer vision and image detection. the OpenCV findhomography come on helps in transforming an image that is present in the form of a three by three matrix and Maps the specific points present in that image to the corresponding points that are present in another image that has been provided. This helps in object detection and face detection in a real-time scenario especially used in applications related to camera vision detection entries detection using artificial intelligence.
Syntax for using OpenCV findhomography()
The following syntax has to be used in order to utilize the OpenCV findhomography function for detecting objects in the relevant image which have been provided by the user:
cv2.findHomography()
How does the OpenCV findhomography is executed?
In order for the function to operate and calculating the homography present between two different images, the user needs to have a knowledge of a minimum of correspondence points that are present between the 2 images (the initial image entered by the user; and the image in which the initial image correspondence points have to be found). the open civil library is enabled to efficiently give an estimate for the homography that is the most suitable with respect to the corresponding points. It is generally observed that the correspondence points add automatically found by enabling the match of various features like SURF or SIFT within the images.
Parameters of evaluation:
- Importing the data (here image) – The System is enabled to read the images that are being passed on by the user
- Feature Matching – The parameter of feature matching focuses upon the features that correspond to two sets of data that are similarly based upon the distance for the searching dimension for two commands are used from the OpenCV library [e.g.: cv2.flann () and cv2.sift ()] which enable the system, to match the features with respect to the image that is being analyzed.
Example of using OpenCV findhomography()
Following is the example which helps us to understand how the OpenCV findhomography command is used in Python programming language
# Program which has been coded in the Python programming language in order to explain the use of find function
# command used to import the OpenCV library to utilize OpenCV erosion function
import cv2
import numpy as np1
# path defined for selecting the image
path1 = r'C:\Users\Priyanka Banerjee\Desktop\EduCBA_Image sample.png'
# the image is being read as a gray scaled image
img1 = cv2.imread(path1, cv2.IMREAD_GRAYSCALE)
# the webcam attached with system is being intialized
cap1 = cv2.VideoCapture(0)
# creating an algorithm using the sift command
sift1 = cv2.xfeatures2d.SIFT_create()
# finding the keypoints and the descriptors using the sift command
kp_image1, desc_image1 =sift1.detectAndCompute(img1, None)
* * * *
# The dictionary is being initialized
* index_params1 * * * = * * * dict * (* algorithm * = * 0 *, * * * * trees * = * 5 *) *
* search_params1 * * * = * * * dict(*)
# Using the flann command
flann1 = cv2.FlannBasedMatcher(index_params1, search_params1)
# the frame is being read by the system
_, * * *frame1 * * *= * * *cap.read(*)
# conversion of applied frame into grayscale image
grayframe1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
# finding the keypoints and the descriptors using the SIFT command
kp_grayframe1, * ** ** *desc_grayframe1* ** *=* ** *sift.detect* *And* *Compute(grayframe1, None)
# finding the nearest match using the KNN* ** *algorithm
matches1= flann.knnMatch* * (* *desc* *_* *image1, * ** ** *desc* *_* *grayframe1, * ** ** *k* *=* *2* *)
# initializing the list for keeping track of the points that are of acceptable threshold
good* ** *_* *points* *=* * [* *]
for m1, n1 in matches:
#appending the points in accordance with the distance observed for the descriptors
if(m1.distance1 < 0.6*n1.distance1):
good* *_* *points.append1(m1)
# index descriptors list maintained
query* *_* *pts1* ** *=* ** *np1.float32([kp_image1[m1.queryIdx]
.pt1 for m1 in good* *_* *points]).reshape(-10, 10, 20)
train_pts1* ** *=* ** *np1.float32([kp_grayframe1[m1.trainIdx]
.pt1 for m1 in good* *_* *points]).reshape(-10, 10, 20)
# find the perspective transformation
matrix1, mask1 = cv2.findHomography(query_pts1, train_pts1, cv2.RANSAC, 5.0)
matches_mask1* ** *=* ** *mask1.ravel(* *).tolist(* *)
# initialize the height & width for image
h1, w1 = img1* *.* *shape
# save all the points in variable pts1
pts1 = np1.float32([[0,0],[0,h1],[w1,h1],[w1,0]]).reshape(-10, 10, 20)
dst1 = cv2.perspectiveTransform(pts1, matrix1)
# drawing frame
homography1 = cv2.polylines(frame1, [np1.int32(dst)], True, (255,0,0),3)
# Displaying the output image
cv2.imshow("output", homography1)
cv2.waitKey(0)
cv2.destroyAllWindows
Output:
Original Image provided
Final Output on various images given
Note: With respect to the given example, the court that has been executed is seen to maintain a list that contains the index numbers for the descriptors along with the train descriptors. Further, the code is seen to be making use of cv2. find homography() command for getting the perspective of the transformation that is occurring. The command mask. ravel() is utilized for getting a contagious array that has been flattened. We further make use of the command cv2.polylines() in order for drawing a frame around the image which has been shared by the user. Finally, the function imshow () is used for displaying the result.
Conclusion
OpenCV findhomography() An essential function that is specially used in programs that are aimed at tracking various objects that are present within a video with the reference of an image that has been provided. similarly, the system is also enabled to track a certain object within the image which has been provided using the reference image provided by the user. This helps in object detection and face detection in a real-time scenario especially used in applications related to camera vision detection entries detection using artificial intelligence.
Recommended Articles
We hope that this EDUCBA information on “OpenCV findhomography()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.