Skip to content

Commit 4b8a094

Browse files
authored
Vanishing point detection for 2 & 3 parallel lines
1 parent c474187 commit 4b8a094

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

VanishingPoint_detection/img1.jpg

3.58 MB
Loading
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
import cv2
3+
import math
4+
import random
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
from PIL import Image
8+
from scipy.ndimage import filters
9+
10+
11+
def slopIntercept(x, y):
12+
slope = (y[1] - y[0])/(x[1] - x[0])
13+
intercept = y[1] - slope*(x[1])
14+
return [slope,intercept]
15+
16+
def det(a, b):
17+
return a[0] * b[1] - a[1] * b[0]
18+
19+
def line_intersection(line1, line2):
20+
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
21+
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
22+
23+
div = det(xdiff, ydiff)
24+
if div == 0:
25+
return False
26+
27+
d = (det(*line1), det(*line2))
28+
x = det(d, xdiff) / div
29+
y = det(d, ydiff) / div
30+
return x, y
31+
32+
33+
def vPoint(img):
34+
n1 = input('How many line want to select: ')
35+
n = 2*n1
36+
lines = []
37+
plt.imshow(img)
38+
p = plt.ginput(n)
39+
40+
for i in range(0, n, 4):
41+
plt.plot([p[i][0], p[i+1][0]], [p[i][1], p[i+1][1]], marker = 'o')
42+
plt.plot([p[i+2][0], p[i+3][0]], [p[i+2][1], p[i+3][1]], marker = 'o')
43+
44+
for j in range(0, n, 6):
45+
for i in range(j, j+6, 2):
46+
x = [p[i][0], p[i+1][0]]
47+
y = [p[i][1], p[i+1][1]]
48+
lines.append(slopIntercept(x, y))
49+
50+
list_vp = []
51+
for j in range(0, n1, 3):
52+
for i in range(j, j+3, 3):
53+
ang1 = math.degrees(math.atan((lines[0][0] - lines[1][0])/(1+ lines[1][0]*lines[0][0])))
54+
ang2 = math.degrees(math.atan((lines[1][0] - lines[2][0])/(1+ lines[2][0]*lines[1][0])))
55+
if ang1 > 90:
56+
ang1 = 180 - ang1
57+
if ang2 > 90:
58+
ang2 = 180 - ang2
59+
if ang1 < ang2:
60+
x = (lines[i][1] - lines[i+1][1])/(lines[i+1][0] - lines[i][0])
61+
y = lines[i+1][0]*x + lines[i+1][1]
62+
vp = (x, y)
63+
#vp = line_intersection((p[i], p[i+1]), (p[i+2], p[i+3]))
64+
list_vp.append(vp)
65+
else:
66+
x = (lines[i+2][1] - lines[i+1][1])/(lines[i+1][0] - lines[i+2][0])
67+
y = lines[i+1][0]*x + lines[i+1][1]
68+
vp = (x, y)
69+
#vp = line_intersection((p[i+2], p[i+3]), (p[i+4], p[i+5]))
70+
list_vp.append(vp)
71+
72+
73+
plt.plot([list_vp[0][0], list_vp[1][0]], [list_vp[0][1], list_vp[1][1]], marker = 'o')
74+
print('The VPs coorinate are: ', list_vp)
75+
x1, y1 = list_vp[0]
76+
x2, y2 = list_vp[1]
77+
a, b = (y1 - y2), (x1 - x2)
78+
slope = a/b
79+
intercept = y1 - slope*(x1)
80+
print("The equataion is:", a,"x - ",b,"y + ",intercept*b," = 0")
81+
plt.show()
82+
83+
def main():
84+
filename = 'img1.jpg'
85+
im = Image.open(filename, 'r')
86+
img = np.float32(im)
87+
88+
vPoint(im)
89+
90+
91+
if __name__== "__main__":
92+
main()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from scipy.ndimage import filters
2+
import cv2
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
import random
6+
from PIL import Image
7+
8+
def det(a, b):
9+
return a[0] * b[1] - a[1] * b[0]
10+
11+
def line_intersection(line1, line2):
12+
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
13+
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
14+
15+
div = det(xdiff, ydiff)
16+
if div == 0:
17+
return False
18+
19+
d = (det(*line1), det(*line2))
20+
x = det(d, xdiff) / div
21+
y = det(d, ydiff) / div
22+
return x, y
23+
24+
25+
def vPoint(img):
26+
n1 = input('How many line want to select: ')
27+
n = 2*n1
28+
list_vp = []
29+
plt.imshow(img)
30+
p = plt.ginput(n)
31+
for i in range(0, n, 4):
32+
plt.plot([p[i][0], p[i+1][0]], [p[i][1], p[i+1][1]], marker = 'o')
33+
plt.plot([p[i+2][0], p[i+3][0]], [p[i+2][1], p[i+3][1]], marker = 'o')
34+
vp = line_intersection((p[i], p[i+1]), (p[i+2], p[i+3]))
35+
print(vp)
36+
list_vp.append(vp)
37+
#plt.plot(vp[0], vp[1], 'ro', ls='')
38+
plt.plot([list_vp[0][0], list_vp[1][0]], [list_vp[0][1], list_vp[1][1]], marker = 'o')
39+
print('The VPs coorinate are: ', list_vp)
40+
x1, y1 = list_vp[0]
41+
x2, y2 = list_vp[1]
42+
a, b = (y1 - y2), (x1 - x2)
43+
slope = a/b
44+
intercept = y1 - slope*(x1)
45+
print("The equataion is:", a,"x - ",b,"y + ",intercept*b," = 0")
46+
plt.show()
47+
48+
def main():
49+
filename = 'img1.jpg'
50+
im = Image.open(filename, 'r')
51+
img = np.float32(im)
52+
53+
R = vPoint(im)
54+
55+
56+
57+
if __name__== "__main__":
58+
main()
336 KB
Loading
278 KB
Loading

0 commit comments

Comments
 (0)