Skip to content

Commit 93464bf

Browse files
FLANN and template matching
1 parent 2b7bd01 commit 93464bf

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

modifiedFLANNAlgo.py

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import matplotlib.pyplot as plt
99
import os
1010
import numpy as np
11+
import json
12+
from tqdm import tqdm
1113

1214
base_dir = os.getcwd()
1315
data_folder = os.path.join(base_dir, "Dataset")
@@ -16,6 +18,7 @@
1618
crops_folder = os.path.join(data_folder, "Crops")
1719
sample_testset = os.path.join(data_folder, "sample_testset")
1820

21+
sample_json_result = os.path.join(sample_testset, "sample_result.json")
1922
sample_images = os.path.join(sample_testset, "images")
2023
sample_crops = os.path.join(sample_testset, "crops")
2124

@@ -32,9 +35,23 @@ def ModifiedFLANN(img1, img2):
3235
kp2, des2 = sift.detectAndCompute(img2, None)
3336

3437
orgBorder = None
38+
flannMatch = True
3539

3640
if (des1 is None) or (des2 is None):
37-
return orgBorder
41+
flannMatch = False
42+
43+
if (img2.shape[0] > img1.shape[0]) and (img2[1].shape[1] > img1.shape[1]):
44+
res = cv2.matchTemplate(img2,img1, cv2.TM_CCOEFF)
45+
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
46+
h, w, c = img1.shape
47+
48+
pts = [int(min_loc[0]), int(min_loc[0]) + w,
49+
int(max_loc[0]), int(max_loc[1])]
50+
51+
return flannMatch, pts
52+
53+
return flannMatch, orgBorder
54+
3855
flann = cv2.FlannBasedMatcher(index_params, search_params)
3956
matches = flann.knnMatch(des1, des2, k=2)
4057

@@ -58,37 +75,33 @@ def ModifiedFLANN(img1, img2):
5875

5976
H, status = cv2.findHomography(cropImg, orgImg, cv2.RANSAC, 3.0)
6077

78+
if H is None:
79+
return flannMatch, orgBorder
80+
6181
h, w, c = img1.shape
6282

6383
cropBorder = np.float32([[[0,0], [0,h-1], [w-1,h-1], [w-1,0]]])
84+
6485
orgBorder = cv2.perspectiveTransform(cropBorder, H)
6586

6687
cv2.polylines(img2, [np.int32(orgBorder)], True, (0, 255, 0), 5)
67-
68-
else:
69-
print("No Good Matches Found")
70-
71-
matched = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
72-
plt.imshow(matched)
73-
plt.show()
74-
75-
return orgBorder
88+
89+
return flannMatch, orgBorder
7690

7791
def findMinMax(border):
7892
x, y = np.transpose(border)[0], np.transpose(border)[1]
7993

80-
x1, x2 = x.min(), x.max()
94+
x1, x2 = int(x.min()), int(x.max())
8195

82-
y1, y2 = y.min(), x.max()
96+
y1, y2 = int(y.min()), int(x.max())
8397

84-
return (x1, y1, x2, y2)
98+
return [x1, y1, x2, y2]
8599

86100
completeTracker = {}
87101

88-
allImages = os.listdir(sample_images)
89-
allcropImages = os.listdir(sample_crops)
102+
noAssociationCropImages = os.listdir(sample_crops)
90103

91-
for imagefile in os.listdir(sample_images):
104+
for imagefile in tqdm(os.listdir(sample_images)):
92105
img = cv2.imread(os.path.join(sample_images,
93106
imagefile))
94107
imageTracker = []
@@ -97,20 +110,31 @@ def findMinMax(border):
97110
crop_img = cv2.imread(os.path.join(sample_crops,
98111
cropfile))
99112

100-
crop_border = ModifiedFLANN(crop_img, img)
101-
102-
if crop_border is None:
103-
print("Images are Not associated")
113+
flannMatch, crop_border = ModifiedFLANN(crop_img, img)
104114

115+
if flannMatch:
116+
if crop_border is not None:
117+
pts = findMinMax(crop_border[0])
118+
imageTracker.append((cropfile.replace(".jpg", ""), pts))
119+
if cropfile in noAssociationCropImages:
120+
noAssociationCropImages.remove(cropfile)
105121
else:
106-
print("images are Associated")
107-
pts = findMinMax(crop_border[0])
108-
imageTracker.append((cropfile.replace(".jpg", ""), pts))
122+
if crop_border is not None:
123+
imageTracker.append((cropfile.replace(".jpg", ""), crop_border))
124+
if cropfile in noAssociationCropImages:
125+
noAssociationCropImages.remove(cropfile)
109126

110127
completeTracker[imagefile.replace(".jpg", "")] = imageTracker
111-
# =============================================================================
112-
# plt.imshow(cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB))
113-
# plt.show()
114-
#
115-
# =============================================================================
116128

129+
NA_Crops = []
130+
131+
for crop in noAssociationCropImages:
132+
NA_Crops.append([crop.replace(".jpg", ""), []])
133+
134+
completeTracker["NA"] = NA_Crops
135+
136+
with open(sample_json_result, "w") as f:
137+
json.dump(completeTracker, f)
138+
139+
140+
print("Output Json File is generated")

0 commit comments

Comments
 (0)