Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(clip): limit number of keypoints #449

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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