Algorithmadvanced
Calibration from vanishing points
Also called orthogonal vanishing points calibration.
Get the focal length, the principal point and the rotation from the vanishing points of three orthogonal directions: the principal point is the orthocenter of their triangle.
- Solves
- Camera calibration
- Time
- Θ(1) worst · average · best
- Space
- Θ(1) extra
- Where
- k = the number of polar iterations at line L9 (at most 60)
- Model
- Real RAM
- Properties
- deterministicexactofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Bruno Caprile, Vincent Torre, 1990
Calibration from vanishing points finds the focal length , the principal point and the rotation of a camera from a single image that shows three mutually orthogonal sets of parallel lines, such as the edges of a building or a box. Each set of lines meets at a vanishing point , where is the direction of the lines in the camera frame. If the camera has zero skew and square pixels, the principal point is the orthocenter of the triangle , and (Caprile and Torre 1990, HZ Section 8.8). The method needs no calibration target and has a closed form. It is less accurate than Zhang's method, which uses many points on several views of a plane, and unlike Tsai's method it needs no known 3D coordinates at all. The camera calibration page states the problem; the vanishing point detection page covers how to find the three points.
In plain words
Stand in front of a building and take a photo. The horizontal edges along the front wall seem to meet at one point far to the side. The edges along the side wall meet at another point, and the vertical edges meet at a third point far above or below. These three meeting points form a triangle.
The camera's center of view sits where the three heights of this triangle cross. The focal length follows from how far the points are from that center. Each meeting point also tells the direction of one set of edges, so you also get how the camera is turned. All this comes from one photo and a few lines, with no special pattern in the scene. The method assumes that the pixels are square and that the camera has no skew, which holds for nearly all modern cameras.
The problem
Input. Three finite pixels , the vanishing points of three mutually orthogonal scene directions. Coordinates follow the area: to the right, down, and the center of a pixel at integer coordinates.
Model. with zero skew and square pixels. The world axes are the three directions, and holds them in the camera frame, so . Lens distortion must be removed first, because lens distortion acts on normalized coordinates (project, distort, then apply ) and bends the lines that define the .
Output. , and a rotation whose columns are . The sign of each column is not fixed by the image, because the directions and have the same vanishing point. The translation is not part of the output: vanishing points are images of points at infinity, so they do not depend on where the camera is.
The key fact. is parallel to , and with the form of above it equals . So becomes
In the language of the image of the absolute conic , this is . Subtract the equation for the pair from the one for : , so lies on the altitude of the triangle from . In the same way, lies on the other two altitudes, so is the orthocenter.
The algorithm
VP-CALIBRATE(v_1, v_2, v_3)- 1
a_1 <- v_1 - v_2; b_1 <- a_1 . v_3 - 2
a_2 <- v_2 - v_3; b_2 <- a_2 . v_1 - 3
D <- det(a_1, a_2); if D = 0 then fail: the points are collinear - 4
c <- the solution p of a_1 . p = b_1 and a_2 . p = b_2, by Cramer's rule - 5
q <- -(v_1 - c) . (v_2 - c); if q <= 0 then fail: the triangle is not acute - 6
f <- sqrt(q); K <- [[f, 0, c_x], [0, f, c_y], [0, 0, 1]] - 7
for i <- 1 to 3: r_i <- K^-1 (v_i, 1) / ||K^-1 (v_i, 1)|| - 8
R <- [r_1 r_2 r_3]; if det R < 0 then r_3 <- -r_3 - 9
R <- nearest rotation to R, by the iteration X <- (X + X^-T) / 2 - 10
return K, R
Lines L1 to L4 intersect two altitudes. The equation says , the altitude from , and is the altitude from . With and ,
In floating point, line L3 rejects below a small multiple of . Line L5 uses the pair ; at the orthocenter all three pairs give the same value, as the proof shows. Line L7 picks for each column the sign with a positive component, because has last entry 1. Line L8 makes the frame right handed. Line L9 is the Newton iteration for the orthogonal polar factor (Higham 1986); it does not change an exact rotation, and with noisy input it returns the nearest rotation in the Frobenius norm.
The two point variant. If the principal point is known, for example as the image center, two vanishing points of orthogonal directions suffice: .
Example
Let the true camera have and . The three scene directions have, in the camera frame, the directions , and , which are pairwise orthogonal. Their vanishing points are :
| line | values |
|---|---|
| L1 | , |
| L2 | , |
| L3 | |
| L4 | , |
| L5 | |
| L6 | |
| L7 | , , |
| L8 | , so and no column changes sign |
| L9 | is already a rotation and does not change |
The side is vertical, so the altitude from is the row , and indeed . The other two pairs give the same value: and . The triangle is acute; for example, the angle at has .
As a check through the IAC, write . Each pair gives , here the rows , and . The null vector is , which gives and again. A script with exact fractions confirmed every number in this section.
Correctness
The full proof is in proofs/correctness.md. In outline: orthogonality gives for each pair, and the difference of two of these equations is an altitude equation, so the true solves the system of line L4. The three vectors are independent, so the are not collinear, and the solution is unique. Then line L5 gives , and line L7 gives . Two more facts come from the same identity. The triangle of a real camera is always acute, because the angle at has . And every input that the algorithm accepts is the exact set of vanishing points of the camera it returns; so a rejection at line L3 or L5 proves that no camera with zero skew and square pixels fits the input.
Proof: Three exact vanishing points of orthogonal directions give K and R, and a returned answer always fits the inputfull
We use the notation of the page. The camera has zero skew and square pixels, so with and . The rotation holds the three orthogonal scene directions in the camera frame, and is the component of . The vanishing point of direction is . It is a finite pixel exactly when , and then
For a point of the image we write , and .
Claim. (a) Let be the exact vanishing points of a camera as above, all finite. Then the points are not on one line, the triangle has three acute angles, and lines L1 to L10 return the true and a rotation whose columns are . (b) In exact arithmetic, for any three input points that lines L3 and L5 accept, the returned is a rotation and for . So every accepted input is the exact set of vanishing points of some camera with zero skew and square pixels. (c) If line L3 or line L5 rejects the input, then no camera with zero skew and square pixels has these three points as finite vanishing points of three orthogonal directions.
Step 1: the key identity
Let . The directions are orthogonal, so . Divide by and use the formula for :
Step 2: the points are not collinear and the triangle is acute
The vectors are images of three independent vectors under the invertible , so they are independent. If were on one line , then for all three, and three independent vectors cannot all be orthogonal to one nonzero vector in . So the points are not collinear, and the vectors and are independent. The determinant of line L3 is not zero.
For the angle at , with , (1) gives
So every angle of the triangle is acute.
Step 3: lines L1 to L6 return c and f
Lines L1 and L2 set , , and . Line L4 solves the system
by Cramer's rule. The first equation says , so is on the altitude from ; the second says is on the altitude from . By Step 2, , so (2) has exactly one solution, the orthocenter. The true solves (2), because by (1)
and in the same way . So line L4 returns . Line L5 computes by (1), so it does not reject, and line L6 sets the true and .
Step 4: lines L7 to L10 return a rotation with columns plus or minus r_i
Line L7 computes and divides it by its length. As , the result is with . Line L8 forms , with . If this is , line L8 negates the third column, so the matrix becomes , whose determinant is . In both cases the matrix is a rotation with columns . Line L9 runs the iteration . For a rotation , so the iteration does not change it. This proves (a).
Step 5: a returned answer fits the input (part b)
Take any three input points for which lines L3 and L5 do not reject. Let be the solution of (2) and write . The first equation of (2) says , so . The second says , so . So all three products with have the same value, and line L5 sets to minus this value, with . With and ,
So the unit vectors of line L7 are orthonormal, the matrix of line L8 is a rotation after the sign fix, and line L9 keeps it (Step 4). Each column is , and by the definition of , so . With rounding, the columns are only nearly orthonormal, and line L9 replaces the matrix with its orthogonal polar factor, the nearest rotation in the Frobenius norm (the determinant is positive after line L8).
Step 6: rejections are correct (part c)
Suppose some camera with zero skew and square pixels had as finite vanishing points of three orthogonal directions. Then Step 2 shows that the points are not collinear, so line L3 does not reject. Step 3 shows that the unique solution of (2) is its principal point and that there, so line L5 does not reject either. Hence a rejection at line L3 or line L5 proves that no such camera exists. In particular, three collinear points, or a triangle with a right or an obtuse angle, are rejected. For a right or obtuse angle at , the identity of Step 2 holds at the orthocenter for any triangle (its proof used only that the three products are equal, which Step 5 shows), so forces .
Cost
Each line does a fixed number of arithmetic operations on numbers of the real-RAM, except line L9, which runs at most a fixed number (60) of iterations of fixed cost on a matrix. So the method takes time and extra space.
Complexity
In the real-RAM model, lines L1 to L8 do a fixed number of arithmetic operations and three square roots: time and extra space. Line L9 runs at most a fixed number of iterations on a matrix, so the total stays . For an exact rotation it stops after one iteration. The real cost of calibration from vanishing points is in vanishing point detection, which is linear or worse in the number of line segments.
Properties
- Closed form. No start value and no iteration, apart from the final projection onto rotations.
- Order free. Any order of the three points gives the same ; only the order and the signs of the columns of change. The tests check all six orders.
- Uses one image. The method needs no motion and no known lengths. In return it needs strong assumptions: zero skew, square pixels and no distortion.
- Invariant to scale of the scene and to translation. Vanishing points do not depend on or on the size of the scene, so neither does the result.
- Exact on exact data. With exact vanishing points, the output is the true and up to the signs of the columns.
Variants and improvements
- Known principal point. With assumed at the image center, two orthogonal vanishing points give , as above. This is the common case when one direction (often the vertical) is nearly parallel to the image plane and its vanishing point is far away.
- More than three points or images. Each orthogonal pair gives one linear equation on the IAC. With more pairs, from several images or several boxes, stack the equations and solve them by least squares, then get from by the Cholesky decomposition. Other linear constraints fit in the same system: zero skew, square pixels, and the images of circular points from Zhang's method (HZ Section 8.8).
- Weaker camera models. Without the square pixel assumption, has 4 unknowns up to scale. Three orthogonal vanishing points give only 3 equations, so one more constraint is needed, for example a vanishing line together with the vanishing point of its normal direction (HZ Section 8.8).
- Refinement. After the closed form, the line segments that support each vanishing point can be refitted jointly with and , so that the three points are exactly orthogonal. This is a small nonlinear least squares problem.
- Manhattan world detection. Detectors that assume three orthogonal directions estimate (or only ) and the three vanishing points together, so the orthogonality constraint helps the detection itself.
When to use it
Use it when you have only one image, or images from a camera you cannot calibrate in advance, of a scene with clear orthogonal structure: buildings, rooms, roads with poles, boxes. It is also a good way to get a start value for bundle adjustment or to check a calibration. Do not use it when you can print a pattern and take several photos; Zhang's method is much more accurate and also estimates distortion. Do not use it with a strong wide-angle lens unless the distortion is removed first.
Pitfalls
- A vanishing point far away. If one direction is nearly parallel to the image plane, its vanishing point is far outside the image and its position is poorly determined. The orthocenter then moves a lot for small errors. In the limit, with one vanishing point at infinity, the principal point is only known to lie on the line through the other two, and must be assumed. Fall back to the two point variant.
- Directions that are not orthogonal. The method trusts that the three directions are at right angles. Real buildings have walls that are a few degrees off, and a wrong pairing of a line to a direction moves a vanishing point.
- Distortion. Radial distortion bends straight lines, so the line intersections do not meet at one point. Undistort the segments first, for example with the Brown-Conrady model.
- Noise. In the tests, Gaussian noise with sigma = 1 pixel on each vanishing point of a camera with gave focal length errors up to about 3 pixels and principal point errors up to about 5 pixels over 40 random views. This is a measurement on synthetic data with nearby vanishing points, not a bound. Errors on real images are much larger, because a vanishing point from a few short segments can be off by many pixels.
- Signs of the columns. Each column of is known only up to sign. Fix the signs with extra knowledge, such as the vertical pointing up, before you use .
- A rejection is information. A right or obtuse triangle, or a nonpositive , means that the points cannot come from a camera with zero skew and square pixels. Usually one vanishing point is wrong. Do not take the absolute value of .
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/vanishing-point-calibration/. The authors are listed in prov/QBYP5B.jsonl.
"""Camera calibration from three vanishing points of orthogonal directions.
The method of Caprile and Torre (1990), in the notation of the area: pixel
(u, v) with u to the right and v down, K = [[f, 0, c_x], [0, f, c_y], [0, 0, 1]]
(zero skew and square pixels), and a vanishing point v_i ~ K r_i, where r_i
is the direction of the i-th scene axis in the camera frame. Then the
principal point c is the orthocenter of the triangle v_1 v_2 v_3 and
f^2 = -(v_1 - c) . (v_2 - c).
The vanishing points are finite pixels (x, y). The functions orthocenter and
focal_squared use only +, -, * and /, so they also accept Fraction inputs
and then give exact results.
"""
from __future__ import annotations
import math
from typing import List, Sequence, Tuple
Point = Sequence[float]
Matrix = List[List[float]]
def _dot2(a: Point, b: Point):
return a[0] * b[0] + a[1] * b[1]
def _sub2(a: Point, b: Point):
return (a[0] - b[0], a[1] - b[1])
def orthocenter(v1: Point, v2: Point, v3: Point, rel_tol: float = 1e-12):
"""Intersection of the altitudes of the triangle v1 v2 v3.
The altitude from v3 is the set of c with (c - v3) . (v1 - v2) = 0, and
the altitude from v1 is the set of c with (c - v1) . (v2 - v3) = 0.
Raises ValueError if the three points are (nearly) on one line.
"""
a1 = _sub2(v1, v2) # @line L1
b1 = _dot2(a1, v3) # @line L1
a2 = _sub2(v2, v3) # @line L2
b2 = _dot2(a2, v1) # @line L2
D = a1[0] * a2[1] - a1[1] * a2[0] # @line L3
if D == 0 or abs(D) <= rel_tol * math.sqrt(float(_dot2(a1, a1) * _dot2(a2, a2))): # @line L3
raise ValueError("the vanishing points are collinear") # @line L3
cx = (b1 * a2[1] - a1[1] * b2) / D # @line L4
cy = (a1[0] * b2 - a2[0] * b1) / D # @line L4
return (cx, cy)
def focal_squared(v1: Point, v2: Point, c: Point):
"""f^2 = -(v1 - c) . (v2 - c) for two orthogonal vanishing points."""
return -_dot2(_sub2(v1, c), _sub2(v2, c)) # @line L5
def focal_from_two(v1: Point, v2: Point, c: Point) -> float:
"""Focal length from two orthogonal vanishing points and a known
principal point c (the two point variant)."""
q = focal_squared(v1, v2, c)
if q <= 0:
raise ValueError("the points do not come from orthogonal directions")
return math.sqrt(q)
def make_K(f: float, cx: float, cy: float) -> Matrix:
return [[f, 0.0, cx], [0.0, f, cy], [0.0, 0.0, 1.0]]
def _det3(A: Matrix) -> float:
return (A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])
- A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])
+ A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]))
def _inv3(A: Matrix) -> Matrix:
d = _det3(A)
C = [[A[1][1] * A[2][2] - A[1][2] * A[2][1], A[0][2] * A[2][1] - A[0][1] * A[2][2],
A[0][1] * A[1][2] - A[0][2] * A[1][1]],
[A[1][2] * A[2][0] - A[1][0] * A[2][2], A[0][0] * A[2][2] - A[0][2] * A[2][0],
A[0][2] * A[1][0] - A[0][0] * A[1][2]],
[A[1][0] * A[2][1] - A[1][1] * A[2][0], A[0][1] * A[2][0] - A[0][0] * A[2][1],
A[0][0] * A[1][1] - A[0][1] * A[1][0]]]
return [[C[i][j] / d for j in range(3)] for i in range(3)]
def nearest_rotation(M: Matrix, tol: float = 1e-15, max_iter: int = 60) -> Matrix:
"""Orthogonal polar factor of M by the Newton iteration
M <- (M + M^-T) / 2. For det M > 0 the result is the rotation nearest
to M in the Frobenius norm. A rotation is a fixed point."""
X = [list(map(float, row)) for row in M]
for _ in range(max_iter):
Xi = _inv3(X)
Y = [[(X[i][j] + Xi[j][i]) / 2.0 for j in range(3)] for i in range(3)]
change = max(abs(Y[i][j] - X[i][j]) for i in range(3) for j in range(3))
X = Y
if change <= tol:
break
return X
def calibrate(v1: Point, v2: Point, v3: Point) -> Tuple[Matrix, Matrix]:
"""Return (K, R) from three finite vanishing points of orthogonal
directions. The columns of R are the scene axes in the camera frame,
each known only up to sign; this function picks the sign with a
positive Z component for r_1 and r_2 and fixes det R = +1 with r_3.
Raises ValueError for collinear points or an obtuse or right triangle.
"""
vs = [(float(v[0]), float(v[1])) for v in (v1, v2, v3)]
c = orthocenter(*vs) # @line L1
q = focal_squared(vs[0], vs[1], c) # @line L5
if not q > 0: # @line L5
raise ValueError("no real focal length: the triangle is not acute") # @line L5
f = math.sqrt(q) # @line L6
K = make_K(f, c[0], c[1]) # @line L6
cols = []
for v in vs:
d = ((v[0] - c[0]) / f, (v[1] - c[1]) / f, 1.0) # @line L7
n = math.sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]) # @line L7
cols.append([x / n for x in d]) # @line L7
R = [[cols[j][i] for j in range(3)] for i in range(3)] # @line L8
if _det3(R) < 0: # @line L8
for i in range(3): # @line L8
R[i][2] = -R[i][2] # @line L8
R = nearest_rotation(R) # @line L9
return K, R # @line L10
test_vanishing_point_calibration.py 202 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/vanishing-point-calibration/. The authors are listed in prov/QBYP5B.jsonl.
"""Tests of calibration from three orthogonal vanishing points."""
import itertools
import math
import random
from fractions import Fraction
import pytest
from vanishing_point_calibration import (calibrate, focal_from_two, focal_squared,
nearest_rotation, orthocenter)
# The worked example of the page: f = 800, c = (320, 240), and the scene axes
# have the camera frame directions (2, 0, 1), (-0.5, 1, 1), (-0.5, -1.25, 1).
V1, V2, V3 = (1920, 240), (-80, 1040), (-80, -760)
def quat_to_R(q):
w, x, y, z = q
return [[1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y)],
[2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x)],
[2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)]]
def random_rotation_facing(rng, min_z=0.25):
"""A random rotation whose three columns all have Z >= min_z, so that
the three vanishing points are finite and not too far away."""
while True:
q = [rng.gauss(0.0, 1.0) for _ in range(4)]
n = math.sqrt(sum(a * a for a in q))
R = quat_to_R([a / n for a in q])
if min(R[2][i] for i in range(3)) >= min_z:
return R
def vanishing_points(f, cx, cy, R):
return [(f * R[0][i] / R[2][i] + cx, f * R[1][i] / R[2][i] + cy) for i in range(3)]
def det3(A):
return (A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])
- A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])
+ A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]))
def rotation_angle_deg(A, B):
M = [[sum(A[k][i] * B[k][j] for k in range(3)) for j in range(3)] for i in range(3)]
tr = M[0][0] + M[1][1] + M[2][2]
return math.degrees(math.acos(max(-1.0, min(1.0, (tr - 1.0) / 2.0))))
def is_rotation(R, tol):
for i in range(3):
for j in range(3):
g = sum(R[k][i] * R[k][j] for k in range(3))
if abs(g - (1.0 if i == j else 0.0)) > tol:
return False
return abs(det3(R) - 1.0) <= tol
def iac_reference(v1, v2, v3):
"""Reference method: solve the IAC omega = [[w1, 0, w2], [0, w1, w3],
[w2, w3, w4]] (zero skew, square pixels) from v_i^T omega v_j = 0 as the
null vector of a 3 x 4 matrix, by cofactors, then read off c and f."""
A = []
for a, b in ((v1, v2), (v1, v3), (v2, v3)):
A.append([a[0] * b[0] + a[1] * b[1], a[0] + b[0], a[1] + b[1], 1.0])
def minor(k):
cols = [j for j in range(4) if j != k]
return det3([[row[j] for j in cols] for row in A])
w = [(-1) ** k * minor(k) for k in range(4)]
cx, cy = -w[1] / w[0], -w[2] / w[0]
f2 = w[3] / w[0] - cx * cx - cy * cy
return cx, cy, f2
def test_worked_example_exact():
vs = [tuple(Fraction(x) for x in v) for v in (V1, V2, V3)]
c = orthocenter(*vs)
assert c == (Fraction(320), Fraction(240))
for a, b in itertools.combinations(vs, 2):
assert focal_squared(a, b, c) == Fraction(640000)
def test_worked_example_rotation():
K, R = calibrate(V1, V2, V3)
assert abs(K[0][0] - 800.0) < 1e-9 and abs(K[1][1] - 800.0) < 1e-9
assert abs(K[0][2] - 320.0) < 1e-9 and abs(K[1][2] - 240.0) < 1e-9
dirs = [(2.0, 0.0, 1.0), (-0.5, 1.0, 1.0), (-0.5, -1.25, 1.0)]
for j, d in enumerate(dirs):
n = math.sqrt(sum(x * x for x in d))
col = [R[i][j] for i in range(3)]
s = 1.0 if j < 2 else math.copysign(1.0, sum(c * x for c, x in zip(col, d)))
assert all(abs(col[i] - s * d[i] / n) < 1e-12 for i in range(3))
assert is_rotation(R, 1e-12)
def test_random_exact_recovery_and_reference():
rng = random.Random(20250101)
for _ in range(60):
f = rng.uniform(300.0, 1500.0)
cx, cy = rng.uniform(200.0, 450.0), rng.uniform(150.0, 330.0)
Rt = random_rotation_facing(rng)
vs = vanishing_points(f, cx, cy, Rt)
K, R = calibrate(*vs)
assert abs(K[0][0] - f) <= 1e-8 * f
assert math.hypot(K[0][2] - cx, K[1][2] - cy) <= 1e-8 * f
assert K[0][1] == 0.0 and K[0][0] == K[1][1]
assert is_rotation(R, 1e-10)
for j in range(3):
dot = sum(R[i][j] * Rt[i][j] for i in range(3))
assert abs(abs(dot) - 1.0) <= 1e-9
rx, ry, rf2 = iac_reference(*vs)
assert math.hypot(rx - K[0][2], ry - K[1][2]) <= 1e-6 * f
assert abs(math.sqrt(rf2) - K[0][0]) <= 1e-6 * f
def test_order_does_not_matter():
rng = random.Random(5)
Rt = random_rotation_facing(rng)
vs = vanishing_points(900.0, 330.0, 250.0, Rt)
K0, _ = calibrate(*vs)
for perm in itertools.permutations(vs):
K, R = calibrate(*perm)
assert all(abs(K[i][j] - K0[i][j]) <= 1e-8 * 900.0 for i in range(3) for j in range(3))
assert is_rotation(R, 1e-10)
def test_orthocenter_property_and_acute_triangle():
rng = random.Random(11)
for _ in range(30):
Rt = random_rotation_facing(rng)
vs = vanishing_points(700.0, 300.0, 200.0, Rt)
c = orthocenter(*vs)
for i in range(3):
a, b, o = vs[i], vs[(i + 1) % 3], vs[(i + 2) % 3]
side = (a[0] - b[0], a[1] - b[1])
alt = (c[0] - o[0], c[1] - o[1])
scale = math.hypot(*side) * max(1.0, math.hypot(*alt))
assert abs(side[0] * alt[0] + side[1] * alt[1]) <= 1e-9 * scale
u = (a[0] - o[0], a[1] - o[1])
w = (b[0] - o[0], b[1] - o[1])
assert u[0] * w[0] + u[1] * w[1] > 0.0
def test_two_point_variant_with_known_principal_point():
rng = random.Random(3)
for _ in range(20):
f = rng.uniform(400.0, 1200.0)
Rt = random_rotation_facing(rng)
vs = vanishing_points(f, 320.0, 240.0, Rt)
for a, b in itertools.combinations(vs, 2):
assert abs(focal_from_two(a, b, (320.0, 240.0)) - f) <= 1e-8 * f
def test_noise():
# sigma = 1 pixel of Gaussian noise on each vanishing point, 40 cameras
# with f = 800 and c = (320, 240); tolerances 10 px, 15 px and 1 degree.
rng = random.Random(7)
for _ in range(40):
Rt = random_rotation_facing(rng, min_z=0.3)
vs = [(x + rng.gauss(0.0, 1.0), y + rng.gauss(0.0, 1.0))
for x, y in vanishing_points(800.0, 320.0, 240.0, Rt)]
K, R = calibrate(*vs)
assert abs(K[0][0] - 800.0) < 10.0
assert math.hypot(K[0][2] - 320.0, K[1][2] - 240.0) < 15.0
assert is_rotation(R, 1e-10)
assert rotation_angle_deg(R, Rt) < 1.0
def test_collinear_points_raise():
with pytest.raises(ValueError):
calibrate((0.0, 0.0), (100.0, 50.0), (300.0, 150.0))
with pytest.raises(ValueError):
orthocenter((Fraction(1), Fraction(2)), (Fraction(3), Fraction(4)), (Fraction(5), Fraction(6)))
def test_obtuse_or_right_triangle_raises():
# The angle at (1, 1) is obtuse, so no real focal length exists.
with pytest.raises(ValueError):
calibrate((0.0, 0.0), (10.0, 0.0), (1.0, 1.0))
c = orthocenter((Fraction(0), Fraction(0)), (Fraction(10), Fraction(0)), (Fraction(1), Fraction(1)))
assert focal_squared((0, 0), (10, 0), c) < 0
# A right angle at (0, 0): the orthocenter is that vertex and f^2 = 0.
with pytest.raises(ValueError):
calibrate((0.0, 0.0), (10.0, 0.0), (0.0, 7.0))
with pytest.raises(ValueError):
focal_from_two((0.0, 0.0), (10.0, 0.0), (5.0, 5.0))
def test_nearest_rotation_fixes_rotations_and_projects():
rng = random.Random(2)
R = random_rotation_facing(rng, min_z=0.0)
assert all(abs(a - b) <= 1e-14 for ra, rb in zip(nearest_rotation(R), R) for a, b in zip(ra, rb))
M = [[R[i][j] + rng.uniform(-0.01, 0.01) for j in range(3)] for i in range(3)]
Q = nearest_rotation(M)
assert is_rotation(Q, 1e-12)
assert rotation_angle_deg(Q, R) < 2.0
History
The orthocenter property of three orthogonal vanishing points was shown by Bruno Caprile and Vincent Torre in 1990, in a paper that also recovered the rotation of the camera from the same vanishing points. Hartley and Zisserman later described the same method with the image of the absolute conic: each orthogonal pair is a linear constraint on , and other known properties of the camera add more linear constraints. Since then, the method has been a standard tool for single image metrology and for calibration in man-made scenes.
References
- Bruno Caprile and Vincent Torre, Using vanishing points for camera calibration, International Journal of Computer Vision 4(2), 1990, 127-139, the orthocenter property of the principal point, the focal length from two orthogonal vanishing points, and the rotation from the vanishing points
- Richard Hartley and Andrew Zisserman, Multiple View Geometry in Computer Vision, second edition, Cambridge University Press, 2004, Section 8.6, Vanishing points and vanishing lines; Section 8.8, Determining calibration K from a single view (the linear constraints on omega and the orthocenter result)
- Richard Szeliski, Computer Vision, Algorithms and Applications, second edition, Springer, 2022, Chapter 11, Structure from motion and SLAM (the part on geometric intrinsic calibration from vanishing points)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag QBYP5B Edit this page History Report an error Markdown