-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureDetector.py
69 lines (53 loc) · 1.78 KB
/
FeatureDetector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import cv2
from matplotlib import pyplot as plt
import os
TRESHOLD = 15
'''Taking all images that we want to classify for them'''
path= "C:\\Users\\faruk\Documents\\git_ws\\FeatureBasedTemplateMatching\\Class\\"
images = []
classname = []
image_list = os.listdir(path)
'''Creating classes via image names'''
for clss in image_list:
imgCurrent = cv2.imread(f'{path}{clss}',0)
images.append(imgCurrent)
classname.append(os.path.splitext(clss)[0])
'''Creating ORB object'''#Fast and Free to use
orb = cv2.ORB_create()
'''Finding All Descriptors'''
def findDesc(images):
descList = []
for image in images:
kp,desc = orb.detectAndCompute(image,None)
descList.append(desc)
return descList
'''Finding image id via using descritor list'''
def findID(img, descList):
kp2, desc2 = orb.detectAndCompute(img,None)
bf = cv2.BFMatcher()
matchList = []
finalval = -1
try:
for des in descList:
matches = bf.knnMatch(des,desc2,k=2)
goodmatches = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
goodmatches.append([m])
matchList.append(len(goodmatches))
except:
pass
if matchList:
if max(matchList) > TRESHOLD:
finalval = matchList.index(max(matchList))
return finalval
'''Image that we want to detect'''
detection_image = cv2.imread("C:\\Users\\faruk\Documents\\git_ws\\FeatureBasedTemplateMatching\\10kmmatch.jpg")
img_gray = cv2.cvtColor(detection_image,cv2.COLOR_BGR2GRAY)
descList = findDesc(images)
id =findID(img_gray,descList)
if id != -1:
cv2.putText(detection_image,classname[id],(50,50),cv2.FONT_HERSHEY_PLAIN,5,(255,0,0),3)
plt.imshow(detection_image)
plt.show()
cv2.imwrite("output.jpg",detection_image)