I'm tryng to implement Inverse Perspective Mapping to calculate the distance to another vehicle on a road. I know that I need to generate a transformation matrix with source points and destination points before applying the function warpPerspective()
, but I don't know how to calculate destination points.
I searched in this forum and other sites but I can't transform the first image to the second image:
(source: shanetuohy.com)
(source: shanetuohy.com)
The goal that you want to achieve requires you to compute the honography between the ground plane, its position in the image as viewed by the camera, and its position in the eagle view.
This is traditionnaly handled by extracting features (corners or keypoints) in most Computer Vision pipelines, but this is impractical here: theere are no feature (or changing features) on the road.
What you can do instead is computing these homographies in a calibration step, with the following procedure:
Put your camera on the target vehicle. Put a known rectangular pattern (usually a chessboard) lying on flat ground in front of the car. If you want metric distances, then you also need to know the size of the pattern, otherwise you will measure relative distances only.
Take a picture of the calibration pattern with this setup.
In the image of the pattern, you need to detect it. This can be done in several ways: if you have a reference image of the pattern, you can detect it automatically, or you can manually click onthe 4 outer corners. This will give you at least four point correspondences (1 for each outer corner of the pattern). The point correspondences are established between image points (e.g., the 4 corners) and a reference position, which is either the position of the said point in the referenc eimage of the pattern (if oyu have one) or the position that you want for this point in the eagle view.
From the correspondences, estimate a homography
H
.H
is a 3x3 matrix (see any Computer Vision reference, either online or textbook).This will output you the transform between what the camera is seeing and the reference position of the calibration pattern. If you took as reference a fronto-parallel view of the pattern or its desired coordinates in the eagle view, you have the homography that you are looking for. If not, you need to estimate a second homography and chain their results.
Note that this application is considered in Gary Bradski's Learning OpenCV book. You can read more about it in the book, and probbaly find the original code online.