Skip to content

tracks

tracks #

tracks — pairwise matches chained into the keypoint sets that see one point.

tracks(images, pairs) #

The tracks of images: keypoints that match chains across pairs.

Every pair is matched — across the thread pool, since the detector and the matcher release the interpreter lock — and its correspondences are edges between (image, keypoint) nodes; a track is a connected component of that graph. A component holding two keypoints of one image contradicts itself (one point cannot appear twice in a photograph) and is dropped whole, as is one shorter than two keypoints.

Parameters:

Name Type Description Default
images Array[Image]

The images, in the order pairs indexes.

required
pairs Pairs

Which images to match.

required
Source code in capturegraph-recipes/capturegraph_recipes/sfm/correspondence/tracks.py
@cg.pure_function("sfm.tracks/1")
def tracks(images: cg.Array[cg.Image], pairs: Pairs) -> Tracks:
    """The tracks of ``images``: keypoints that ``match`` chains across ``pairs``.

    Every pair is matched — across the thread pool, since the detector and
    the matcher release the interpreter lock — and its correspondences are
    edges between ``(image, keypoint)`` nodes; a track is a connected
    component of that graph. A component holding two keypoints of one image
    contradicts itself (one point cannot appear twice in a photograph) and
    is dropped whole, as is one shorter than two keypoints.

    Args:
        images: The images, in the order ``pairs`` indexes.
        pairs: Which images to match.
    """
    ab = [(int(a), int(b)) for a, b in pairs.array("ab")]
    matched = cg.Array(ab).pmap(
        lambda pair: match(images[pair[0]], images[pair[1]]), "Matching pairs"
    )
    edges: list[np.ndarray] = []
    for (a, b), matches in zip(ab, matched, strict=True):
        if cg.is_missing(matches):
            raise RuntimeError(f"matching images {a} and {b} failed: {matches!r}")
        rows = matches.array("ab")
        edges.append(
            np.stack([np.full(len(rows), a), rows[:, 0], np.full(len(rows), b), rows[:, 1]], axis=1)
        )
    track, image, keypoint = _components(
        np.concatenate(edges) if edges else np.zeros((0, 4), dtype=np.int64)
    )
    cg.logging.info(
        "Chained tracks",
        tracks=int(track.max() + 1) if len(track) else 0,
        mean_length=float(len(track) / (track.max() + 1)) if len(track) else 0.0,
    )
    return Tracks.of(track=track, image=image, keypoint=keypoint)