Image Stitching for NVIDIA Jetson - User Guide - Homography estimation
The following page 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 Scrip's 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 to install 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:
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
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.
Script usage
The script has two 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.
Both 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 using the prediction capabilities of the models in the Adversarial Anomaly Detector. positional arguments: {left_fixed,right_fixed} left_fixed Estimation of homography between two images, with the left one fixed. right_fixed Estimation of homography between two images, with the right one fixed. 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] [--targetImage TARGETIMAGE] [--originalImage ORIGINALIMAGE] [--homographyScale HOMOGRAPHYSCALE] Estimation of homography between two images, with the left one fixed. optional arguments: -h, --help show this help message and exit --config CONFIG Path of configure file --targetImage TARGETIMAGE Path of the target image --originalImage ORIGINALIMAGE Path of the original image --homographyScale HOMOGRAPHYSCALE The scale factor of the homography. For example, if you go from 1920x1080 in the estimation to 640x360 in the processing the scale factor should be 1/3
Options for the right_fixed mode:
python homography_estimation.py right_fixed --help usage: homography_estimation.py right_fixed [-h] [--config CONFIG] [--targetImage TARGETIMAGE] [--originalImage ORIGINALIMAGE] [--homographyScale HOMOGRAPHYSCALE] 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 configure file --targetImage TARGETIMAGE Path of the target image --originalImage ORIGINALIMAGE Path of the original image --homographyScale HOMOGRAPHYSCALE Scale factor of the homography. For example if you go from 1920x1080 in the estimation to 640x360 in the processing the scale factor should be 1/3
Configuration file
The script makes use of a configuration file where is stored the values of different variables needed to set up the algorithm before perform the homography estimation. This configuration file has the following values:
- K: Array with the values corresponding to the camera matrix.
- d: 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: Filed 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, is used the following configuration file:
// config-rc.json { "cameraMatrix":[2.8472876737532920e+03, 0, 9.7983673800322515e+02, 0, 2.8608529052506838e+03, 5.0423299551699932e+02, 0, 0, 1], "distortionParameters":[-6.7260720359999060e-01, 2.5160831522455513e+00, 5.4007310542765141e-02, -1.1365265232659062e-02, -1.2760075297700798e+01], "reprojError":4.5, "matchRatio":0.75, "sigma":0.5, "overlap":15, "crop":0, "fov":70, "undistort":true }
The command to perform the estimation is:
python homography_estimation.py left_fixed --config /path/to/config-rc.json --targetImage /path/to/cam1.png --originalImage /path/to/cam2.png
The --targetImage options corresponds to the image that is fixed and the --originalImage corresponds to the image that will be transformed. The output will be something like this:
matches: 69 RC_HOMOGRAPHY="{\"h00\":0.16665120123596464,\"h01\":-0.08897097209511769, \"h02\":1808.7657933620071, \"h10\":-0.2887392749063616, \"h11\":0.9790321622250535, \"h12\":39.68771747852058, \"h20\":-0.00038491321338392687, \"h21\":-4.358286543507097e-06, \"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 options --homographyScale.