Skip to content

Commit

Permalink
fix(clip): limit number of keypoints
Browse files Browse the repository at this point in the history
adressess in part #403
  • Loading branch information
matthiasschaub committed Apr 16, 2024
1 parent 4eaee8d commit fc4fea1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions sketch_map_tool/upload_processing/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def clip(photo: NDArray, template: NDArray) -> NDArray:
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

# Detect keypoints and compute descriptors
kpts1, desc1 = brisk.detectAndCompute(photo_gray, None)
kpts2, desc2 = brisk.detectAndCompute(template_gray, None)
kpts1_, desc1_ = brisk.detectAndCompute(photo_gray, None)
kpts2_, desc2_ = brisk.detectAndCompute(template_gray, None)
kpts1, desc1 = limit_keypoints(kpts1_, desc1_)
kpts2, desc2 = limit_keypoints(kpts2_, desc2_)

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


def limit_keypoints(
keypoints: list,
descriptors: NDArray,
max_keypoints: int = 50000,
) -> tuple:
"""Limit the number of keypoints and descriptors.
This adressess the issue described in #403.
"""
if len(keypoints) > max_keypoints:
# randomly select max_keypoints
indices = np.random.choice(len(keypoints), max_keypoints, replace=False)
keypoints = [keypoints[i] for i in indices]
descriptors = descriptors[indices]
return keypoints, descriptors


def filter_matrix(tran_matrix: NDArray) -> bool:
"""Filters a failed transformation matrix based on specified conditions."""
h13 = tran_matrix[0, 2]
Expand Down

0 comments on commit fc4fea1

Please sign in to comment.