def test_cast_to_opencv_keypoints(self): """Tests conversion of GTSFM's keypoints to OpenCV's keypoints.""" gtsfm_keypoints = Keypoints( coordinates=np.array([[1.3, 5], [20, 10]]), scales=np.array([1.0, 5.2]), responses=np.array([4.2, 3.2]), ) results = gtsfm_keypoints.cast_to_opencv_keypoints() # check the length of the result self.assertEqual(len(results), len(gtsfm_keypoints)) # check all the keypoint values for idx in range(len(gtsfm_keypoints)): opencv_kp = results[idx] self.assertAlmostEqual(opencv_kp.pt[0], gtsfm_keypoints.coordinates[idx, 0], places=5) self.assertAlmostEqual(opencv_kp.pt[1], gtsfm_keypoints.coordinates[idx, 1], places=5) self.assertAlmostEqual(opencv_kp.size, gtsfm_keypoints.scales[idx], places=5) self.assertAlmostEqual(opencv_kp.response, gtsfm_keypoints.responses[idx], places=5)
def describe(self, image: Image, keypoints: Keypoints) -> np.ndarray: """Assign descriptors to detected features in an image. Arguments: image: the input image. keypoints: the keypoints to describe, of length N. Returns: Descriptors for the input features, of shape (N, D) where D is the dimension of each descriptor. """ if len(keypoints) == 0: return np.array([]) gray_image = image_utils.rgb_to_gray_cv(image) opencv_obj = cv.SIFT_create() # TODO(ayush): what to do about new set of keypoints _, descriptors = opencv_obj.compute( gray_image.value_array, keypoints.cast_to_opencv_keypoints()) return descriptors