├── mask.jpg
├── meluha.jpg
├── meluha_from_webcam.jpg
├── FeatureDetection.py
├── README.md
├── FeatureMatching.py
└── ImageAugmentation.py
/mask.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infoaryan/Augmented-Reality-from-scratch/HEAD/mask.jpg
--------------------------------------------------------------------------------
/meluha.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infoaryan/Augmented-Reality-from-scratch/HEAD/meluha.jpg
--------------------------------------------------------------------------------
/meluha_from_webcam.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infoaryan/Augmented-Reality-from-scratch/HEAD/meluha_from_webcam.jpg
--------------------------------------------------------------------------------
/FeatureDetection.py:
--------------------------------------------------------------------------------
1 | #Code written by : Aryan Verma (infoaryan)
2 | #Full explanation video link : https://youtu.be/lU4zgDe1x6Y
3 |
4 | import cv2
5 | import numpy as np
6 |
7 | #Getting the Image ready for feature detection
8 | input_image = cv2.imread('meluha.jpg')
9 | input_image = cv2.resize(input_image, (400,550),interpolation=cv2.INTER_AREA)
10 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
11 | # Initiate ORB object
12 | orb = cv2.ORB_create(nfeatures=1000)
13 |
14 | # find the keypoints with ORB
15 | keypoints, descriptors = orb.detectAndCompute(gray_image, None)
16 |
17 | # draw only the location of the keypoints without size or
18 | final_keypoints = cv2.drawKeypoints(gray_image, keypoints,input_image,(0,255,0))
19 |
20 | cv2.imshow('ORB keypoints', final_keypoints)
21 | cv2.waitKey()
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Augmented-Reality-from-scratch
2 | In this project series we are going to implement a series of project files, where at the end we will make a Final Software which will be capable to take
3 | a picture from us and augment any 3-D model or any other picture to the live video stream.
4 |
First Module Details : (FEATURE DETECTION)
5 |
6 |
7 | - It explains the concept of Feature Detection and Description.
8 |
- Oriented FAST and Rotated BRIEF (ORB) used, being Open Source.
9 |
- Feature detected and marked as green points in the image (meluha.jpg)
10 |
- Next video aim : To use the features for live stream feature matching.
11 |
- Video Link : Youtube Video Explanation Here
12 |
13 |
14 | Second Module Details: (FEATURE MATCHING)
15 |
16 | - It explains the concept of feature matching.
17 |
- Feature Matching Attained with FLANN Based Matcher
18 |
- Next Video Aim: To compute the Homography Matrix and Augment Image on realtime frame.
19 |
- Video link : Explanation Video Here.
20 |
--------------------------------------------------------------------------------
/FeatureMatching.py:
--------------------------------------------------------------------------------
1 | #Code designed by : Aryan Verma
2 | #Video Explanation Link : https://youtu.be/lU4zgDe1x6Y
3 |
4 | import cv2
5 | import numpy as np
6 |
7 |
8 | #Initilizing the ORB Feature Detector
9 | MIN_MATCHES = 20
10 | detector = cv2.ORB_create(nfeatures=5000)
11 |
12 | #Preparing the FLANN Based matcher
13 | index_params = dict(algorithm = 1, trees=3)
14 | search_params = dict(checks=100)
15 | flann = cv2.FlannBasedMatcher(index_params,search_params)
16 |
17 |
18 |
19 | #Function for Loading input image and Keypoints
20 | def load_input():
21 | input_image = cv2.imread('meluha.jpg')
22 | input_image = cv2.resize(input_image, (400,550),interpolation=cv2.INTER_AREA)
23 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
24 | # find the keypoints with ORB
25 | keypoints, descriptors = detector.detectAndCompute(gray_image, None)
26 |
27 | return gray_image,keypoints, descriptors
28 |
29 |
30 |
31 | #Function for Computing Matches between the train and query descriptors
32 | def compute_matches(descriptors_input, descriptors_output):
33 |
34 | if(len(descriptors_output)!=0 and len(descriptors_input)!=0):
35 | matches = flann.knnMatch(np.asarray(descriptors_input,np.float32),np.asarray(descriptors_output,np.float32),k=2)
36 | good = []
37 | for m,n in matches:
38 | if m.distance < 0.68*n.distance:
39 | good.append([m])
40 | return good
41 | else:
42 | return None
43 |
44 |
45 |
46 |
47 | #Main Working Logic
48 | if __name__=='__main__':
49 |
50 | #Getting Information form the Input image
51 | input_image, input_keypoints, input_descriptors = load_input()
52 |
53 | #Getting camera ready
54 | cap = cv2.VideoCapture(0)
55 | ret, frame = cap.read()
56 |
57 | while(ret):
58 | ret, frame = cap.read()
59 |
60 | #Condition Check for error escaping
61 | if(len(input_keypoints)10):
61 | src_pts = np.float32([ input_keypoints[m.queryIdx].pt for m in matches ]).reshape(-1,1,2)
62 | dst_pts = np.float32([ output_keypoints[m.trainIdx].pt for m in matches ]).reshape(-1,1,2)
63 |
64 | #Finally find the homography matrix
65 | M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
66 | #matchesMask = mask.ravel().tolist()
67 | pts = np.float32([ [0,0],[0,399],[299,399],[299,0] ]).reshape(-1,1,2)
68 | dst = cv2.perspectiveTransform(pts,M)
69 | M_aug = cv2.warpPerspective(aug_image, M, (600,450))
70 |
71 | #getting the frame ready for addition operation with Mask Image
72 | frameb = cv2.fillConvexPoly(frame,dst.astype(int),0)
73 | Final = frameb+M_aug
74 |
75 | #output_final = cv2.polylines(frame,[np.int32(dst)],True,255,3, cv2.LINE_AA)
76 | cv2.imshow('Final Output', Final)
77 | #cv2.imshow('Finallli', Final)
78 | else:
79 | cv2.imshow('Final Output', frame)
80 | else:
81 | cv2.imshow('Final Output', frame)
82 | key = cv2.waitKey(15)
83 | if(key==27):
84 | break
85 |
--------------------------------------------------------------------------------