ds_msp.cv¶
A cv2/cv2.fisheye-signature-compatible shim over the library's own models, so existing
OpenCV call sites drop in with minimal changes. See
Solve PnP on a fisheye and
Undistort images.
ds_msp.cv ¶
Double Sphere Camera Model - OpenCV-style Wrapper¶
This module provides a functional interface for the Double Sphere camera model
that mimics the cv2.fisheye module signatures. This allows for easy integration
into existing OpenCV-based pipelines.
The distortion coefficients D are assumed to be [xi, alpha].
distortPoints ¶
Distort 2D points.
Mimics cv2.fisheye.distortPoints.
Input points are interpreted as normalized image-plane coordinates (x, y)
on the z = 1 plane, matching the convention of undistortPoints with no
P matrix. They are lifted to 3D rays (x, y, 1) and projected through the
Double Sphere model defined by K and D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
undistorted
|
(N, 1, 2) or (N, 2) array
|
Undistorted points on the normalized plane (z = 1). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
distorted |
(N, 1, 2) array
|
|
Source code in ds_msp/cv.py
estimateNewCameraMatrixForUndistortRectify ¶
estimateNewCameraMatrixForUndistortRectify(K: ndarray, D: ndarray, image_size: Tuple[int, int], R: Optional[ndarray] = None, balance: float = 0.0, new_size: Optional[Tuple[int, int]] = None, fov_scale: float = 1.0) -> np.ndarray
Estimate a balanced pinhole camera matrix for undistortion/rectification.
Mimics cv2.fisheye.estimateNewCameraMatrixForUndistortRectify's call
signature, but the underlying rule is this library's own
:func:ds_msp.core.pinhole.balanced_pinhole_K: the new focal length is
(fx + fy) / 2 * (0.4 + 0.4 * balance) — balance=0.0 gives 0.4x
the average input focal (widest field of view, most black border) and
balance=1.0 gives 0.8x (tightest crop, no border). The principal
point is placed at the output image center. R and fov_scale are
accepted for signature compatibility but are ignored — unlike real
OpenCV, this does not support a rectification rotation or an explicit FOV
scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
K
|
ndarray of shape (3, 3)
|
Camera matrix of the original (distorted) image. |
required |
D
|
ndarray of shape (2,)
|
Distortion coefficients. Unused by this implementation (the new focal
is derived from |
required |
image_size
|
tuple of (int, int)
|
Original image |
required |
R
|
ndarray of shape (3, 3)
|
Ignored (accepted for signature compatibility with |
None
|
balance
|
float
|
Field-of-view/border trade-off in |
0.0
|
new_size
|
tuple of (int, int)
|
Output image |
None
|
fov_scale
|
float
|
Ignored (accepted for signature compatibility with |
1.0
|
Returns:
| Type | Description |
|---|---|
ndarray of shape (3, 3)
|
The new pinhole camera matrix, |
Source code in ds_msp/cv.py
initUndistortRectifyMap ¶
initUndistortRectifyMap(K: ndarray, D: ndarray, R: ndarray, P: ndarray, size: Tuple[int, int], m1type: int) -> Tuple[np.ndarray, np.ndarray]
Compute undistortion and rectification maps.
Mimics cv2.fisheye.initUndistortRectifyMap: for every destination pixel,
lifts it to a normalized ray via P, rotates by R.T into the original
camera frame, then projects through the Double Sphere model (K, D) to
find the source pixel — the standard backward-remap pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
K
|
ndarray of shape (3, 3)
|
Camera matrix of the original (distorted) image. |
required |
D
|
ndarray of shape (2,)
|
Distortion coefficients |
required |
R
|
ndarray of shape (3, 3)
|
Rectification rotation (pass |
required |
P
|
ndarray of shape (3, 3)
|
New camera matrix for the destination (undistorted) image. |
required |
size
|
tuple of (int, int)
|
Destination image |
required |
m1type
|
int
|
Requested map type. Only |
required |
Returns:
| Name | Type | Description |
|---|---|---|
map1 |
ndarray of shape (height, width)
|
Source-image |
map2 |
ndarray of shape (height, width)
|
Source-image |
Source code in ds_msp/cv.py
projectPoints ¶
projectPoints(objectPoints: ndarray, rvec: ndarray, tvec: ndarray, K: ndarray, D: ndarray, alpha: float = 0.0, jacobian: Optional[ndarray] = None) -> Tuple[np.ndarray, Optional[np.ndarray]]
Project 3D points to image plane.
Mimics cv2.fisheye.projectPoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPoints
|
(N, 1, 3) or (N, 3) array
|
3D points in world coordinates. |
required |
rvec
|
(3, 1) or (3,) array
|
Rotation vector. |
required |
tvec
|
(3, 1) or (3,) array
|
Translation vector. |
required |
K
|
(3, 3) array
|
Camera matrix [fx, 0, cx; 0, fy, cy; 0, 0, 1]. |
required |
D
|
(2,) or (4,) array
|
Distortion coefficients [xi, alpha]. |
required |
alpha
|
float
|
Skew parameter (not supported, ignored). |
0.0
|
jacobian
|
optional
|
Not supported, always returns None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
imagePoints |
(N, 1, 2) array
|
Projected 2D points. |
jacobian |
None
|
|
Source code in ds_msp/cv.py
solvePnP ¶
solvePnP(objectPoints: ndarray, imagePoints: ndarray, K: ndarray, D: ndarray, rvec: Optional[ndarray] = None, tvec: Optional[ndarray] = None, useExtrinsicGuess: bool = False, flags: int = cv2.SOLVEPNP_ITERATIVE) -> Tuple[bool, np.ndarray, np.ndarray]
Find an object pose from 3D-2D point correspondences on a fisheye image.
Mimics cv2.solvePnP's call signature: builds a
:class:~ds_msp.model.DoubleSphereCamera from K/D and delegates to
:meth:~ds_msp.model.DoubleSphereCamera.solve_pnp, which unprojects to
bearing rays and solves PnP in the normalized plane (see
Solve PnP on a fisheye). rvec,
tvec, and useExtrinsicGuess are accepted for signature
compatibility but ignored — this implementation always solves from
scratch and cannot warm-start from an extrinsic guess.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectPoints
|
ndarray of shape (N, 3) or (N, 1, 3)
|
3D points in world/object coordinates. |
required |
imagePoints
|
ndarray of shape (N, 2) or (N, 1, 2)
|
Corresponding distorted fisheye pixels. |
required |
K
|
ndarray of shape (3, 3)
|
Camera matrix. |
required |
D
|
ndarray of shape (2,)
|
Distortion coefficients |
required |
rvec
|
ndarray
|
Ignored (accepted for signature compatibility with |
None
|
tvec
|
ndarray
|
Ignored (accepted for signature compatibility with |
None
|
useExtrinsicGuess
|
bool
|
Ignored (accepted for signature compatibility with |
False
|
flags
|
int
|
OpenCV PnP method passed through to the underlying solve. |
cv2.SOLVEPNP_ITERATIVE
|
Returns:
| Name | Type | Description |
|---|---|---|
success |
bool
|
Whether the solve succeeded. |
rvec |
ndarray of shape (3, 1)
|
Rodrigues rotation vector. Zeros if |
tvec |
ndarray of shape (3, 1)
|
Translation vector, same units as |
Source code in ds_msp/cv.py
undistortImage ¶
undistortImage(distorted: ndarray, K: ndarray, D: ndarray, Knew: Optional[ndarray] = None, new_size: Optional[Tuple[int, int]] = None) -> np.ndarray
Undistort a fisheye image into a rectified pinhole view.
Mimics cv2.fisheye.undistortImage: builds the backward-remap maps with
:func:initUndistortRectifyMap and applies them with cv2.remap. See
the Undistort a fisheye image how-to for a
worked example including the balance field-of-view trade-off.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distorted
|
ndarray of shape (H, W, ...)
|
Source (distorted) image. |
required |
K
|
ndarray of shape (3, 3)
|
Camera matrix of |
required |
D
|
ndarray of shape (2,)
|
Distortion coefficients |
required |
Knew
|
ndarray of shape (3, 3)
|
New camera matrix for the output image. Defaults to |
None
|
new_size
|
tuple of (int, int)
|
Output image |
None
|
Returns:
| Type | Description |
|---|---|
ndarray of shape (new_size[1], new_size[0], ...)
|
The rectified image. Pixels with no valid source ray are filled per
|
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Source code in ds_msp/cv.py
undistortPoints ¶
undistortPoints(distorted: ndarray, K: ndarray, D: ndarray, R: Optional[ndarray] = None, P: Optional[ndarray] = None) -> np.ndarray
Undistort 2D points.
Mimics cv2.fisheye.undistortPoints: unprojects each distorted pixel to a
unit bearing ray through the Double Sphere model, optionally rotates it by
R, then reprojects to the z=1 normalized plane (or through P if
given).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distorted
|
ndarray of shape (N, 1, 2) or (N, 2)
|
Distorted 2D pixels in the original image. |
required |
K
|
ndarray of shape (3, 3)
|
Camera matrix |
required |
D
|
ndarray of shape (2,)
|
Distortion coefficients |
required |
R
|
ndarray of shape (3, 3)
|
Rectification rotation applied to the unprojected rays before the final projection. Defaults to no rotation. |
None
|
P
|
ndarray of shape (3, 3)
|
New camera matrix used to re-project the rectified rays to pixels. If
omitted, points are returned as normalized coordinates |
None
|
Returns:
| Type | Description |
|---|---|
ndarray of shape (N, 1, 2)
|
Rectified points: normalized coordinates if |