Created by [ Rowan Dempster] on Dec 28, 2019
There are a number of perspective transforms, that must be done for an image to allow it to be processed by Perception, and for extracted object detections to be published with "correct" coordinates (see the Integration page for documentation of coordinate systems). In particular road line detection algorithms rely on the bird's eye view perspective transform of camera images.
This project is best thought of as broken down into the following components.
This process is generalized to any camera. and should only be pursued once the intrinsic calibration is working for that particular camera.
Important note: A BEV transformation matrix is only applicable to one specific camera configuration. If the camera changes position, or is rotated, the above mentioned processed must be performed again.
[{.confluence-embedded-image
.confluence-external-resource}]
\
src = np.float32([[577, 226], [1296, 226], [1661, 719], [303, 719]])
dst = np.float32([[577, 360], [1296, 360], [1296, 950], [577, 950]])
M = cv2.getPerspectiveTransform(src, dst)
bev = cv2.warpPerspective(raw_image, M, img_size, flags=cv2.INTER_LINEAR)
Below is the result image for this specific BEV transform. Note that the four circled corners now appear to be a rectangle instead of a trapezoid:
[{.confluence-embedded-image
.confluence-external-resource}]
Once the matrix is generated, it is reusable as long as the camera is not moved.
It's helpful to see the rectangle from above from our transformed image. However, knowing the image space coordinates of the rectangle's corners isn't enough. We need to know the corresponding real space coordinates. Where is it relative to the camera or vehicle. Therefore we require another coordinate transformation.
We still need the position of the rectangle relative to the camera, or vehicle. Therefore, we need to compute some offset that maps a pixel in the BEV image to a real space point (with respect to some coordinate system).
import numpy as np
import cv2
import matplotlib.pyplot as plt
# Find out the size of the original image
raw_image = cv2.imread('BEV_Image/bev_pic.jpg')
img_size = (raw_image.shape[1], raw_image.shape[0])
# Define the source and destination points
src = np.float32([[577, 226], [1296, 226], [1661, 719], [303, 719]])
dst = np.float32([[577, 360], [1296, 360], [1296, 950], [577, 950]])
M = cv2.getPerspectiveTransform(src, dst)
# Apply bev to the original image
bev = cv2.warpPerspective(raw_image, M, img_size, flags=cv2.INTER_LINEAR)
cv2.imwrite("BEV_Image/Transformed.jpg", bev) # Saving images
plt.imshow(bev) # Displaying the image
https://nikolasent.github.io/opencv/2017/05/07/Bird%27s-Eye-View-Transformation.html
Document generated by Confluence on Dec 10, 2021 04:01