Image Stitching for NVIDIA Jetson/User Guide/Homography estimation: Difference between revisions

From RidgeRun Developer Wiki
m (Update configuration instructions)
Line 191: Line 191:
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:
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.  
* '''targetCameraMatrix''': Array with the values corresponding to the camera matrix of the target source.
* '''distortionParameters''': Array with the values corresponding to the distortion coefficients.
* '''targetDistortionParameters''': Array with the values corresponding to the distortion coefficients of the target source.
* '''referenceCameraMatrix''': Array with the values corresponding to the camera matrix of the reference source.
* '''referenceDistortionParameters''': Array with the values corresponding to the distortion coefficients of the reference source.
* '''reprojError''': Reprojection error for the homography calculation.
* '''reprojError''': Reprojection error for the homography calculation.
* '''matchRatio''': Max distance ratio for a possible match of keypoints.
* '''matchRatio''': Max distance ratio for a possible match of keypoints.
Line 199: Line 201:
* '''crop''': Degrees of the crop to apply in the sides of the image corresponding to the seam.
* '''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.
* '''fov''': Field of view in degrees of the cameras.
* '''undistort''': Bool value to enable the application or not of the distortion removal.
* '''targetUndistort''': Bool value to enable the distortion removal of the target source.
* '''referenceUndistort''': Bool value to enable the distortion removal of the reference source.  


=== Example ===
=== Example ===
Line 209: Line 212:


{
{
   "cameraMatrix":[1, 0, 0, 0, 1, 0, 0, 0, 1],
   "targetCameraMatrix":[1, 0, 0, 0, 1, 0, 0, 0, 1],
   "distortionParameters":[0, 0, 0, 0, 0, 0],
   "targetDistortionParameters":[0, 0, 0, 0, 0, 0],
  "referenceCameraMatrix":[1, 0, 0, 0, 1, 0, 0, 0, 1],
  "referenceDistortionParameters":[0, 0, 0, 0, 0, 0],
   "reprojError":5,
   "reprojError":5,
   "matchRatio":0.9,
   "matchRatio":0.9,
Line 217: Line 222:
   "crop":0,
   "crop":0,
   "fov":50,
   "fov":50,
   "undistort":false
   "targetUndistort":false,
  "referenceUndistort":false
   }
   }
 





Revision as of 18:14, 16 April 2021



Previous: User Guide/Controlling the Stitcher Index Next: User Guide/Homography list





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

Script estimation flow

The steps performed by the script are the following:

  1. Load the images.
  2. Remove the distortion of the images. (Optional)
  3. Perform a preprocessing to the images removing the noise using a Gaussian filter.
  4. Extract the keypoint and the corresponding descriptors with the SIFT algorithm.
  5. Find the correspondences among the key points of the two images.
  6. 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 two operation 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:

  • targetCameraMatrix: Array with the values corresponding to the camera matrix of the target source.
  • targetDistortionParameters: Array with the values corresponding to the distortion coefficients of the target source.
  • referenceCameraMatrix: Array with the values corresponding to the camera matrix of the reference source.
  • referenceDistortionParameters: Array with the values corresponding to the distortion coefficients of the reference source.
  • 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.
  • targetUndistort: Bool value to enable the distortion removal of the target source.
  • referenceUndistort: Bool value to enable the distortion removal of the reference source.

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

{
  "targetCameraMatrix":[1, 0, 0, 0, 1, 0, 0, 0, 1],
  "targetDistortionParameters":[0, 0, 0, 0, 0, 0],
  "referenceCameraMatrix":[1, 0, 0, 0, 1, 0, 0, 0, 1],
  "referenceDistortionParameters":[0, 0, 0, 0, 0, 0],
  "reprojError":5,
  "matchRatio":0.9,
  "sigma":1.5,
  "overlap":23,
  "crop":0,
  "fov":50,
  "targetUndistort":false,
  "referenceUndistort":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:

Keypoints matches
Result of the stitching with the estimated homography

To scale the homography to fit another dimension of the input images, use the --homography_scale option.


Previous: User Guide/Controlling the Stitcher Index Next: User Guide/Homography list