Skip to content

Commit 487b308

Browse files
committed
add sift tutorial
1 parent 4dc67b1 commit 487b308

File tree

9 files changed

+56
-0
lines changed

9 files changed

+56
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5555
- [How to Make a Barcode Reader in Python](https://www.thepythoncode.com/article/making-a-barcode-scanner-in-python). ([code](general/barcode-reader))
5656
- [Image Transformations using OpenCV in Python](https://www.thepythoncode.com/article/image-transformations-using-opencv-in-python). ([code](machine-learning/image-transformation))
5757
- [How to Apply HOG Feature Extraction in Python](https://www.thepythoncode.com/article/hog-feature-extraction-in-python). ([code](machine-learning/hog-feature-extraction))
58+
- [SIFT Feature Extraction using OpenCV in Python](https://www.thepythoncode.com/article/sift-feature-extraction-using-opencv-in-python). ([code](machine-learning/sift))
5859
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
5960
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
6061
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).

machine-learning/sift/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [SIFT Feature Extraction using OpenCV in Python](https://www.thepythoncode.com/article/sift-feature-extraction-using-opencv-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- For feature keypoints extraction, use `sift.py`
5+
- For feature matching, use `feature_match.py`

machine-learning/sift/book.jpg

39.9 KB
Loading
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cv2
2+
3+
# read the images
4+
img1 = cv2.imread('book.jpg')
5+
img2 = cv2.imread('table.jpg')
6+
7+
# convert images to grayscale
8+
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
9+
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
10+
11+
# create SIFT object
12+
sift = cv2.xfeatures2d.SIFT_create()
13+
# detect SIFT features in both images
14+
keypoints_1, descriptors_1 = sift.detectAndCompute(img1,None)
15+
keypoints_2, descriptors_2 = sift.detectAndCompute(img2,None)
16+
# create feature matcher
17+
bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True)
18+
# match descriptors of both images
19+
matches = bf.match(descriptors_1,descriptors_2)
20+
# sort matches by distance
21+
matches = sorted(matches, key = lambda x:x.distance)
22+
# draw first 50 matches
23+
matched_img = cv2.drawMatches(img1, keypoints_1, img2, keypoints_2, matches[:50], img2, flags=2)
24+
# show the image
25+
cv2.imshow('image', matched_img)
26+
# save the image
27+
cv2.imwrite("matched_images.jpg", matched_img)
28+
cv2.waitKey(0)
29+
cv2.destroyAllWindows()
105 KB
Loading
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opencv-contrib-python==3.4.2.16
2+
opencv-python==3.4.2.16
3+
numpy

machine-learning/sift/sift.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import cv2
2+
3+
# reading the image
4+
img = cv2.imread('table.jpg')
5+
# convert to greyscale
6+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
7+
# create SIFT feature extractor
8+
sift = cv2.xfeatures2d.SIFT_create()
9+
# detect features from the image
10+
keypoints, descriptors = sift.detectAndCompute(img, None)
11+
# draw the detected key points
12+
sift_image = cv2.drawKeypoints(gray, keypoints, img)
13+
# show the image
14+
cv2.imshow('image', sift_image)
15+
# save the image
16+
cv2.imwrite("table-sift.jpg", sift_image)
17+
cv2.waitKey(0)
18+
cv2.destroyAllWindows()

machine-learning/sift/table-sift.jpg

64.9 KB
Loading

machine-learning/sift/table.jpg

89.2 KB
Loading

0 commit comments

Comments
 (0)