Project: Finding Vanishing Point

Before:

After:

import cv2
import numpy as np
import shapely
from  shapely.geometry import LineString, Point



img = cv2.imread('images/test1.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cannyEdges  = cv2.Canny(gray, 20, 150, apertureSize=3)

lines = cv2.HoughLines(cannyEdges, 1, np.pi/180, 125)


linePoints = []

for i in lines:
    for rho, theta in i:
        #skip near vertical lines
        if abs(theta-np.pi/90) < np.pi/9:
            continue
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a * rho
        y0 = b * rho
        x1 = int(x0 + 1000 * (-b))
        y1 = int(y0 + 1000 * (a))
        x2 = int(x0 - 1000 * (-b))
        y2 = int(y0 - 1000 * (a))
        cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
        linePoints.append(([x1, y1], [x2, y2]))


line1 = LineString([linePoints[0][0], linePoints[0][1]])
line2 = LineString([linePoints[1][0], linePoints[1][1]])
int_pt = line1.intersection(line2)
point_of_intersection = int_pt.x, int_pt.y
print(point_of_intersection)

cv2.circle(img, (int(int_pt.x), int(int_pt.y)), 10, (255, 0, 255), 2)


cv2.imshow('original', img)
cv2.waitKey(0)

Last updated

Was this helpful?