Algorithmcore
FAST corner detector
Also called features from accelerated segment test.
Call a pixel a corner when n contiguous pixels of the 16 on a circle around it are all brighter or all darker by a threshold, tested in an order learned by a decision tree.
- Solves
- Corner detection
- Time
- O(W H n) worstO(W H) average · best
- Space
- O(W H) extra
- Where
- H = the image height in pixels; W = the image width in pixels; n = the length of the arc
- Model
- Word RAM
- Properties
- deterministicexactheuristicnot rotation invariant responsenot scale invariant
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Edward Rosten, Tom Drummond, 2006
The FAST corner detector (features from accelerated segment test) solves corner detection with comparisons only. It looks at the 16 pixels on a circle of radius 3 around a pixel and calls a corner when contiguous pixels of the circle are all brighter than or all darker than . This is the segment test. Unlike the Harris and Shi-Tomasi detectors it computes no gradients and no sums, and most pixels are rejected after two comparisons. Edward Rosten and Tom Drummond made the test fast by learning, with a decision tree, the order in which to read the circle pixels. The cost is for a image. The score that ranks the corners for non-maximum suppression is the largest threshold for which the pixel is still a corner. FAST is the detector inside ORB.
In plain words
Draw a small circle of 16 pixels around a pixel. If a long unbroken part of the circle, say 9 pixels in a row, is clearly brighter than the center, or clearly darker, then the center is a corner. On a flat area the circle looks like the center. On a straight edge about half of the circle is brighter and half is the same, and half of 16 is not enough. Only near a corner or a small spot does a long arc differ from the center. The test needs no arithmetic beyond comparisons, and most pixels fail it after a look at two circle pixels. To keep one point per corner, give each corner a score, the largest threshold at which it still passes, and drop corners with a stronger neighbour.
The problem
The input is a gray digital image with columns and rows of integer values, an integer threshold and an arc length with . A pixel has integer coordinates : is the column and goes right, is the row and goes down. The circle is the discrete circle of radius 3 with 16 pixels, numbered 1 to 16 clockwise from the top:
Pixels 1, 5, 9 and 13 are the top, right, bottom and left. Only the pixels with and are tested, so the circle is inside the image. A circle pixel is brighter when and darker when ; this follows equation 8 of the 2010 paper. Some implementations use strict inequalities, which for integer images is the same test with . The pixel is a corner when there is a run of contiguous circle pixels, with the run allowed to wrap from pixel 16 to pixel 1, that are all brighter or all darker.
The score is the largest for which is still a corner. For integer images it has a closed form. For an arc of contiguous circle pixels, all pixels of are brighter for threshold exactly when , and all are darker exactly when . So
and is a corner for the threshold exactly when . The output is the set of corners whose score is not smaller than the score of any corner among their eight neighbours.
The algorithm
FAST(I, t, n) ▷ I is W × H with integer values, t ≥ 1- 1
for each pixel p with 3 ≤ u ≤ W - 4 and 3 ≤ v ≤ H - 4 do - 2
if n = 12 and fewer than 3 of the pixels 1, 5, 9, 13 are brighter and fewer than 3 are darker then skip p - 3
V(p) ← the largest, over the 16 arcs A of n contiguous circle pixels, of max(min over A of I(x) - I_p, min over A of I_p - I(x)) - 4
if V(p) ≥ t then add p to the candidates C - 5
corners ← ∅ - 6
for each p in C do - 7
if V(p) ≥ V(q) for each q in C with q one of the 8 neighbours of p then - 8
add p to corners - 9
return corners
Line L2 is the high-speed test of the original detector. It is valid only for : a run of 12 contiguous pixels leaves out a run of 4, and any 4 contiguous pixels hold exactly one of the pixels 1, 5, 9 and 13, so a corner has at least 3 of the four brighter or at least 3 darker. For a run can hold only two of them, and the test would reject real corners. Line L3 computes the score, and line L4 is the segment test, since is the same as a run of brighter or darker pixels. Lines L5 to L8 are a non-maximum suppression on the scores, as in Section III-C of the 2010 paper. The test at line L7 is not strict, so a group of neighbours with the same score is kept whole.
A real FAST detector does not run line L3 on every pixel. It first runs the segment test as a learned decision tree that reads one circle pixel at a time and stops as soon as the answer is known, and it computes the score only for the few pixels that pass. The tree changes the speed, not the output.
The learned tree
Each circle pixel of is in one of three states: darker, similar or brighter. Rosten and Drummond collect the 16 states of every pixel in a set of training images, label each pixel by the segment test, and grow a tree with the ID3 rule: at each node, read the circle pixel whose state gives the most information about the label. They add all state patterns with a low weight, so the tree computes the segment test exactly and not only on the training images. The tree is then written out as nested if-else code. The order of the questions is tuned to the images, which is the source of the speed. The paper also forces the second question to be the same on every branch, so the first two tests can run on a strip of pixels with vector instructions; most pixels are rejected after these two tests.
Example
Take an image with everywhere except a block of s at and , with and . At the corner pixel the 16 circle values, from pixel 1 to pixel 16, are
The 11 zeros from pixel 10 around to pixel 4 are one run, all darker than . An arc of 9 of them gives , so . A pixel just outside the block, such as , sees only 3 bright pixels on its circle and fails.
The corners are the six pixels , , , , and , all with score 9. Since the scores are equal, the suppression keeps all six; a strict test would drop all six. For there are no corners at all: a right angle leaves only 11 darker pixels on the circle. This is one reason why FAST-9 replaced FAST-12. For there are no corners either, since no score is above 9.
Correctness
The output is the set defined in The problem. The proof in proofs/correctness.md shows three facts:
- the closed form of is the largest threshold for which passes the segment test, and the test passes for exactly when ;
- the high-speed test of line L2 never rejects a corner when , so it changes the speed and not the output;
- lines L5 to L9 return exactly the candidates that are not smaller than any candidate neighbour.
For the second fact fails: the circle with pixels 2 to 10 darker and the rest equal to the center is a corner, but only two of the pixels 1, 5, 9 and 13 are darker. That a FAST corner is a good corner for a given task is not a theorem; the 2010 paper measures it as repeatability on image sequences.
Proof: Correctness of the FAST corner detectorfull
Fix an integer image , a pixel that is at least 3 pixels from each border, and an arc length with . Number the circle pixels in the order of the page, and let an arc be a set for . For a threshold , passes the segment test when some arc has all its pixels brighter () or all darker ().
Lemma 1 (the score). Let , and . For each integer , passes the segment test with threshold if and only if . So is the largest threshold for which passes, and the set of thresholds for which it passes is .
Proof. All pixels of are brighter for exactly when for each , that is when . In the same way all are darker exactly when . So passes exactly when or for some , which is when the largest of these numbers, , is at least . Since is an integer, the passing thresholds are the integers up to , and the largest is .
Lemma 2 (the high-speed test for ). If and passes the segment test for , then at least 3 of are brighter, or at least 3 are darker.
Proof. Let be an arc of 12 pixels that are all brighter (the darker case is the same). Its complement on the circle is the arc of the 4 positions modulo 16. Four consecutive integers hold exactly one multiple of 4, so the complement holds exactly one of the positions , and holds the other three. All pixels of are brighter.
So line L2 skips only pixels that would fail the test of line L4, and the set of candidates is the same with or without it. For the complement has positions and can hold two of the four, so the lemma fails; the page gives an example with .
Theorem. FAST returns the set of pixels that pass the segment test for and whose score is not smaller than the score of any passing pixel among their eight neighbours.
Proof. Line L1 visits each testable pixel once. By Lemma 2, line L2 skips only pixels that fail the segment test. Line L3 computes as in Lemma 1, and by Lemma 1 line L4 adds to exactly when passes. So after the first loop is the set of passing pixels, each with its score. Lines L6 to L8 add to the output exactly when for each among its eight neighbours, and no line removes a pixel. Line L9 returns this set.
Remark (rotation by ). The map , which is the rotation that sends pixel of a image to pixel of the rotated image, sends to ; this can be checked on the 16 offsets. A cyclic shift maps arcs to arcs, so the score of each pixel is the same as the score of its image, and the output of the rotated image is the rotated output.
Complexity
The model is the word RAM with integer pixels.
- Line L2 reads 4 pixels, and line L3 reads 16 pixels and takes the minimum over 16 arcs of pixels, which is time per pixel with the direct loop, or with a sliding minimum over the circle. Over pixels this is , which is since .
- Lines L6 and L7 cost per candidate.
- A hash set or a table of scores takes extra space.
The decision tree reads at most 16 pixels per pixel and, for most pixels of typical images, two. The output does not depend on the tree, only the time does.
Properties
- Comparisons only. The segment test uses comparisons with , so it suits 8-bit images and simple hardware.
- Offset invariant. Adding a constant to the image changes nothing. A gain changes which pixels pass for a fixed , since is absolute.
- Rotation. The circle maps to itself under a rotation by (a cyclic shift by 4 positions) and under a mirror, so the detector commutes with these. For other angles the discrete circle is only close to rotation invariant.
- Not scale invariant. The circle has a fixed radius of 3 pixels. ORB and BRISK run FAST on each level of an image pyramid.
- Clusters. The test is true for several adjacent pixels at one corner, as the example shows. The suppression thins them only when the scores differ.
- Spots and lines. A single bright pixel passes for any , and a thin line passes for small . The segment test answers "a long arc differs from the center", which includes more than corners.
Variants and improvements
- FAST-12 and FAST-9. The original test used because of the high-speed test. The 2010 paper reports that gives the best repeatability.
- FAST-ER. The 2010 paper also learns the shape of the test itself, with a larger set of pixels around , by simulated annealing that maximizes repeatability. The result is slower than FAST-9 and more repeatable.
- AGAST. Mair and others build the tree adaptively, so it does not need training on images of the target domain.
- Scores. Other scores are the sum of the absolute differences over the arc, or a Harris response computed at each FAST corner; ORB uses the Harris response to rank the corners.
- Pyramids. Detect on each level of an image pyramid to cover scales, as in ORB and BRISK.
When to use it
Use it when time is short: real-time tracking, visual odometry, SLAM on embedded processors, or a first stage that proposes candidates for a slower descriptor. Pair it with a pyramid for scale and with a Harris or Shi-Tomasi score if the corner quality matters. For precise localization or for matching under large changes of scale or viewpoint, use a detector with scale selection such as the DoG detector.
Pitfalls
- High-speed test with . The test with pixels 1, 5, 9 and 13 is only valid for . For it rejects true corners, as the example in Correctness shows.
- Threshold zero. With and the non-strict inequalities, a circle pixel equal to the center is both brighter and darker, and every pixel is a corner. Use .
- Wrap around. A run can cross from pixel 16 to pixel 1. Code that scans the circle once from pixel 1 misses those runs.
- Border. The circle needs 3 pixels on each side. Pixels within 3 of the border cannot be tested.
- Plateaus in the suppression. Equal scores on neighbours, as in the example, are kept by a test with and all dropped by a test with . Neither is right in general; break ties by position if one point per corner is needed.
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/fast-corner-detector/. The authors are listed in prov/99A606.jsonl.
"""FAST corner detector (features from accelerated segment test).
An image is a list of H rows of W integers; pixel (u, v) is img[v][u], with
u the column (to the right) and v the row (down). The test reads the 16
pixels of a circle of radius 3 around p, numbered 1 to 16 clockwise from the
top. A pixel x of the circle is brighter when I(x) >= I(p) + t and darker
when I(x) <= I(p) - t (equation 8 of Rosten, Porter and Drummond, 2010).
p is a corner when n contiguous circle pixels are all brighter or all darker.
The score V(p) is the largest t for which p is still a corner. For integer
images it has the closed form
V(p) = max over the 16 arcs A of n contiguous circle pixels of
max(min over x in A of (I(x) - I(p)), min over x in A of (I(p) - I(x))),
and p is a corner for the threshold t >= 1 exactly when V(p) >= t.
Only pixels with 3 <= u <= W - 4 and 3 <= v <= H - 4 are tested.
"""
from typing import Dict, List, Sequence, Tuple
Image = Sequence[Sequence[int]]
# Circle offsets (du, dv) for the pixels 1 to 16, clockwise from the top.
CIRCLE: Tuple[Tuple[int, int], ...] = (
(0, -3), (1, -3), (2, -2), (3, -1), (3, 0), (3, 1), (2, 2), (1, 3),
(0, 3), (-1, 3), (-2, 2), (-3, 1), (-3, 0), (-3, -1), (-2, -2), (-1, -3),
)
COMPASS = (0, 4, 8, 12) # the pixels 1, 5, 9 and 13
def ring(img: Image, u: int, v: int) -> List[int]:
"""The 16 circle values around (u, v), in the order of CIRCLE."""
return [img[v + dv][u + du] for du, dv in CIRCLE]
def high_speed_reject(center: int, values: List[int], t: int) -> bool:
"""True when fewer than 3 of the pixels 1, 5, 9, 13 are brighter and fewer than 3 are darker.
Valid for n = 12 only: then a rejected pixel is never a corner.
"""
bright = sum(1 for i in COMPASS if values[i] >= center + t)
dark = sum(1 for i in COMPASS if values[i] <= center - t)
return bright < 3 and dark < 3
def fast_score(center: int, values: List[int], n: int) -> int:
"""V(p): the largest t for which some arc of n contiguous pixels is all brighter or all darker."""
best = None
for s in range(16):
arc = [values[(s + k) % 16] for k in range(n)]
up = min(x - center for x in arc)
down = min(center - x for x in arc)
cand = up if up > down else down
if best is None or cand > best:
best = cand
return best
def fast_scores(img: Image, t: int = 20, n: int = 9, high_speed: bool = True) -> Dict[Tuple[int, int], int]:
"""Return {(u, v): V} for the pixels that pass the segment test with threshold t."""
h, w = len(img), len(img[0])
found: Dict[Tuple[int, int], int] = {}
for v in range(3, h - 3): # @line L1
for u in range(3, w - 3):
c = img[v][u]
vals = ring(img, u, v)
if high_speed and n == 12 and high_speed_reject(c, vals, t): # @line L2
continue
score = fast_score(c, vals, n) # @line L3
if score >= t: # @line L4
found[(u, v)] = score
return found
def fast_corners(img: Image, t: int = 20, n: int = 9, nms: bool = True, high_speed: bool = True) -> List[Tuple[int, int]]:
"""Return the corners (u, v), sorted by v then u.
Precondition: img is a non-empty rectangular list of integer rows,
t >= 1 and 1 <= n <= 16. With nms, a corner is kept when its score is
not smaller than the score of any detected corner among its 8 neighbours.
"""
scores = fast_scores(img, t, n, high_speed)
keys = sorted(scores, key=lambda p: (p[1], p[0]))
if not nms:
return keys
out: List[Tuple[int, int]] = [] # @line L5
for u, v in keys: # @line L6
s = scores[(u, v)]
if all(scores.get((u + du, v + dv), s) <= s for du in (-1, 0, 1) for dv in (-1, 0, 1)): # @line L7
out.append((u, v)) # @line L8
return out # @line L9
test_fast_corner_detector.py 139 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/fast-corner-detector/. The authors are listed in prov/99A606.jsonl.
import random
from fast_corner_detector import CIRCLE, COMPASS, fast_corners, fast_scores, high_speed_reject, ring
def segment_test(img, u, v, t, n):
"""Brute force: is there a run of n circle pixels, all brighter or all darker?"""
c = img[v][u]
vals = [img[v + dv][u + du] for du, dv in CIRCLE]
for start in range(16):
run = [vals[(start + k) % 16] for k in range(n)]
if all(x >= c + t for x in run) or all(x <= c - t for x in run):
return True
return False
def oracle(img, t, n):
"""Score by a linear scan over thresholds, then 3 x 3 suppression."""
h, w = len(img), len(img[0])
scores = {}
for v in range(3, h - 3):
for u in range(3, w - 3):
if segment_test(img, u, v, t, n):
s = t
while segment_test(img, u, v, s + 1, n):
s += 1
scores[(u, v)] = s
keep = []
for (u, v), s in scores.items():
if all(scores.get((u + a, v + b), -1) <= s for a in (-1, 0, 1) for b in (-1, 0, 1)):
keep.append((u, v))
return sorted(keep, key=lambda p: (p[1], p[0])), scores
def block():
return [[9 if (u >= 5 and v >= 5) else 0 for u in range(11)] for v in range(11)]
def test_circle_is_a_bresenham_circle_of_radius_3():
assert len(set(CIRCLE)) == 16
assert all(8 <= du * du + dv * dv <= 10 or du * dv == 0 for du, dv in CIRCLE)
assert all(max(abs(du), abs(dv)) == 3 or (abs(du), abs(dv)) == (2, 2) for du, dv in CIRCLE)
# Consecutive pixels touch, so the circle is a closed 8-connected loop.
for i in range(16):
(a, b), (c, d) = CIRCLE[i], CIRCLE[(i + 1) % 16]
assert max(abs(a - c), abs(b - d)) == 1
assert [CIRCLE[i] for i in COMPASS] == [(0, -3), (3, 0), (0, 3), (-3, 0)]
def test_worked_example_block_corner():
img = block()
assert ring(img, 5, 5) == [0, 0, 0, 0, 9, 9, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0]
scores = fast_scores(img, 1, 9)
assert scores == {(5, 5): 9, (6, 5): 9, (7, 5): 9, (5, 6): 9, (6, 6): 9, (5, 7): 9}
assert fast_corners(img, 1, 9) == [(5, 5), (6, 5), (7, 5), (5, 6), (6, 6), (5, 7)]
# A right-angle corner leaves only 11 dark pixels on the circle, so FAST-12 misses it.
assert fast_scores(img, 1, 12) == {}
assert fast_scores(img, 10, 9) == {}
def test_flat_image_and_small_image():
assert fast_corners([[4] * 9 for _ in range(9)], 1, 9) == []
assert fast_corners([[1, 2, 3, 4, 5, 6]] * 6, 1, 9) == []
def test_single_bright_pixel():
img = [[0] * 7 for _ in range(7)]
img[3][3] = 50
assert fast_scores(img, 1, 16) == {(3, 3): 50}
assert fast_corners(img, 50, 9) == [(3, 3)]
assert fast_corners(img, 51, 9) == []
def test_high_speed_test_is_exact_for_n_12():
# For every set of brighter pixels on the circle: a run of 12 implies 3 of the 4 compass pixels.
for mask in range(1 << 16):
bits = [(mask >> i) & 1 for i in range(16)]
run12 = any(all(bits[(s + k) % 16] for k in range(12)) for s in range(16))
if run12:
assert sum(bits[i] for i in COMPASS) >= 3
def test_high_speed_test_is_wrong_for_n_9():
# A dark run of 9 that holds only 2 compass pixels: pixels 2 to 10.
vals = [10] * 16
for i in range(1, 10):
vals[i] = 0
assert sum(1 for i in COMPASS if vals[i] <= 10 - 5) == 2
assert high_speed_reject(10, vals, 5)
img = [[10] * 7 for _ in range(7)]
for (du, dv), x in zip(CIRCLE, vals):
img[3 + dv][3 + du] = x
assert segment_test(img, 3, 3, 5, 9)
assert fast_scores(img, 5, 9) == {(3, 3): 10}
def test_high_speed_flag_does_not_change_output():
rng = random.Random(12)
for _ in range(80):
img = [[rng.choice([0, 0, 5, 9, 9]) for _ in range(9)] for _ in range(9)]
assert fast_scores(img, 3, 12, True) == fast_scores(img, 3, 12, False)
def test_random_against_oracle():
rng = random.Random(2006)
for _ in range(600):
w, h = rng.randint(7, 10), rng.randint(7, 10)
img = [[rng.randint(0, 6) for _ in range(w)] for _ in range(h)]
if rng.random() < 0.5:
# Plant a bright or dark blob so that corners occur often.
cu, cv = rng.randint(3, w - 4), rng.randint(3, h - 4)
img[cv][cu] = rng.choice([0, 12])
t = rng.randint(1, 4)
n = rng.choice([9, 10, 11, 12])
want, scores = oracle(img, t, n)
assert fast_scores(img, t, n) == scores
assert fast_corners(img, t, n) == want
def test_rotation_by_90_degrees():
rng = random.Random(77)
for _ in range(60):
h, w = 9, 10
img = [[rng.randint(0, 9) for _ in range(w)] for _ in range(h)]
# rot[i][j] = img[h - 1 - j][i]: pixel (u, v) of img goes to (h - 1 - v, u).
rot = [[img[h - 1 - j][i] for j in range(h)] for i in range(w)]
a = fast_scores(img, 2, 9)
b = fast_scores(rot, 2, 9)
assert {(h - 1 - v, u): s for (u, v), s in a.items()} == b
def test_offset_invariance():
rng = random.Random(3)
for _ in range(40):
img = [[rng.randint(0, 9) for _ in range(9)] for _ in range(9)]
assert fast_scores([[x + 100 for x in r] for r in img], 2, 9) == fast_scores(img, 2, 9)
History
Rosten and Drummond first used the segment test with and the high-speed test in a system for real-time tracking. At ECCV 2006 they presented the learned version: a decision tree, grown by ID3 from the states of the circle pixels, that computes the segment test with few comparisons. In the 2010 journal version, with Reid Porter, they extended the evaluation, chose , defined the score as the largest threshold and introduced FAST-ER. FAST became the standard fast detector, and ORB (2011) made it a part of a complete feature pipeline.
References
- Edward Rosten and Tom Drummond, Machine learning for high-speed corner detection, European Conference on Computer Vision (ECCV), LNCS 3951, 2006, pages 430-443, the whole paper (the segment test, the learned decision tree and the non-maximal suppression)
- Edward Rosten, Reid Porter and Tom Drummond, Faster and better, a machine learning approach to corner detection, IEEE Transactions on Pattern Analysis and Machine Intelligence 32(1), 105-119, 2010 (arXiv 0810.2434), Section III-A (the segment test and the high-speed test), equation 8 (the three states), Section III-B (the ID3 tree and the 3^16 configurations), Section III-C (the score and the 3 x 3 suppression)
- Richard Szeliski, Computer Vision, Algorithms and Applications, second edition, Springer, 2022, Chapter 7, Section 7.1.1, Feature detectors
Where this leads
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 99A606 Edit this page History Report an error Markdown