@@ -29,8 +29,10 @@ def clip(photo: NDArray, template: NDArray) -> NDArray:
29
29
template_gray = cv2 .cvtColor (template , cv2 .COLOR_BGR2GRAY )
30
30
31
31
# Detect keypoints and compute descriptors
32
- kpts1 , desc1 = brisk .detectAndCompute (photo_gray , None )
33
- kpts2 , desc2 = brisk .detectAndCompute (template_gray , None )
32
+ kpts1_ , desc1_ = brisk .detectAndCompute (photo_gray , None )
33
+ kpts2_ , desc2_ = brisk .detectAndCompute (template_gray , None )
34
+ kpts1 , desc1 = limit_keypoints (kpts1_ , desc1_ )
35
+ kpts2 , desc2 = limit_keypoints (kpts2_ , desc2_ )
34
36
35
37
# FLANN parameters
36
38
flann_params = {
@@ -72,6 +74,23 @@ def clip(photo: NDArray, template: NDArray) -> NDArray:
72
74
return np .zeros (template .shape , dtype = np .uint8 )
73
75
74
76
77
+ def limit_keypoints (
78
+ keypoints : list ,
79
+ descriptors : NDArray ,
80
+ max_keypoints : int = 50000 ,
81
+ ) -> tuple :
82
+ """Limit the number of keypoints and descriptors.
83
+
84
+ This adressess the issue described in #403.
85
+ """
86
+ if len (keypoints ) > max_keypoints :
87
+ # randomly select max_keypoints
88
+ indices = np .random .choice (len (keypoints ), max_keypoints , replace = False )
89
+ keypoints = [keypoints [i ] for i in indices ]
90
+ descriptors = descriptors [indices ]
91
+ return keypoints , descriptors
92
+
93
+
75
94
def filter_matrix (tran_matrix : NDArray ) -> bool :
76
95
"""Filters a failed transformation matrix based on specified conditions."""
77
96
h13 = tran_matrix [0 , 2 ]
0 commit comments