> ## Documentation Index
> Fetch the complete documentation index at: https://aegean.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Lines, warps and implicit images

> Point and line duality, image warping and implicit images.

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pantelis/eng-ai-agents/blob/main/notebooks/3d-reconstruction/images-and-geometry/index.ipynb)

*This section was written by [Kaushik Kachireddy](https://github.com/kaushik0x7d2) ([pull request #74](https://github.com/pantelis/eng-ai-agents/pull/74)), with help from an AI coding agent (Claude Code) on the code. It reproduces the ideas of Chapter 38 of [*Foundations of Computer Vision*](https://visionbook.mit.edu/homogeneous_coordinates.html) by Antonio Torralba, Phillip Isola, and William T. Freeman.*

This section builds on [homogeneous coordinates](https://aegean.ai/aiml-common/lectures/sensor-models/homogeneous-coordinates/index.md) and applies them to lines and to image pixels. Three ideas drive it:

1. The transformation matrices from that section act on whole images, and the way you apply them to a pixel grid decides whether the result has holes.
2. Lines and points are dual under the cross product in homogeneous form: joining two points and intersecting two lines are the same operation.
3. Implicit image representations (SIREN-style networks that map $(x,y)\to \text{intensity}$) let you read an image at any sub-pixel location without an interpolation kernel, so a geometric transformation becomes an evaluation at inverse-transformed coordinates.

The figures are regenerated in PyTorch. One of them reframes warping as a 1D convolution on coordinates, and the last one replaces the pixel grid with a neural network.

> [accordion] Code
>
> ```python theme={null}
> import math
> from pathlib import Path
> import torch
> import torch.nn as nn
> import kornia.geometry.transform as KT
> import matplotlib.pyplot as plt
> from cycler import cycler
> from matplotlib_inline.backend_inline import set_matplotlib_formats
> set_matplotlib_formats("svg")
> FIG_GRAY = "#808080"
> FIG_COLORS = ["#0072B2", "#D55E00", "#009E73", "#cb77a6", "#c28600", "#1d99e0", "#7f7f7f"]
> plt.rcParams.update({
>     "figure.facecolor": "none", "axes.facecolor": "none",
>     "savefig.facecolor": "none", "savefig.transparent": True,
>     "text.color": FIG_GRAY, "axes.labelcolor": FIG_GRAY, "axes.edgecolor": FIG_GRAY,
>     "xtick.color": FIG_GRAY, "ytick.color": FIG_GRAY,
>     "grid.color": FIG_GRAY, "grid.alpha": 0.25,
>     "legend.facecolor": "none", "legend.edgecolor": FIG_GRAY, "legend.framealpha": 0,
>     "axes.prop_cycle": cycler(color=FIG_COLORS),
>     "svg.hashsalt": "aiml", "svg.fonttype": "path",
> })
> from matplotlib.patches import Polygon, Rectangle, Circle, FancyArrowPatch, PathPatch
> from matplotlib.path import Path as MplPath
> from matplotlib.collections import LineCollection
> from mpl_toolkits.mplot3d.art3d import Poly3DCollection
> from PIL import Image, ImageDraw
> import skimage.data
>
> torch.set_default_dtype(torch.float32)
> _ = torch.manual_seed(0)
> ```

## Heterogeneous and homogeneous coordinates

The [homogeneous coordinates](https://aegean.ai/aiml-common/lectures/sensor-models/homogeneous-coordinates/index.md) section introduced the extra coordinate. The book calls $(x, y)$ the heterogeneous form of a 2D point and $(x, y, 1)$ its homogeneous form. Every vector $(\lambda x, \lambda y, \lambda)$ with $\lambda \ne 0$ lies on the same ray through the origin and stands for the same 2D point, and dividing by $w$ recovers $(x, y)$. The figure below draws that ray where it crosses the plane $w = 1$. The two helpers below convert between the forms, and the rest of the section uses them.

> [accordion] Code
>
> ```python theme={null}
> def to_homogeneous(p):
>     """Append a 1 to the last axis.  (..., D) -> (..., D+1)."""
>     return torch.cat([p, torch.ones_like(p[..., :1])], dim=-1)
>
>
> def from_homogeneous(p):
>     """Divide by last coord. (..., D+1) -> (..., D)."""
>     return p[..., :-1] / p[..., -1:]
> ```

![Output from cell 5](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_5_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=dbfdf2ac18afe8dd5d16f8ac730afc0c)

## 2D image transformations

Every transformation that follows is a $3\times 3$ matrix acting on homogeneous points. The [homogeneous coordinates](https://aegean.ai/aiml-common/lectures/sensor-models/homogeneous-coordinates/index.md#which-properties-a-transformation-keeps) section sorts these matrices into rigid, similarity, affine and projective families and lists what each family keeps. Here the same matrices act on an image. The book applies them to a photograph of a clock. A wall-clock image stands in for it, and each matrix warps the actual pixels (first figure below). The second figure collects the elementary matrices in one place: translation, scaling, rotation and shear.

> [accordion] Code
>
> ```python theme={null}
> # Reference object: an AI-generated wall-clock image supplied by the contributor,
> # loaded as a torch tensor. It stands in for the book's own clock photograph,
> # which is not reproduced here.
> def apply_T(M, pts):
>     """Apply 3x3 homogeneous matrix to (N, 2) points - still used by the
>     hand-written forward_warp / backward_warp helpers in the warping section."""
>     return from_homogeneous(to_homogeneous(pts) @ M.T)
>
>
> CLOCK_URL = ("https://raw.githubusercontent.com/pantelis/eng-ai-agents/main/notebooks/"
>              "3d-reconstruction/images-and-geometry/assets/wallclock.png")
>
>
> def clock_path():
>     # Find the clock image next to this section, or download it once.
>     for candidate in (Path("assets"), Path("notebooks/3d-reconstruction/images-and-geometry/assets")):
>         if (candidate / "wallclock.png").exists():
>             return candidate / "wallclock.png"
>     import urllib.request
>     local = Path("wallclock.png")
>     if not local.exists():
>         urllib.request.urlretrieve(CLOCK_URL, local)
>     return local
>
>
> def render_clock(size=256):
>     """Load the bundled wall-clock asset (assets/wallclock.png) as-is.
>     The asset already has natural background padding around the clock, so we
>     just resize to `size` if needed and return the tensor unchanged."""
>     img = Image.open(clock_path()).convert("L")
>     if img.size != (size, size):
>         img = img.resize((size, size), Image.LANCZOS)
>     pixels = torch.frombuffer(bytearray(img.tobytes()), dtype=torch.uint8)
>     return pixels.to(torch.float32).view(size, size) / 255.0
>
>
> CLOCK = render_clock(256)
> CLOCK_H, CLOCK_W = CLOCK.shape
>
>
> def warp_image(img, M_3x3, dsize=None):
>     """Apply a 3x3 homogeneous matrix to an image via kornia (backward, bilinear).
>     Convention: M maps source pixel coords to target coords."""
>     H, W = img.shape
>     if dsize is None:
>         dsize = (H, W)
>     img_bchw = img.unsqueeze(0).unsqueeze(0)
>     M_b23 = M_3x3[:2, :3].unsqueeze(0)
>     out = KT.warp_affine(img_bchw, M_b23, dsize=dsize, mode="bilinear", padding_mode="border")
>     return out.squeeze(0).squeeze(0)
> ```
>
> ```python theme={null}
> def T_translate(tx, ty):
>     return torch.tensor([[1.0, 0.0, tx], [0.0, 1.0, ty], [0.0, 0.0, 1.0]])
>
>
> def T_scale(sx, sy, cx=0.0, cy=0.0):
>     """Scale about the point (cx, cy)."""
>     return (
>         torch.tensor([[1.0, 0.0, cx], [0.0, 1.0, cy], [0.0, 0.0, 1.0]])
>         @ torch.tensor([[sx, 0.0, 0.0], [0.0, sy, 0.0], [0.0, 0.0, 1.0]])
>         @ torch.tensor([[1.0, 0.0, -cx], [0.0, 1.0, -cy], [0.0, 0.0, 1.0]])
>     )
>
>
> def T_rotate(theta, cx=0.0, cy=0.0):
>     """Rotation about (cx, cy) by theta radians (image-down y)."""
>     c, s = math.cos(theta), math.sin(theta)
>     return (
>         torch.tensor([[1.0, 0.0, cx], [0.0, 1.0, cy], [0.0, 0.0, 1.0]])
>         @ torch.tensor([[c, -s, 0.0], [s, c, 0.0], [0.0, 0.0, 1.0]])
>         @ torch.tensor([[1.0, 0.0, -cx], [0.0, 1.0, -cy], [0.0, 0.0, 1.0]])
>     )
>
>
> def T_shear(shx, shy):
>     return torch.tensor([[1.0, shx, 0.0], [shy, 1.0, 0.0], [0.0, 0.0, 1.0]])
>
>
> def T_projective_warp(strength=0.0028):
>     """A homography that visibly tilts the image (right side closer to camera).
>     Bigger strength -> more pronounced trapezoidal warp."""
>     M = torch.eye(3)
>     M[2, 0] = strength            # right side shrinks toward vanishing point
>     M[2, 1] = -strength * 0.55    # top tilts inward
>     return M
>
>
> # Inverse of each transform is what warp_affine wants (backward mapping),
> # but kornia's warp_affine expects the source-to-target matrix and inverts
> # internally, so the forward transform is passed.
> cx, cy = CLOCK_W / 2, CLOCK_H / 2
> ```

![Output from cell 10](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_10_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=3f3eb3b6a4b1fd1513ebda2b60b7fc07)

![Output from cell 11](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_11_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=3e48d0ae365c93551c1f21376da0bd97)

### Geometric transformation as a convolution

If you represent an image as a set of triples $\{(\ell_i, x_i, y_i)\}$ (intensity plus explicit position), then a rotation is a *1D convolution on the position channels*:

$\begin{bmatrix}x'\\y'\end{bmatrix} = \underbrace{\begin{bmatrix}\cos\theta & -\sin\theta\\\sin\theta & \cos\theta\end{bmatrix}}_{\text{kernels } w_x, w_y} \begin{bmatrix}x\\y\end{bmatrix}.$

Intensity passes through untouched; only the coordinate channels are mixed, as the figure below shows. This framing makes it natural to learn warps with neural networks that operate on coordinate channels, and the rest of the section builds that bridge.

![Output from cell 12](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_12_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=4e8fd23678e3cacac12e75c6e99e4200)

## Lines and points: cross-product duality

In homogeneous coordinates, a 2D line $ax + by + c = 0$ is the vector $\mathbf{l} = (a, b, c)$, and incidence is $\mathbf{l}^\top \mathbf{p} = 0$. Two operations turn out to be the same cross product:

$$
\begin{aligned}
\mathbf{l} &= \mathbf{p}_1 \times \mathbf{p}_2 && \text{(line through two points)} \\
\mathbf{p} &= \mathbf{l}_1 \times \mathbf{l}_2 && \text{(intersection of two lines)}
\end{aligned}
$$

Parallel lines intersect at a point with $w = 0$. This is the *point at infinity* that the [homogeneous coordinates](https://aegean.ai/aiml-common/lectures/sensor-models/homogeneous-coordinates/index.md#homogeneous-scale-and-projective-points) section calls an ideal point, and it is the homogeneous form of a vanishing point. The section on [3D motion and its 2D projection](https://aegean.ai/aiml-common/lectures/3d-reconstruction/3d-motion-projection/index.md) uses exactly this construction.

![Output from cell 13](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_13_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=ee94aef680a6d4b3e00c481817673cea)

## Image warping: forward and backward mapping

Applying a transformation $M$ to an image *should* give you a new image. The naive way, forward mapping, walks the input pixels, transforms each location, and writes its intensity to the nearest target pixel. That leaves **holes**, because an expansion spreads the input pixels too thinly to cover the target grid. The correct way, backward mapping, walks the *target* pixels and pulls each back through $M^{-1}$, so every output pixel gets one well-defined intensity.

A small checkerboard makes the artifacts easy to see.

> [accordion] Code
>
> ```python theme={null}
> def make_checkerboard(n=80, squares=8):
>     """Small checkerboard image as a tensor in [0, 1]."""
>     g = (torch.arange(n)[:, None] // (n // squares)) + (torch.arange(n)[None, :] // (n // squares))
>     return (g % 2).float()
>
>
> def forward_warp(img, M):
>     """Forward mapping with nearest-neighbor write. Produces holes."""
>     H, W = img.shape
>     out = torch.zeros_like(img)
>     ys, xs = torch.meshgrid(torch.arange(H), torch.arange(W), indexing="ij")
>     pts = torch.stack([xs.flatten().float(), ys.flatten().float()], dim=-1)
>     new = apply_T(M, pts).round().long()
>     new_x, new_y = new[:, 0], new[:, 1]
>     mask = (new_x >= 0) & (new_x < W) & (new_y >= 0) & (new_y < H)
>     out[new_y[mask], new_x[mask]] = img[ys.flatten()[mask], xs.flatten()[mask]]
>     return out
>
>
> def backward_warp(img, M):
>     """Backward mapping with nearest-neighbor sample. Hole-free."""
>     H, W = img.shape
>     Minv = torch.linalg.inv(M)
>     ys, xs = torch.meshgrid(torch.arange(H), torch.arange(W), indexing="ij")
>     pts = torch.stack([xs.flatten().float(), ys.flatten().float()], dim=-1)
>     src = apply_T(Minv, pts).round().long()
>     src_x, src_y = src[:, 0], src[:, 1]
>     mask = (src_x >= 0) & (src_x < W) & (src_y >= 0) & (src_y < H)
>     out = torch.zeros_like(img)
>     out.view(-1)[mask] = img[src_y[mask], src_x[mask]]
>     return out
> ```

![Output from cell 15](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_15_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=009f776c0c642e5a9226fa471661d524)

![Output from cell 15](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_15_output_2.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=3caca1b7705f8573cb2ff0fb73648e2b)

### Differentiable warping with Kornia

The hand-written `backward_warp` above shows the principle. In practice you use [Kornia](https://kornia.readthedocs.io/), a differentiable computer vision library built on PyTorch. Its `warp_affine` performs the same backward mapping, but with bilinear interpolation (so no nearest-neighbor aliasing), in batches, and differentiably end to end. The next cell feeds it the same matrix $M$ and compares the result with the hand-written version.

> [accordion] Code
>
> ```python theme={null}
> # kornia equivalent of backward_warp - bilinear, batched, differentiable.
> # The clock image from above keeps the side-by-side on the same subject.
> img_bchw = clock_img.unsqueeze(0).unsqueeze(0)
> M_b23 = M[:2, :3].unsqueeze(0)
> H, W = clock_img.shape
> kornia_warp = KT.warp_affine(img_bchw, M_b23, dsize=(H, W), mode="bilinear")
> kornia_warp = kornia_warp.squeeze(0).squeeze(0)
> ```

![Output from cell 17](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_17_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=9f207b764e225fd95592ec334dc6054b)

## Implicit image representations (SIREN)

Backward mapping at sub-pixel locations needs an interpolation kernel, but only because the image is stored as a discrete grid. **SIREN** (Sitzmann et al., NeurIPS 2020) does away with the grid: it represents an image as a small network $f_\theta : (x, y) \to \text{intensity}$ whose layers use $\sin$ activations. The trained network *is* the image, and you can evaluate it at any continuous coordinate.

Geometric warping then becomes a coordinate transform with no interpolation kernel:

$\hat\ell(x, y) = f_\theta(M^{-1}(x, y, 1)^\top).$

The next cells train a small SIREN on the cameraman test image and warp it by feeding pre-transformed coordinates. The training is deliberately short: the point is to demonstrate the representation, not to maximize reconstruction quality.

> [accordion] Code
>
> ```python theme={null}
> class SIREN(nn.Module):
>     """Tiny implicit image network. Sine-activated MLP per Sitzmann et al., 2020."""
>
>     def __init__(self, hidden=256, layers=4, w0=30.0):
>         super().__init__()
>         self.w0 = w0
>         net = [nn.Linear(2, hidden)]
>         for _ in range(layers - 1):
>             net.append(nn.Linear(hidden, hidden))
>         net.append(nn.Linear(hidden, 1))
>         self.net = nn.ModuleList(net)
>         self._init_weights()
>
>     def _init_weights(self):
>         with torch.no_grad():
>             self.net[0].weight.uniform_(-1 / 2, 1 / 2)
>             for layer in self.net[1:]:
>                 bound = (6.0 / layer.in_features) ** 0.5 / self.w0
>                 layer.weight.uniform_(-bound, bound)
>
>     def forward(self, xy):
>         h = torch.sin(self.w0 * self.net[0](xy))
>         for layer in self.net[1:-1]:
>             h = torch.sin(self.w0 * layer(h))
>         return self.net[-1](h)
>
>
> def make_target_image(n=128):
>     """Load the classic 'cameraman' test image (the image the book uses for its SIREN
>     figure), center-crop, and downsample to n x n in [0, 1]."""
>     cam = skimage.data.camera()                  # (512, 512) uint8
>     H, W = cam.shape
>     # Center-crop to square (already square, but stay explicit), then downsample
>     side = min(H, W)
>     cy0, cx0 = (H - side) // 2, (W - side) // 2
>     cropped = cam[cy0:cy0 + side, cx0:cx0 + side]
>     # Convert through PIL for nice resampling, then bytes -> torch (no numpy on the math side).
>     img = Image.fromarray(cropped, mode="L").resize((n, n), Image.LANCZOS)
>     pixels = torch.frombuffer(bytearray(img.tobytes()), dtype=torch.uint8)
>     return pixels.to(torch.float32).view(n, n) / 255.0
>
>
> def coord_grid(n):
>     """Regular [-1, 1]^2 grid of shape (n*n, 2)."""
>     ys = torch.linspace(-1, 1, n)
>     xs = torch.linspace(-1, 1, n)
>     yy, xx = torch.meshgrid(ys, xs, indexing="ij")
>     return torch.stack([xx.flatten(), yy.flatten()], dim=-1)
> ```
>
> ```python theme={null}
> # Train SIREN on the cameraman image. Real images need more capacity / steps
> # than a synthetic blob; 2000 steps with a slightly wider MLP gets a decent
> # memorisation in ~10s on CPU.
> N = 128
> target = make_target_image(N).flatten().unsqueeze(-1)
> grid = coord_grid(N)
>
> model = SIREN(hidden=256, layers=4, w0=30.0)
> optim = torch.optim.Adam(model.parameters(), lr=1e-4)
>
> for step in range(2000):
>     pred = model(grid)
>     loss = ((pred - target) ** 2).mean()
>     optim.zero_grad()
>     loss.backward()
>     optim.step()
>
> print(f"final reconstruction MSE: {loss.item():.4f}")
> ```

```output theme={null}
final reconstruction MSE: 0.0000
```

![Output from cell 20](https://mintcdn.com/aegeanaiinc/y4QJG_qxjY_yF61c/aiml-common/lectures/3d-reconstruction/images-and-geometry/images/cell_20_output_1.svg?fit=max\&auto=format\&n=y4QJG_qxjY_yF61c\&q=85\&s=605e9bb31300a8258a916ce1c5c6e833)

## Concluding remarks

The section moves forward one step at a time:

* From matrices to warps: the transformation matrices act on images, and backward mapping applies them without leaving holes.
* From points and lines to cross-product duality: incidence, joins, and intersections collapse into one operation. Parallel lines meet at points with $w=0$, so vanishing points get a coordinate.
* From a discrete grid to an implicit function: with SIREN, the image *is* its parameters. Warping becomes an inverse-coordinate query and interpolation kernels disappear.

Every later geometry chapter of the book (perspective projection in 39, stereo in 40, homographies in 41, single-view metrology in 42, structure from motion in 44, radiance fields in 45) reuses the homogeneous algebra and the point and line duality.

***

Found an error on this page? [Report an issue](https://github.com/aegean-ai/eaia-feedback/issues/new?template=page-issue.yml\&page=/aiml-common/lectures/3d-reconstruction/images-and-geometry).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.