-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_phg.py
122 lines (84 loc) · 3.41 KB
/
run_phg.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import numpy as np
import cv2
# import torch
import matplotlib.pyplot as plt
from feature_extractor import FeatureExtractor
from feature_matcher import FeatureMatcher
# from fundamental import eight_point
# from fundamental import seven_point
# from fundamental import levmarq
# from fundamental import calc_err_total
from essential import FivePoint
from ransac import Ransac
from fundamental import *
def read_img(path, grayscale = True):
return cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2GRAY)
def draw_kpts(im, kpts):
im_kpts = np.copy(im)
for kp in kpts:
cv2.circle(im_kpts,(int(kp.pt[0]),int(kp.pt[1])),1,(255,0,0),2)
return im_kpts
if __name__ == "__main__":
print("start photogrammetry")
path1 = "./data/img1.png"
path2 = "./data/img2.png"
im1 = read_img(path1)
im2 = read_img(path2)
extractor = FeatureExtractor()
kpts1, descs1 = extractor.extract(im1)
kpts2, descs2 = extractor.extract(im2)
im1_kpts = draw_kpts(im1, kpts1)
matcher = FeatureMatcher()
matches = matcher.match_features(descs1, descs2)
print(f"len(matches): {len(matches)}")
kpts1_np = []
kpts2_np = []
for i in range(len(matches)):
kpts1_np.append(kpts1[matches[i].queryIdx].pt)
kpts2_np.append(kpts2[matches[i].trainIdx].pt)
kpts1_np = np.array(kpts1_np)
kpts2_np = np.array(kpts2_np)
fivePoint = FivePoint()
fivePoint.estimate(kpts1_np[:5,:], kpts2_np[:5,:])
# f8 = eight_point(kpts1_np[:8,:], kpts2_np[:8,:])
# err = calc_err_total(kpts1_np,kpts2_np, f8)
# err_mean = err / kpts1_np.shape[0]
# print(f"8 | err: {err}, err_mean: {err_mean}")
# f7 = seven_point(kpts1_np[:7,:], kpts2_np[:7,:])
# err = calc_err_total(kpts1_np,kpts2_np, f7)
# err_mean = err / kpts1_np.shape[0]
# print(f"7 | err: {err}, err_mean: {err_mean}")
# # f_levmarq = levmarq(kpts1_np[:20,:], kpts2_np[:20,:])
# fCV, mask = cv2.findFundamentalMat(kpts1_np, kpts2_np,cv2.LMEDS)
# err = calc_err_total(kpts1_np,kpts2_np, fCV)
# err_mean = err / kpts1_np.shape[0]
# print(f"CV | err: {err}, err_mean: {err_mean}")
# calc error on all matched keypoints
# err8 = calc_err_total(kpts1_np, kpts2_np, f8)
# err7 = calc_err_total(kpts1_np, kpts2_np, f7)
# err_levmarq = calc_err_total(kpts1_np, kpts2_np, f_levmarq)
# print(f"err8: {err8}")
# print(f"err7: {err7}")
# print(f"err_levmarq: {err_levmarq}")
# print(f"f8 : {f8}")
# print(f"f7 : {f7}")
# print(f"f_levmarq : {f_levmarq}")
# eightPoint = EightPoint()
# ransac = Ransac(eightPoint)
# ratio, F = ransac.run(kpts1_np, kpts2_np)
# err_total = calc_err_total(kpts1_np, kpts2_np, F)
# err_mean = err_total/kpts1_np.shape[0]
# print(f"EightPoint| inlier_ratio: {ratio} | err_total: {err_total} | err_mean: {err_mean}")
# sevenPoint = SevenPoint()
# ransac = Ransac(sevenPoint)
# ratio, F = ransac.run(kpts1_np, kpts2_np)
# err_total = calc_err_total(kpts1_np, kpts2_np, F)
# err_mean = err_total/kpts1_np.shape[0]
# print(f"SevenPoint| inlier_ratio: {ratio} | err_total: {err_total} | err_mean: {err_mean}")
# print(f"F_ransac: {F_ransac}")
# levmarq = LevMarq(7)
# levmarq.__status__()
# eightPoint = EightPoint()
# print(f"eightPoint.min_samples_: {eightPoint.min_samples_}")
# ransac_estimator = Ransac(eightPoint)
# ransac_estimator.run(kpts1_np, kpts2_np)