comments to the specification
[imago.git] / src / ransac.py
1 """RANSAC estimation."""
2
3 import random
4 from math import sqrt
5 import numpy as NP
6
7 # TODO comments
8 # TODO threshold
9
10 def points_to_line((x1, y1), (x2, y2)):
11     return (y2 - y1, x1 - x2, x2 * y1 - x1 * y2)
12
13 def filter_near(data, line, distance):
14     a, b, c = line
15     dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b)
16     is_near = lambda p: dst(p) <= distance
17     return [p for p in data if is_near(p)]
18
19 def least_squares(data):
20     x = NP.matrix([(a, 1) for (a, b) in data])
21     xt = NP.transpose(x)
22     y = NP.matrix([[b] for (a, b) in data])
23     [a,c] = NP.dot(NP.linalg.inv(NP.dot(xt, x)), xt).dot(y).flat
24     return (a, -1, c)
25
26 class Linear_model:
27     def __init__(self, data):
28         self.data = data
29
30     def get(self, sample):
31         if len(sample) == 2:
32             return points_to_line(*sample)
33         else:
34             return least_squares(sample)
35
36     def initial(self):
37         return random.sample(self.data, 2)
38
39     def score(self, est, dist):
40         cons = []
41         score = 0
42         a, b, c = est
43         dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b)
44         for p in self.data:
45             d = dst(p)
46             if d <= dist:
47                 cons.append(p)
48             score += min(d, dist)
49         return score, cons
50
51     def remove(self, data):
52         self.data = list(set(self.data) - set(data))
53
54 def iterate(model, distance):
55     score = float("inf")
56     consensual = model.initial()
57     estimate = model.get(consensual)
58     new_score, new_consensual = model.score(estimate, distance)
59     if new_consensual != []:
60         while (new_score < score):
61             score, consensual = new_score, new_consensual
62             try:
63                 estimate = model.get(consensual)
64                 new_score, new_consensual = model.score(estimate, distance)
65             except (NP.linalg.LinAlgError):
66                 pass
67     return score, estimate, consensual
68         
69 def estimate(data, dist, k, modelClass=Linear_model, model=None):
70     if not model:
71         model = modelClass(data)
72     best = float("inf")
73     estimate = None
74     consensual = None
75     for i in xrange(0, k):
76         new, new_estimate, new_consensual = iterate(model, dist)
77         if new < best:
78             best = new
79             estimate = new_estimate
80             consensual = new_consensual
81
82     return estimate, consensual
83
84 def ransac_multi(m, data, dist, k, modelClass=Linear_model, model=None):
85     if not model:
86         model = modelClass(data)
87     ests = []
88     cons = []
89     for i in xrange(m):
90         est, cons_new = estimate(None, dist, k, model=model)
91         model.remove(cons_new)
92         ests.append((est, cons_new))
93     return ests