Skip to content

Commit 8974895

Browse files
fix(clip): limit number of keypoints
adressess in part #403
1 parent f3a4cd1 commit 8974895

File tree

1 file changed

+21
-2
lines changed
  • sketch_map_tool/upload_processing

1 file changed

+21
-2
lines changed

sketch_map_tool/upload_processing/clip.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ def clip(photo: NDArray, template: NDArray) -> NDArray:
2929
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
3030

3131
# 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_)
3436

3537
# FLANN parameters
3638
flann_params = {
@@ -72,6 +74,23 @@ def clip(photo: NDArray, template: NDArray) -> NDArray:
7274
return np.zeros(template.shape, dtype=np.uint8)
7375

7476

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+
7594
def filter_matrix(tran_matrix: NDArray) -> bool:
7695
"""Filters a failed transformation matrix based on specified conditions."""
7796
h13 = tran_matrix[0, 2]

0 commit comments

Comments
 (0)