Skip to content

Commit 49e36e9

Browse files
committed
Line to line segment conversion
1 parent f985ba2 commit 49e36e9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pclines/utils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
3+
4+
def line_segments_from_homogeneous(lines, bbox):
5+
x,y,w,h = bbox
6+
7+
# Corner points
8+
A = np.array([x,y,1])
9+
B = np.array([x+w,y,1])
10+
C = np.array([x+w,y+h,1])
11+
D = np.array([x,y+h,1])
12+
13+
# Cross product of pairs of corner points
14+
edges = [
15+
np.cross(a,b) for a,b in [[A,B],[B,C],[C,D],[D,A]]
16+
]
17+
18+
# Cross product of line params with edges
19+
intersections = [
20+
np.cross(lines, e) for e in edges
21+
]
22+
23+
# Normalize
24+
normalized = [
25+
p[:,:2] / p[:,-1].reshape(-1,1) for p in intersections
26+
]
27+
28+
X = []
29+
Y = []
30+
31+
for p in zip(*normalized):
32+
P = []
33+
for (u,v) in p:
34+
if (x <= u <= x+w) and (y <= v <= y+h):
35+
P.append( (u,v) )
36+
if len(P) == 2:
37+
(x0,y0), (x1,y1) = P
38+
X.append( (x0,x1) )
39+
Y.append( (y0,y1) )
40+
else:
41+
X.append(None)
42+
Y.append(None)
43+
44+
return X, Y

0 commit comments

Comments
 (0)