Image Stitching for NVIDIA Jetson - User Guide - Homography estimation
Image Stitching for NVIDIA®Jetson™ |
---|
Before Starting |
Image Stitching Basics |
Overview |
Getting Started |
User Guide |
Resources |
Examples |
Spherical Video |
Performance |
Contact Us |
Homography
A homography in the context of image stitching describes a relation between two images, that relation transforms a target image based on a reference image. This provides a way to specify the arrangement and adjustments between images for stitching.
These adjustments and transformations are represented in a homography matrix that synthesizes the mapping between two image planes. For a two dimension homography, the matrix looks like the following, where x and y are the original coordinates while x' and y' are the new ones.
Homography decomposition
This mapping mentioned above can be decomposed into translation, rotation, shear and scale. each one represented by its own transformation matrix.
From that original homography matrix, the translation can be factored out like so:
Where and are the x and y axis translations respectively.
The reduced resulting matrix
can then be decomposed into three more matrices representing rotation, shear and scale; however that process is not as intuitive as the previous one; therefore it is not recommended to manually define the homography matrix unless the transformation is only a translation.
Even with simple homographies it is highly recommended to use the included homography estimation tool, since it is easier and yields better results.
Homography estimation tool
The following section will introduce a way to estimate an initial homography matrix that can be used in the cudastitcher element. This method consists of a Python script that estimates the homography between two images. Find the script in the scripts
directory of the rrstitcher project.
Dependencies
- Python 3.6
- OpenCV 4.0 or later.
- Numpy
Installing the dependencies using a virtual environment
The above dependencies can be installed making use of a Python virtual environment. A virtual environment is useful for installing Python packages without damaging some other environment in your machine. To create a new virtual environment, run the following command:
python3.6 -m venv ENV_NAME
A new folder will be created with the name ENV_NAME. To activate the virtual environment, run the following command:
source ${ENV_NAME}/bin/activate
Source the virtual environment each time you want to use it. To install the packages in the virtual environment:
pip install numpy pip install opencv-contrib-python pip install scikit-image
Script estimation flow
The steps performed by the script are the following:
- Load the images.
- Remove the distortion of the images. (Optional)
- Perform a preprocessing to the images removing the noise using a Gaussian filter.
- Extract the keypoint and the corresponding descriptors with the SIFT algorithm.
- Find the correspondences among the key points of the two images.
- With the resulting keypoints, estimate the homography.
Lens distortion correction
In case there is need to apply distortion correction on the camera, follow the camera calibration process found in this wiki to obtain the camera matrix and distortion parameters.
Script usage
The script has six modes:
- left_fixed: where the left image is fixed and the right image will be transformed by the homography.
- right_fixed: where the right image is fixed and the left image will be transformed by the homography.
All modes can be adjusted to the sigma value of the Gaussian filter and the width size of the overlap between the two images. The following are the complete options of the script:
python homography_estimation.py --help usage: ./homography_estimation.py [-h] {left_fixed,right_fixed} ... Tool for estimating the homography between two images. positional arguments: {left_fixed,right_fixed} left_fixed Estimation of homography between two images, with the left one as reference. right_fixed Estimation of homography between two images, with the right one as reference. optional arguments: -h, --help show this help message and exit Type "./homography_estimation.py <command> -h" for more information.
Options for the left_fixed mode:
python homography_estimation.py left_fixed --help usage: ./homography_estimation.py left_fixed [-h] [--config CONFIG] [--reference_image REFERENCE_IMAGE] [--target_image TARGET_IMAGE] [--scale_width SCALE_WIDTH] [--scale_height SCALE_HEIGHT] Estimation of homography between two images, with the left one as reference. optional arguments: -h, --help show this help message and exit --config CONFIG Path of the configuration file --reference_image REFERENCE_IMAGE Path of the reference image --target_image TARGET_IMAGE Path of the target image --scale_width SCALE_WIDTH Scale width dimension of the input images --scale_height SCALE_HEIGHT Scale height dimension of the input images
Options for the right_fixed mode:
python homography_estimation.py right_fixed --help usage: homography_estimation.py right_fixed [-h] [--config CONFIG] [--reference_image REFERENCE_IMAGE] [--target_image TARGET_IMAGE] [--scale_width SCALE_WIDTH] [--scale_height SCALE_HEIGHT] Estimation of homography between two images, with the right one fixed. optional arguments: -h, --help show this help message and exit --config CONFIG Path of the configuration file --reference_image REFERENCE_IMAGE Path of the reference image --target_image TARGET_IMAGE Path of the target image --scale_width SCALE_WIDTH Scale width dimension of the input images --scale_height SCALE_HEIGHT Scale height dimension of the input images
Configuration file
The script uses a configuration file to access the values of the different variables needed to set up the algorithm before performing the homography estimation. The configuration file contains the following parameters:
- cameraMatrix: Array with the values corresponding to the camera matrix.
- distortionParameters: Array with the values corresponding to the distortion coefficients.
- reprojError: Reprojection error for the homography calculation.
- matchRatio: Max distance ratio for a possible match of keypoints.
- sigma: Sigma value for the Gaussian filter.
- overlap: Degrees of overlap between the two images.
- crop: Degrees of the crop to apply in the sides of the image corresponding to the seam.
- fov: Field of view in degrees of the cameras.
- undistort: Bool value to enable the application or not of the distortion removal.
Example
The following example will estimate the homography of two images, with the left one fixed. In this case, the following configuration file is used:
// config.json { "cameraMatrix":[1, 0, 0, 0, 1, 0, 0, 0, 1], "distortionParameters":[0, 0, 0, 0, 0, 0], "reprojError":5, "matchRatio":0.9, "sigma":1.5, "overlap":23, "crop":0, "fov":50, "undistort":false }
The command to perform the estimation is:
python homography_estimation.py left_fixed --config /path/to/config.json --reference_image /path/to/cam1.png --target_image /path/to/cam2.png
The --reference_image option corresponds to the image that is fixed (left in this case) and the --target_image corresponds to the image that will be transformed. The output will be something like this:
matches: 69 "matrix":{"h00":1.1762953900821504,"h01":0.18783005980813444, "h02":1382.1180307642137, "h10":0.019908986235761796, "h11":1.0951514369488284, "h12":0.6427298229101379, "h20":6.676211791348554e-05, "h21":8.14441129307644e-05, "h22":1.0}
Also, the script will generate some images to evaluate the quality of the homography:
To scale the homography to fit another dimension of the input images, use the --homography_scale option.