What is a vanishing point?
A vanishing point is a point on the horizon line to which parallel lines appear to converge in a two-dimensional perspective drawing or an image.
The vanishing points mimic the effect of distance and depth we perceive in the real world. For example, in the following image, we can simulate a three-dimensional scenery in our minds, although the picture is two-dimensional.
Horizon line
The horizon line is a horizontal line representing the viewer's eye level. It is located at the viewer's eye height when looking straight ahead. In a landscape painting, the horizon line usually separates the sky from the ground. In the picture above, a blue line represents the horizon line.
Example code
The following code finds the vanishing point by calculating the intersection point of the two converging lines.
Note: The provided code allows us to mark four points on an image. The first two points will create one line, and the last two will form another. After marking the points, you're supposed to press the "Enter" button.
import cv2
import numpy as np
lines_coordinates = []
# Stores the coordinate values of the points user marks on the image
def reading_points(event, x, y, flags, params):
global lines_coordinates
if event == cv2.EVENT_FLAG_LBUTTON:
lines_coordinates.append((x,y))
# Asks the user to mark points on the image
def point_reader(img_path):
org_image = cv2.imread(img_path)
image = cv2.resize(org_image, (800, 800)) #resizing the image to make it confided within the screen
cv2.imshow('img', image)
cv2.setMouseCallback('img', reading_points)
cv2.waitKey(0)
# Calculates the vanishing point based on the marked points
def vanishing_point(img_path):
global lines_coordinates
if len(lines_coordinates) != 4:
print("Four points are needed to calculate the vanishing point.")
return
x1, y1 = lines_coordinates[0]
x2, y2 = lines_coordinates[1]
x3, y3 = lines_coordinates[2]
x4, y4 = lines_coordinates[3]
# Calculate the intersection point coordinates
intersection_x = ((x1*y2 - y1*x2) * (x3 - x4) - (x1 - x2) * (x3*y4 - y3*x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
intersection_y = ((x1*y2 - y1*x2) * (y3 - y4) - (y1 - y2) * (x3*y4 - y3*x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
intersection_point = (int(intersection_x), int(intersection_y))
org_image = cv2.imread(img_path)
image = cv2.resize(org_image, (800, 800))
cv2.circle(image, intersection_point, radius=5, color=(255, 0, 255), thickness=-1)
cv2.imshow('vanishing_pointed_image', image)
cv2.waitKey(0)
def main():
path = r"image.jpg"
point_reader(path)
vanishing_point(path)
main()Code explanation
Lines 1–2: Importing the necessary library.
Line 4: Declaring a global list to store the coordinates of the points we will mark once the image gets displayed.
Lines 44–47: The main function is reading the image and calling the necessary functions
point_reader()andvanishing_point().Line 13: The
point_reader()function takes the image as an argument.Lines 14–16: The
imread()function reads the image and stores it inorg_image. Theresize()method scales the image to a desired window size. Then the image is displayed using theimshow()method so that the points can be marked on it.Line 17: The
setMouseCallback()function calls another functionreading_points.Lines 7–10: The
reading_points()function stores the marked points inlines_coordinateslist.
Note: We kept the
list_coordinatesglobal so that it could be accessed by all the defined functions.
Line 21: Now we have the necessary points on the converging lines.
vanishing_point()function creates lines out of those points and calculates their point of intersection.Line 22–25: Condition ensures that at least four points have been marked.
Line 27–30: These lines extract the points from the
lines_coordinateslist.Lines 33–34: Calculate the intersection point of the lines that are converging to a vanishing point.
Line 36: Stores the intersection point at a tuple in the
intersection_pointvariable.Line 38–42: Displaying the image with the vanishing point in it.
Conclusion
The vanishing point helps a lot in determining the point perspective and depth of an image. Artists, architects, VR engineers, and designers utilize the vanishing point to create realistic architectural renderings that accurately depict depth and three-dimensional space.
Note: Read about perspective projection.
Free Resources