Skip to content

growing

growing #

Growing — the model under construction, from loaded tracks to a sealed Reconstruction.

  1. Load. load reads every image's keypoints and its calibration prior, and indexes the track rows so a track's views and an image's tracks are one slice or one mask away.
  2. Grow. The registration loop fills in cameras, points and observations through the fields; cameras stacks any subset of the registered ones for the geometry helpers.
  3. Seal. sealed turns what was placed into a Reconstruction: poses at each image's size, coloured points, observations by point.

FOCAL_PRIOR = 1.2 module-attribute #

Focal length over the longer image side when EXIF names none: a typical phone camera.

Growing dataclass #

The model under construction: image priors, tracks, and what has been placed so far.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
@dataclasses.dataclass
class Growing:
    """The model under construction: image priors, tracks, and what has been placed so far."""

    pixels: list[np.ndarray]
    colors: list[np.ndarray]
    sizes: list[tuple[int, int]]
    calibrations: np.ndarray
    track: np.ndarray
    image: np.ndarray
    keypoint: np.ndarray
    starts: np.ndarray
    ends: np.ndarray
    rotations: dict[int, np.ndarray] = dataclasses.field(default_factory=dict)
    translations: dict[int, np.ndarray] = dataclasses.field(default_factory=dict)
    points: list[np.ndarray] = dataclasses.field(default_factory=list)
    point_track: list[int] = dataclasses.field(default_factory=list)
    track_point: dict[int, int] = dataclasses.field(default_factory=dict)
    observations: list[tuple[int, int, int]] = dataclasses.field(default_factory=list)

    def registered(self) -> list[int]:
        """The registered images, ascending."""
        return sorted(self.rotations)

    def cameras(self, images: list[int]) -> Cameras:
        """The registered ``images`` stacked, in that order."""
        return Cameras(
            np.array([self.rotations[image] for image in images]).reshape(-1, 3, 3),
            np.array([self.translations[image] for image in images]).reshape(-1, 3),
            self.calibrations[images],
        )

    def rows_of_track(self, track: int) -> tuple[np.ndarray, np.ndarray]:
        """The ``(images, keypoints)`` of one track's rows."""
        span = slice(self.starts[track], self.ends[track])
        return self.image[span], self.keypoint[span]

    def tracks_of_image(self, image: int) -> tuple[np.ndarray, np.ndarray]:
        """The ``(tracks, keypoints)`` of one image's rows."""
        rows = np.flatnonzero(self.image == image)
        return self.track[rows], self.keypoint[rows]

cameras(images) #

The registered images stacked, in that order.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
def cameras(self, images: list[int]) -> Cameras:
    """The registered ``images`` stacked, in that order."""
    return Cameras(
        np.array([self.rotations[image] for image in images]).reshape(-1, 3, 3),
        np.array([self.translations[image] for image in images]).reshape(-1, 3),
        self.calibrations[images],
    )

registered() #

The registered images, ascending.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
def registered(self) -> list[int]:
    """The registered images, ascending."""
    return sorted(self.rotations)

rows_of_track(track) #

The (images, keypoints) of one track's rows.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
def rows_of_track(self, track: int) -> tuple[np.ndarray, np.ndarray]:
    """The ``(images, keypoints)`` of one track's rows."""
    span = slice(self.starts[track], self.ends[track])
    return self.image[span], self.keypoint[span]

tracks_of_image(image) #

The (tracks, keypoints) of one image's rows.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
def tracks_of_image(self, image: int) -> tuple[np.ndarray, np.ndarray]:
    """The ``(tracks, keypoints)`` of one image's rows."""
    rows = np.flatnonzero(self.image == image)
    return self.track[rows], self.keypoint[rows]

load(images, tracks) #

An empty model over images and tracks, priors and indices in place.

Raises:

Type Description
ValueError

If the track ids are not 0..T-1 as tracks writes them.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
def load(images: cg.Array[cg.Image], tracks: Tracks) -> Growing:
    """An empty model over ``images`` and ``tracks``, priors and indices in place.

    Raises:
        ValueError: If the track ids are not ``0..T-1`` as ``tracks`` writes them.
    """
    pixels: list[np.ndarray] = []
    colors: list[np.ndarray] = []
    sizes: list[tuple[int, int]] = []
    calibrations: list[np.ndarray] = []
    for image in cg.logging.progress(images, label="Reading keypoints"):
        found = keypoints(image).arrays()
        pixels.append(found["xy"])
        colors.append(found["color"])
        sizes.append((int(found["width"]), int(found["height"])))
        calibrations.append(_prior_calibration(image, *sizes[-1]))
    rows = tracks.arrays()
    ids, starts = np.unique(rows["track"], return_index=True)
    if len(ids) and (ids != np.arange(len(ids))).any():
        raise ValueError("track ids must run 0..T-1 as `tracks` writes them")
    return Growing(
        pixels=pixels,
        colors=colors,
        sizes=sizes,
        calibrations=np.array(calibrations).reshape(-1, 3, 3),
        track=rows["track"],
        image=rows["image"],
        keypoint=rows["keypoint"],
        starts=starts,
        ends=np.append(starts[1:], len(rows["track"])),
    )

sealed(model) #

What model placed, as a Reconstruction keyed by image position.

Source code in capturegraph-recipes/capturegraph_recipes/sfm/reconstruction/growing.py
def sealed(model: Growing) -> Reconstruction:
    """What ``model`` placed, as a ``Reconstruction`` keyed by image position."""
    cameras: dict[str, cg.Pose] = {}
    for image in model.registered():
        width, height = model.sizes[image]
        template = cg.Pose(
            position_x=0.0,
            position_y=0.0,
            position_z=0.0,
            quaternion_w=1.0,
            quaternion_x=0.0,
            quaternion_y=0.0,
            quaternion_z=0.0,
            reference_width=float(width),
            reference_height=float(height),
        )
        cameras[camera_key(image)] = pose_from_extrinsics(
            model.rotations[image],
            model.translations[image],
            template,
            model.calibrations[image],
        )
    observed = np.array(model.observations, dtype=np.int64).reshape(-1, 3)
    order = np.argsort(observed[:, 0], kind="stable")
    observed = observed[order]
    return Reconstruction(
        cameras=cg.Map[cg.String, cg.Pose](cameras),
        points=cg.PointCloud.of_points(
            np.array(model.points, dtype=np.float32).reshape(-1, 3),
            track_colors(model.colors, observed[:, 0], observed[:, 1], observed[:, 2]),
        ),
        observations=Observations.of(
            point=observed[:, 0],
            image=observed[:, 1],
            keypoint=observed[:, 2],
        ),
    )