Skip to content

keypoints

keypoints #

keypoints — the SIFT features of one image, expressed at its stored size.

MAX_KEYPOINTS = 8192 module-attribute #

The strongest features kept per image.

ROOT_SIFT_SCALE = 512.0 module-attribute #

Maps a unit-length RootSIFT descriptor onto uint8 the way COLMAP stores it.

keypoints(image, max_dimension=1600) #

The SIFT features of image, with RootSIFT descriptors and sampled colours.

The detector runs on the grey image, downscaled when its longest side exceeds max_dimension; every position and scale is mapped back to the stored image's pixels, so a feature means the same thing whatever size it was found at. Descriptors are RootSIFT — L1-normalised, square-rooted — so a Euclidean distance between them is the Hellinger distance between the original histograms, which matches better than plain SIFT.

Parameters:

Name Type Description Default
image Image

The photograph.

required
max_dimension int

The longest side, in pixels, the detector runs at.

1600
Source code in capturegraph-recipes/capturegraph_recipes/sfm/correspondence/keypoints.py
@cg.pure_function("sfm.keypoints/1")
def keypoints(image: cg.Image, max_dimension: int = 1600) -> Keypoints:
    """The SIFT features of ``image``, with RootSIFT descriptors and sampled colours.

    The detector runs on the grey image, downscaled when its longest side
    exceeds ``max_dimension``; every position and scale is mapped back to the
    stored image's pixels, so a feature means the same thing whatever size it
    was found at. Descriptors are RootSIFT — L1-normalised, square-rooted —
    so a Euclidean distance between them is the Hellinger distance between
    the original histograms, which matches better than plain SIFT.

    Args:
        image: The photograph.
        max_dimension: The longest side, in pixels, the detector runs at.
    """
    with image.pil() as picture:
        width, height = picture.size
        rgb = np.asarray(picture.convert("RGB"))
        small = (
            picture
            if max(width, height) <= max_dimension
            else ImageOps.contain(picture, (max_dimension, max_dimension))
        )
        grey = np.asarray(small.convert("L"))
        small_width, small_height = small.size
    scale = np.array([width / small_width, height / small_height])
    found, descriptors = cv2.SIFT.create(nfeatures=MAX_KEYPOINTS).detectAndCompute(grey, None)
    if descriptors is None:
        descriptors = np.zeros((0, 128), dtype=np.float32)
    detected = np.array([point.pt for point in found], dtype=np.float64).reshape(-1, 2)
    xy = (detected + 0.5) * scale - 0.5
    columns = np.clip(np.rint(xy[:, 0]), 0, width - 1).astype(np.intp)
    rows = np.clip(np.rint(xy[:, 1]), 0, height - 1).astype(np.intp)
    cg.logging.info("Detected keypoints", count=len(found), width=width, height=height)
    return Keypoints.of(
        xy=xy,
        scale=np.array([point.size for point in found]) * float(scale.mean()),
        angle=np.array([point.angle for point in found]),
        descriptors=root_sift(descriptors),
        color=rgb[rows, columns],
        width=width,
        height=height,
    )

root_sift(descriptors) #

SIFT descriptors as uint8 RootSIFT: L1-normalised, square-rooted, scaled.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/correspondence/keypoints.py
def root_sift(descriptors: np.ndarray) -> np.ndarray:
    """SIFT descriptors as ``uint8`` RootSIFT: L1-normalised, square-rooted, scaled."""
    normalised = descriptors / np.maximum(descriptors.sum(axis=1, keepdims=True), 1e-12)
    return np.clip(np.rint(ROOT_SIFT_SCALE * np.sqrt(normalised)), 0, 255).astype(np.uint8)