Rectification

This module provides functions to project an image onto a real-world coordinate system using ground control points. The module is largely based on the OpenCV Camera Calibration and 3D Reconstruction workflow and works nicely together with the argus2 toolbox for coastal image analysis.

A typical workflow consists of determining ground control points by measuring the real-world coordinates of object visible in the image and the image coordinates of these very same objects. Also the camera matrix and lens distortion parameters should be determined.

Subsequently, a homography can be determined using the flamingo.rectification.rectification.find_homography() function and a projection of the image can be plotted using the accompanying flamingo.rectification.plot module.

Rectification

flamingo.rectification.rectification.find_homography(UV, XYZ, K, distortion=array([[ 0., 0., 0., 0.]]), z=0)[source]

Find homography based on ground control points

Parameters:
  • UV (np.ndarray) – Nx2 array of image coordinates of gcp’s
  • XYZ (np.ndarray) – Nx3 array of real-world coordinates of gcp’s
  • K (np.ndarray) – 3x3 array containing camera matrix
  • distortion (np.ndarray, optional) – 1xP array with distortion coefficients with P = 4, 5 or 8
  • z (float, optional) – Real-world elevation on which the image should be projected
Returns:

3x3 homography matrix

Return type:

np.ndarray

Notes

Function uses the OpenCV image rectification workflow as described in http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html starting with solvePnP.

Examples

>>> camera_id = 4
>>> r = argus2.rest.get_rectification_data('kijkduin')
>>> H = flamingo.rectification.find_homography(r[camera_id]['UV'],
                                               r[camera_id]['XYZ'],
                                               r[camera_id]['K'])
flamingo.rectification.rectification.get_pixel_coordinates(img)[source]

Get pixel coordinates given an image

Parameters:img (np.ndarray) – NxMx1 or NxMx3 image matrix
Returns:
  • np.ndarray – NxM matrix containing u-coordinates
  • np.ndarray – NxM matrix containing v-coordinates
flamingo.rectification.rectification.rectify_coordinates(U, V, H)[source]
Get projection of image pixels in real-world coordinates
given image coordinate matrices and homography
Parameters:
  • U (np.ndarray) – NxM matrix containing u-coordinates
  • V (np.ndarray) – NxM matrix containing v-coordinates
  • H (np.ndarray) – 3x3 homography matrix
Returns:

  • np.ndarray – NxM matrix containing real-world x-coordinates
  • np.ndarray – NxM matrix containing real-world y-coordinates

flamingo.rectification.rectification.rectify_image(img, H)[source]
Get projection of image pixels in real-world coordinates
given an image and homography
Parameters:
  • img (np.ndarray) – NxMx1 or NxMx3 image matrix
  • H (np.ndarray) – 3x3 homography matrix
Returns:

  • np.ndarray – NxM matrix containing real-world x-coordinates
  • np.ndarray – NxM matrix containing real-world y-coordinates

Visualization

flamingo.rectification.plot.find_horizon_offset(x, y, max_distance=10000.0)[source]

Find minimum number of pixels to crop to guarantee all pixels are within specified distance

Parameters:
  • x (np.ndarray) – NxM matrix containing real-world x-coordinates
  • y (np.ndarray) – NxM matrix containing real-world y-coordinates
  • max_distance (float, optional) – Maximum distance from origin to be included in the plot. Larger numbers are considered to be beyond the horizon.
Returns:

Minimum crop distance in pixels (from the top of the image)

Return type:

float

flamingo.rectification.plot.plot_coverage(X, Y, rotation=None, translation=None, max_distance=10000.0, ax=None, figsize=(30, 20), cmap=<matplotlib.colors.LinearSegmentedColormap object at 0x107b7de50>, alpha=0.4)[source]

Plot the coverage of the projection of multiple images in a single axis.

Plot the outline of lists of real-world x and y coordinate matrices. The resulting composition can be rotated and translated seperately.

Points projected at infinite distance can be ignored by specifying a maximum distance.

Parameters:
  • X (list of np.ndarrays) – List of NxM matrix containing real-world x-coordinates
  • Y (list of np.ndarrays) – List of NxM matrix containing real-world y-coordinates
  • rotation (float, optional) – Rotation angle in degrees
  • translation (list or tuple, optional) – 2-tuple or list with x and y translation distances
  • max_distance (float, optional) – Maximum distance from origin to be included in the plot. Larger numbers are considered to be beyond the horizon.
  • ax (matplotlib.axes.AxesSubplot, optional) – Axis object used for plotting
  • figsize (tuple, optional) – 2-tuple or list containing figure dimensions
  • cmap (matplotlib.colors.Colormap, optional) – Colormap to determine colors for individual patches
  • alpha (float, optional) – Alpha value for patches
Returns:

  • matplotlib.figure.Figure – Figure object containing axis object
  • matplotlib.axes.AxesSubplot – Axis object containing plot

flamingo.rectification.plot.plot_rectified(X, Y, imgs, rotation=None, translation=None, max_distance=10000.0, ax=None, figsize=(30, 20), cmap='Greys', color=True, n_alpha=0)[source]

Plot the projection of multiple RGB images in a single axis.

Plot a list of images using corresponding lists of real-world x and y coordinate matrices. The resulting composition can be rotated and translated seperately.

Points projected at infinite distance can be ignored by specifying a maximum distance.

Parameters:
  • X (list of np.ndarrays) – List of NxM matrix containing real-world x-coordinates
  • Y (list of np.ndarrays) – List of NxM matrix containing real-world y-coordinates
  • imgs (list of np.ndarrays) – List of NxMx1 or NxMx3 image matrices
  • rotation (float, optional) – Rotation angle in degrees
  • translation (list or tuple, optional) – 2-tuple or list with x and y translation distances
  • max_distance (float, optional) – Maximum distance from origin to be included in the plot. Larger numbers are considered to be beyond the horizon.
  • ax (matplotlib.axes.AxesSubplot, optional) – Axis object used for plotting
  • figsize (tuple, optional) – 2-tuple or list containing figure dimensions
  • color (bool, optional) – Whether color image should be plotted or grayscale
  • n_alpha (int) – Number of border pixels to use to increase alpha
Returns:

  • matplotlib.figure.Figure – Figure object containing axis object
  • matplotlib.axes.AxesSubplot – Axis object containing plot

flamingo.rectification.plot.rotate_translate(x, y, rotation=None, translation=None)[source]

Rotate and/or translate coordinate system

Parameters:
  • x (np.ndarray) – NxM matrix containing x-coordinates
  • y (np.ndarray) – NxM matrix containing y-coordinates
  • rotation (float, optional) – Rotation angle in degrees
  • translation (list or tuple, optional) – 2-tuple or list with x and y translation distances
Returns:

  • np.ndarrays – NxM matrix containing rotated/translated x-coordinates
  • np.ndarrays – NxM matrix containing rotated/translated y-coordinates

Table Of Contents

Previous topic

Welcome to Flamingo’s documentation!

Next topic

Segmentation

This Page