-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineSegmentation.py
77 lines (59 loc) · 2.08 KB
/
lineSegmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# lineSegmentation Module
# Using RANSAC algorithm
# author Aaron Brown
# modified by W.Jin
####################################################################################
############# Return Line's equation & Picked Point & Outliers in R^2 ##############
####################################################################################
import numpy as np
import random
import math
def RansacLine(points, maxIterations, distanceTol):
if(len(points)<2):
return None
# Initialize unordered set inliersResult
inliersResult = set({})
outliersResult = set({})
#Line_a = Line_b = Line_c = 0
while maxIterations:
# Initialize unordered set inliers
inliers = set({})
outliers = set({})
# Pick 2 Random Samples
while len(inliers) < 2 :
inliers.add(random.randint(0,len(points)-1))
inliers_iter = inliers.__iter__()
itr = next(inliers_iter)
x1 = points[itr][0]
y1 = points[itr][1]
itr = next(inliers_iter)
x2 = points[itr][0]
y2 = points[itr][1]
# Get Line Equation : ax+by+c = 0
a = y1-y2
b = -x1+x2
c = (x1-x2)*y1-(y1-y2)*x1
for i in range(len(points)):
# Not consider three points already picked
if i in inliers:
continue
x3 = points[i][0]
y3 = points[i][1]
# Distance between picked point and the plane
dist = math.fabs(a*x3 + b*y3 + c) / math.sqrt(a*a + b*b)
if dist <= distanceTol:
inliers.add(i)
else:
outliers.add(i)
if len(inliers) > len(inliersResult):
inliersResult = inliers
outliersResult = outliers
#Line_a = a
#Line_b = b
#Line_c = c
maxIterations -= 1
if(len(outliersResult)==0):
return list(inliersResult), []
return list(inliersResult), list(outliersResult)
if __name__ == "__main__":
print("Error.. Why PlaneSegmentation execute")