Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Sift Feature Detection





No account yet ?

Sign up to access all content


Tested in Anaconda and Python 3.7

# # SIFT (Scale-Invariant Feature Transform)

# ## Import resources and display image

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)
 
 
# ## Detect keypoints and Create Descriptor

sift = cv2.xfeatures2d.SIFT_create()
 
train_keypoints, train_descriptor = sift.detectAndCompute(training_gray, None)
test_keypoints, test_descriptor = sift.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))
 
 
# ## Matching Keypoints

 
# Create a Brute Force Matcher object.
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck = False)
 
# Perform the matching between the SIFT 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))
 
 
Free image provided by pexel.com









Introduction To Feature Detection And Matching - GitHub











Tested in Anaconda and Python 3.7

import cv2 as cv
from matplotlib import pyplot as plt
 
def showimage(myimage, figsize=[10,10]):
    if (myimage.ndim>2):  #This only applies to RGB or RGBA images (e.g. not to Black and White images)
        myimage = myimage[:,:,::-1] #OpenCV follows BGR order, while matplotlib likely follows RGB order
         
    fig, ax = plt.subplots(figsize=figsize)
    ax.imshow(myimage, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.show()
 
img_originale = cv.imread('pexels-ali-pazani-2878373.jpg')
 
img = cv.imread('pexels-ali-pazani-2878373.jpg')
showimage(img)
 
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create()
kp = sift.detect(gray,None)
img=cv.drawKeypoints(img_originale,kp,img)
showimage(img)
img=cv.drawKeypoints(img_originale,kp,img,flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
showimage(img)
 
Free image provided by pexel.com
Free image provided by pexel.com
Free image provided by pexel.com










Tested in Anaconda and Python 3.7

import cv2
 
# read the images
img1 = cv2.imread('book.jpg')  
img2 = cv2.imread('table.jpg')
 
# convert images to grayscale
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
 
# create SIFT object
sift = cv2.xfeatures2d.SIFT_create()
# detect SIFT features in both images
keypoints_1, descriptors_1 = sift.detectAndCompute(img1,None)
keypoints_2, descriptors_2 = sift.detectAndCompute(img2,None)
# create feature matcher
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True)
# match descriptors of both images
matches = bf.match(descriptors_1,descriptors_2)
# sort matches by distance
matches = sorted(matches, key = lambda x:x.distance)
# draw first 50 matches
matched_img = cv2.drawMatches(img1, keypoints_1, img2, keypoints_2, matches[:50], img2, flags=2)
# show the image
cv2.imshow('image', matched_img)
# save the image
cv2.imwrite("matched_images.jpg", matched_img)
 


pythoncode-tutorials

License: MITLicenseMIT  Copyright (c) 2019 Rockikz


SIFT Feature Extraction using OpenCV in Python - GitHub

The Python Code Tutorials - GitHub



Free image provided by pexel.com
Free image provided by pexel.com




Free image provided by pexel.com










Tested in Anaconda and Python 3.7

import cv2
 
# reading the image
img = cv2.imread('table.jpg')
# convert to greyscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# create SIFT feature extractor
sift = cv2.xfeatures2d.SIFT_create()
# detect features from the image
keypoints, descriptors = sift.detectAndCompute(img, None)
# draw the detected key points
sift_image = cv2.drawKeypoints(gray, keypoints, img)
# show the image
cv2.imshow('image', sift_image)
# save the image
cv2.imwrite("table-sift.jpg", sift_image)
 


pythoncode-tutorialsy

License: MITLicenseMIT  Copyright (c) 2019 Rockikz


SIFT Feature Extraction using OpenCV in Python - GitHub

The Python Code Tutorials - GitHub



Free image provided by pexel.com




Free image provided by pexel.com








Image features


Computer vision


Deep learning

Machine learning












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