|
| 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() |
0 commit comments