How do I mask opencv's keypoints post detection?

Viewed 528

I'm working with opencv on python.

I'm looking for an efficient way to use a mask to select all keypoints that fall inside it, after the keypoints were already detected.

TO BE CLEAR - I know that I can use a mask during the detection stage, what I need is to use another (different) mask after the keypoints were already detected (and this process is repeated with multiple sub-regions masks, so I can't combine 2 mask prior to detection).

import cv2

image = cv2.imread(some_image_file)
mask = cv2.imread(some_mask_file)
mask_2 = cv2.imread(some_additional_mask_file)


# Defining the detector:
detector = cv2.ORB_create(scoreType=cv2.ORB_FAST_SCORE)

# Detecting keypoints inside the initial mask's region:
kp, dsk = detector.detectAndCompute(image, mask)

# Using mask_2 to select a subset of the detected keypoints:
...

1 Answers

According to this post, you need to iterate over all keypoints and descriptors and add the good ones to a new list.

There is no possibility to mark them unnecessary.

Here is a sample code:

import numpy as np
import cv2

some_image_file = 'graf.png'

image = cv2.imread(some_image_file)

rows, cols = image.shape[0], image.shape[1]

mask = np.full((rows, cols), 255, np.uint8) # cv2.imread(some_mask_file)
mask_2 = mask.copy()  #cv2.imread(some_additional_mask_file)
mask_2[:, 0:cols//2] = 0

# Defining the detector:
detector = cv2.ORB_create(scoreType=cv2.ORB_FAST_SCORE)

# Detecting keypoints inside the initial mask's region:
kp, dsk = detector.detectAndCompute(image, mask)

good_kp = []  # List of "good keypoint"
good_dsk = []  # List of "good descriptors"


# Iterate over all keypoints and descriptors and the good ones to a new list. 
# There is no possibility to mark them unnecessary.
# https://stackoverflow.com/questions/29180815/delete-matches-in-opencv-keypoints-and-descriptors
for k, d in zip(kp, dsk):
    x, y = k.pt  # Each keypoint as an x, y tuple  https://stackoverflow.com/questions/35884409/how-to-extract-x-y-coordinates-from-opencv-cv2-keypoint-object

    if mask_2[int(y), int(x)] != 0:
        good_kp.append(k)   # Append keypoint to a list of "good keypoint".
        good_dsk.append(d)  # Append descriptor to a list of "good descriptors".

# Draw keypoints for testing
image_kp = image.copy()
cv2.drawKeypoints(image, kp, image_kp)

image_good_kp = image.copy()
cv2.drawKeypoints(image, good_kp, image_good_kp)

cv2.imshow('image_kp', image_kp)
cv2.imshow('image_good_kp', image_good_kp)
cv2.waitKey()
cv2.destroyAllWindows()

Input image:
enter image description here

image_kp:
enter image description here

image_good_kp.png (left side keypoints are masked):
enter image description here

Related