Homography, put simply, is "some math to warp an image."
In our case, we want to take an image from our camera and warp it onto the plane of the field so we can extract coordinates of objects in the real world. This allows us to take pixel coordinates on the image and convert them into real-world coordinates.
More info on how homography works here: OpenCV Docs
There are really only two main steps:
- Calculate the homography matrix.
- Apply the matrix in real-time to calculate locations of objects.
(Snapshot -> Reference Chessboard -> Resulting Transformed Snapshot)
To calibrate/calculate your homography matrix, you need to make sure the camera is mounted on your robot in a way that it will not move around or shift at all, and you need a way to capture images.
You also need a chessboard, ideally one that is solid (flat, no creases, rigid) and with tiles 1 inch × 1 inch. You can print out our reference_chessboard.png image on paper and maybe tape/glue it to cardboard, or 3D print our printed_chessboard.3mf file. We chose to 3D print ours.
If you cannot attain a chessboard with 1 inch × 1 inch tiles, you will just have to apply a scale to the calculated positions.
- Place the chessboard fully within the camera's frame, near the middle. Make sure the front edge of the chessboard is parallel to the front edge of your robot.
- Take a picture and download it.
- Download
homography_calibration.py,homography_test.py, andreference_chessboard.pngand put them in the same directory as the image from your camera. - Change the filename on line 19 of
homography_calibration.pyto match the name of the image from your camera. - Run the program (
python3 homography_calibration.py). If errors appear about missing packages, install them with pip (pip3 install [package-name]). - Press
con one of the image windows to have it automatically select all of the chessboard vertices. You can also manually do this by clicking a corresponding location on each image and pressingsfor each pair. - The point dots are colored. Make the locations of the colored dots match between the two images by pressing
rto rotate them andfto flip them. - Press
hto calculate the homography matrix. A transformed image will appear. - Copy the calculated matrix out of the terminal/shell output.
If you want to test the matrix, you can copy the Python syntax for the matrix into homography_test.py (line 5) and the image filename (line 12). Run that program, and test the matrix by clicking on a point on the chessboard on the snapshot. A dot at the corresponding location on the reference image should appear.
You need to have a pipeline from which you can extract result coordinates. This sample code uses results from a Limelight pipeline, but you can adapt it to fit others.
- Copy BasicHomographySample.java into your
teamcodefolder. - Replace
CAMERA_WIDTHandCAMERA_HEIGHTwith your camera's resolution, andHORIZONTAL_FOVandVERTICAL_FOVwith your camera's field of view. - Copy the Java syntax for the matrix into the BasicHomographySample.java on line 33. Change the active pipeline (line 63) if necessary.
- Upload and run it. Detected results should be printed in Telemetry.
- The printed coordinates of detected objects will likely be offset. We calibrate the offsets (
HORIZONTAL_OFFSETandVERTICAL_OFFSET) by placing a detected object at a known position, for instance, 24 inches in front of the center of the robot, equivalent to the coordinate (0, 24). Modify the offsets to make the printed coordinate match the expected coordinate.
FIRST Tech Challenge Team 6165 MSET Cuttlefish