No account yet ?
Tested in Anaconda and Python 3.7
import cv2 import matplotlib.pyplot as plt import numpy as np # Load the image image1 = cv2.imread('pexels-bestbe-models-2412691.jpg') # Convert the training image to RGB training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB) # Convert the training image to gray scale training_gray = cv2.cvtColor(training_image, cv2.COLOR_RGB2GRAY) # Create test image by adding Scale Invariance and Rotational Invariance test_image = cv2.pyrDown(training_image) test_image = cv2.pyrDown(test_image) num_rows, num_cols = test_image.shape[:2] rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 30, 1) test_image = cv2.warpAffine(test_image, rotation_matrix, (num_cols, num_rows)) test_gray = cv2.cvtColor(test_image, cv2.COLOR_RGB2GRAY) # Display traning image and testing image fx, plots = plt.subplots(1, 2, figsize=(20,10)) plots[0].set_title("Training Image") plots[0].imshow(training_image) plots[1].set_title("Testing Image") plots[1].imshow(test_image) orb = cv2.ORB_create() train_keypoints, train_descriptor = orb.detectAndCompute(training_gray, None) test_keypoints, test_descriptor = orb.detectAndCompute(test_gray, None) keypoints_without_size = np.copy(training_image) keypoints_with_size = np.copy(training_image) cv2.drawKeypoints(training_image, train_keypoints, keypoints_without_size, color = (0, 255, 0)) cv2.drawKeypoints(training_image, train_keypoints, keypoints_with_size, flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Display image with and without keypoints size fx, plots = plt.subplots(1, 2, figsize=(20,10)) plots[0].set_title("Train keypoints With Size") plots[0].imshow(keypoints_with_size, cmap='gray') plots[1].set_title("Train keypoints Without Size") plots[1].imshow(keypoints_without_size, cmap='gray') # Print the number of keypoints detected in the training image print("Number of Keypoints Detected In The Training Image: ", len(train_keypoints)) # Print the number of keypoints detected in the query image print("Number of Keypoints Detected In The Query Image: ", len(test_keypoints)) # Create a Brute Force Matcher object. bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True) # Perform the matching between the ORB descriptors of the training image and the test image matches = bf.match(train_descriptor, test_descriptor) # The matches with shorter distance are the ones we want. matches = sorted(matches, key = lambda x : x.distance) result = cv2.drawMatches(training_image, train_keypoints, test_gray, test_keypoints, matches, test_gray, flags = 2) # Display the best matching points plt.rcParams['figure.figsize'] = [14.0, 7.0] plt.title('Best Matching Points') plt.imshow(result) plt.show() # Print total number of matching points between the training and query images print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))
Introduction To Feature Detection And Matching - GitHub
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License