kornia.geometry.homography#
Functions to estimate homographies from point or line correspondences and to score them.
- kornia.geometry.homography.find_homography_dlt(points1, points2, weights=None, solver='lu')[source]#
Compute the homography matrix using the DLT formulation.
The weighted DLT system of four or more correspondences is solved with
solver.- Convention:
Hmapspoints1topoints2,points2 ~ H @ points1, and is scaled so thatH[2, 2] = 1bynormalize_transformation(), which leaves it at its unnormalised scale when|H[2, 2]|is at most1e-8; Two-view geometry compares this with OpenCV.weightsmultiply each correspondence’s squared algebraic residual: a weight of 0 removes the correspondence from the equations, and only relative weights matter.solver="lu"and"svd"give the same homography on exact data, to roundoff scaled by the conditioning of the system; on noisy data they solve different least-squares problems and differ.
- Parameters:
points1 (
Tensor) – A set of points in the first image with a tensor shape \((B, N, 2)\).points2 (
Tensor) – A set of points in the second image with a tensor shape \((B, N, 2)\).weights (
Optional[Tensor], optional) – Tensor containing the weights per point correspondence with a shape of \((B, N)\). Zero-weight points are excluded from the DLT equations and Hartley normalization. Default:Nonesolver (
str, optional) – variants: svd, lu. Default:"lu"
- Return type:
- Returns:
the computed homography matrix with shape \((B, 3, 3)\).
- kornia.geometry.homography.find_homography_dlt_iterated(points1, points2, weights, soft_inl_th=3.0, n_iter=5)[source]#
Compute the homography matrix using the iteratively-reweighted least squares (IRWLS).
- Convention:
Direction and
H[2, 2] = 1asfind_homography_dlt(). Each solve after the first re-weights with the Gaussian kernelexp(-e**2 / (2 * soft_inl_th**2))of the symmetric transfer errore(the root of the summed squared forward and backward transfer errors), sosoft_inl_this a standard deviation in pixels: a correspondence withe = soft_inl_thkeeps weightexp(-1/2).
- Parameters:
points1 (
Tensor) – A set of points in the first image with a tensor shape \((B, N, 2)\).points2 (
Tensor) – A set of points in the second image with a tensor shape \((B, N, 2)\).weights (
Tensor) – Tensor containing the weights per point correspondence with a shape of \((B, N)\). Used for the first iteration of the IRWLS.soft_inl_th (
float, optional) – standard deviation, in pixels, of the Gaussian re-weighting kernel given above. Default:3.0n_iter (
int, optional) – number of solves, including the initial one. Default:5
- Return type:
- Returns:
the computed homography matrix with shape \((B, 3, 3)\).
- kornia.geometry.homography.find_homography_lines_dlt(ls1, ls2, weights=None)[source]#
Compute the homography matrix from line segment correspondences with a DLT formulation.
See [JJC01] for details.
- Convention:
Hmaps image-1 points to image-2 points, as infind_homography_dlt(). Each segment is a[start, end]pair of(x, y)points, andweightshas one entry per segment.Both endpoints of image-1 segment
iare constrained to lie, after mapping byH, on the line through image-2 segmenti, so the endpoints need not be point correspondences.
- Parameters:
ls1 (
Tensor) – A set of line segments in the first image with a tensor shape \((B, N, 2, 2)\), or \((N, 2, 2)\), which is treated as \(B = 1\).ls2 (
Tensor) – A set of line segments in the second image with a tensor shape \((B, N, 2, 2)\), or \((N, 2, 2)\), which is treated as \(B = 1\).weights (
Optional[Tensor], optional) – Tensor containing the weights per segment with a shape of \((B, N)\). Zero-weight segments are excluded from Hartley normalization. Default:None
- Return type:
- Returns:
the computed homography matrix with shape \((B, 3, 3)\).
- kornia.geometry.homography.find_homography_lines_dlt_iterated(ls1, ls2, weights, soft_inl_th=4.0, n_iter=5)[source]#
Compute the homography matrix using the iteratively-reweighted least squares (IRWLS) from line segments.
- Convention:
As
find_homography_dlt_iterated(), withfind_homography_lines_dlt()as the solver and, asein the Gaussian kernel, the perpendicular distance in pixels of the mapped image-1 endpoints from the image-2 line. A zero-length image-2 segment gets weight zero.
- Parameters:
ls1 (
Tensor) – A set of line segments in the first image with a tensor shape \((B, N, 2, 2)\).ls2 (
Tensor) – A set of line segments in the second image with a tensor shape \((B, N, 2, 2)\).weights (
Tensor) – Tensor containing the weights per segment with a shape of \((B, N)\). Used for the first iteration of the IRWLS.soft_inl_th (
float, optional) – standard deviation, in pixels, of the Gaussian re-weighting kernel offind_homography_dlt_iterated(). Default:4.0n_iter (
int, optional) – number of solves, including the initial one. Default:5
- Return type:
- Returns:
the computed homography matrix with shape \((B, 3, 3)\).
- kornia.geometry.homography.line_segment_transfer_error_one_way(ls1, ls2, H, squared=False)[source]#
Return the mean perpendicular transfer distance in image 2 for line segment correspondences.
Both endpoints of each image-1 segment are mapped into image 2 by
Hand scored against the line through the matching image-2 segment. See [JJC01] for details.- Convention:
Argument order and direction as
oneway_transfer_error().The image-2 line is scaled to unit \((a, b)\), so the error is the mean perpendicular distance of the two mapped endpoints, in pixels.
- Parameters:
ls1 (
Tensor) – line segment correspondences from the left images with shape (B, N, 2, 2).ls2 (
Tensor) – line segment correspondences from the right images with shape (B, N, 2, 2).H (
Tensor) – Homographies with shape \((B, 3, 3)\).squared (
bool, optional) – if True (default is False), the squared error is returned. Default:False
- Return type:
- Returns:
the computed error with shape \((B, N)\).
- kornia.geometry.homography.oneway_transfer_error(pts1, pts2, H, squared=True, eps=1e-8)[source]#
Return transfer error in image 2 for correspondences given the homography matrix.
- Convention:
oneway_transfer_error(pts1, pts2, H)measures in image 2, betweenHapplied topts1andpts2.One set of correspondences scored against several homographies (points with leading dimensions of size 1) is computed from one matrix product for all of them, in at least float32; the result matches the per-homography computation to roundoff.
squared=True, the default here and insymmetric_transfer_error(), returns the squared distance;line_segment_transfer_error_one_way()defaults tosquared=False.The error does not depend on the scale of
H, and an exact match scores exactly 0, with a zero gradient, withsquared=False. A point thatHmaps to infinity (\(w' = 0\)) has errorinf.
- Parameters:
pts1 (
Tensor) – correspondences from the left images with shape (B, N, 2 or 3). If they are homogeneous, converted automatically.pts2 (
Tensor) – correspondences from the right images with shape (B, N, 2 or 3). If they are homogeneous, converted automatically.H (
Tensor) – Homographies with shape \((B, 3, 3)\).squared (
bool, optional) – if True (default), the squared distance is returned. Default:Trueeps (
float, optional) – unused; it was added to the projective denominator and inside the square root, which made the error depend on the scale ofHand an exact match scoresqrt(eps)(#4881). The singular case is guarded exactly instead. Accepted so that existing calls keep working. Default:1e-8
- Return type:
- Returns:
the computed distance with shape \((B, N)\).
- kornia.geometry.homography.sample_is_valid_for_homography(points1, points2)[source]#
Implement oriented constraint check from [MarquezNLopezABB16].
Analogous to opencv/opencv
- Convention:
RANSACuses it to discard minimal samples for"homography". It checks only that the four triples of the first four points keep their orientation across the two views, so a mirror-image sample is rejected. A triple that is collinear in both views, for instance through a repeated point, counts as kept, so collinearity alone does not reject a sample.
- Parameters:
- Return type:
- Returns:
Mask with the minimal sample is good for homography estimation \((B)\).
- kornia.geometry.homography.sampson_homography_distance(pts1, pts2, H, squared=True)[source]#
Return the Sampson distance of correspondences to homographies.
The first-order approximation of the geometric error: the squared distance by which
pts1andpts2together must move for \(x_2 \sim H x_1\) to hold (Hartley and Zisserman, Multiple View Geometry, 2nd ed., section 4.2.6). With \(x_1 = (x, y, 1)\), \(x_2 = (u, v, 1)\) and \(H x_1 = (p, q, w)\), the algebraic residuals \(\epsilon = (v w - q, p - u w)\) and their Jacobian \(J\) with respect to \((x, y, u, v)\) give \(d^2 = \epsilon^\top (J J^\top)^{-1} \epsilon\).- Convention:
Argument order and direction as
oneway_transfer_error():Hmapspts1topts2. Unlike the one-way transfer error, the distance accounts for noise in both images and does not depend on the scale ofH.For an affine
Hthe constraint is linear in the coordinates, and the distance is the exact geometric error.An exact correspondence scores 0; a correspondence whose \(J J^\top\) is singular scores
inf; a non-finite correspondence or homography gives NaN.Computed in at least float32 and returned in the promoted dtype of the inputs, or in float32 when they are all integers.
- Parameters:
pts1 (
Tensor) – points in the first image with shape \((B, N, 2)\), or homogeneous \((B, N, 3)\).pts2 (
Tensor) – points in the second image with shape \((B, N, 2)\), or homogeneous \((B, N, 3)\).H (
Tensor) – homographies with shape \((B, 3, 3)\). Points with a leading dimension of 1 are scored against every homography.squared (
bool, optional) – if True (default), the squared distance is returned, else the distance. Default:True
- Return type:
- Returns:
the computed distance with shape \((B, N)\).
- kornia.geometry.homography.symmetric_transfer_error(pts1, pts2, H, squared=True, eps=1e-8)[source]#
Return Symmetric transfer error for correspondences given the homography matrix.
- Convention:
Argument order as
oneway_transfer_error(). The squared value is the image-2 error ofHplus the image-1 error ofH^-1, andsquared=Falsereturns the square root of that sum.As
oneway_transfer_error(), the error does not depend on the scale ofHand an exact match scores exactly 0 withsquared=False.
- Parameters:
pts1 (
Tensor) – correspondences from the left images with shape (B, N, 2 or 3). If they are homogeneous, converted automatically.pts2 (
Tensor) – correspondences from the right images with shape (B, N, 2 or 3). If they are homogeneous, converted automatically.H (
Tensor) – Homographies with shape \((B, 3, 3)\).squared (
bool, optional) – if True (default), the squared distance is returned. Default:Trueeps (
float, optional) – unused, as inoneway_transfer_error()(#4881). Default:1e-8
- Return type:
- Returns:
the computed distance with shape \((B, N)\). Rows whose homography is not invertible score
max_num, i.e.torch.finfo(pts1.dtype).max, for both values ofsquared.