-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·106 lines (80 loc) · 2.66 KB
/
Copy pathmain.py
File metadata and controls
executable file
·106 lines (80 loc) · 2.66 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
import cv2 as cv
import numpy as np
import sys
import glob
import multiprocessing
import roi
#TODO: Integrate with Henry's Code
#Get score
#Work on getting scale down
MIN_MATCH_COUNT = 4
DATAPATH = "./data/standard/*"
FILE = "./data/demo-images/scattered4.jpg"
def findInlier(img):
# Initiate SIFT detector
img1 = img[0] # Image in question
img2 = img[1][0] # Gold standard image
sift = cv.SIFT()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
# Store all good matches as per Lowe's ratio test.
good = [m for m, n in matches if m.distance < 0.7*n.distance]
if len(good) > MIN_MATCH_COUNT:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
else:
#print "Not enough matches are found - %d/%d" % (len(good), MIN_MATCH_COUNT)
matchesMask = []
# Print Inliers
return (len(matchesMask), img[1][1])
#Gets the nearest neighbor for each tile in
#a list of ROI tiles to the testData
def NN(tiles, testData):
"""param tiles: The regions of interest, in the form of images.
param testData: Array of 30 or forty images.
"""
#store the max inliers for each tile
maxInliers = []
for tile in tiles:
pairs = [(tile.copy(), img2) for img2 in testData]
if False:
p = multiprocessing.Pool(processes = 4)
inliers = p.map(findInlier, pairs)
else:
inliers = [findInlier(pair) for pair in pairs]
theMax = max(inliers)
#Show the tile to identify
cv.imshow("Tile to identify", tile)
cv.waitKey(1)
#Show the best guess tile
guess = cv.imread(theMax[1])
guess = cv.resize(guess, (0,0), fx = 0.2, fy = 0.2)
cv.imshow("The guess", guess)
cv.waitKey(0)
maxInliers.append(theMax)
#Converts a list of image names into a list of
#tuple's where the first value of the tuple is
#the OpenCV image and the second value of the
#tuple is the file name
def getImage(files):
return [(cv.resize(cv.imread(f),(0,0), fx = 0.2, fy = 0.2), f) for f in files]
def main():
#Get images
#get list of images in golden data set
# Obtains the array of images to the golden standards
testData = glob.glob(DATAPATH)
images = getImage(testData)
#REMOVE ONCE HENRY'S DONE
# tiles contains an array of images
tiles = roi.findRoi(FILE)
inliers = NN(tiles, images)
if __name__ == "__main__":
main()