|
OpenCV for Unity 3.0.4
Enox Software / Please refer to OpenCV official document ( http://docs.opencv.org/5.0/index.html ) for the details of the argument of the method.
|
OpenCV AR utilities (engine-independent: drawing, frustum, projection, pose, Mat4d TRS). More...
Classes | |
| struct | Mat4d |
4×4 matrix (row-major layout: mRC is row R, column C). More... | |
| struct | PoseData |
| Position and rotation pose data for AR utilities. Rotation is a unit quaternion stored as (x, y, z, w). More... | |
Static Public Member Functions | |
| static Mat4d | CalculateCameraMatrixValuesFromProjectionMatrix (in Mat4d projectionMatrix, double width, double height) |
| Recovers OpenCV-style intrinsic values (fx, fy, cx, cy) from a projection matrix produced by CalculateProjectionMatrixFromCameraMatrixValues. Result is stored in a Mat4d with m00=fx, m11=fy, m02=cx, m12=cy, m22=1. | |
| static Matrix4x4 | CalculateCameraMatrixValuesFromProjectionMatrix (Matrix4x4 projectionMatrix, float width, float height) |
| Calculates OpenCV-style intrinsic camera parameters from a Unity-compatible projection matrix. Always assumes Unity Camera.projectionMatrix convention. No optional flag. | |
| static Mat4d | CalculateCameraMatrixValuesFromProjectionMatrix (ReadOnlySpan< double > projectionMatrix, double width, double height) |
| Span(16) overload: recovers intrinsics from a row-major projection matrix. | |
| static float | CalculateDistance (float frustumSize, float fov) |
| Calculates distance from frustum size and FOV angle (degrees). | |
| static float | CalculateFOVAngle (float frustumSize, float distance) |
| Calculates FOV angle (degrees) from frustum size and distance. | |
| static float | CalculateFrustumSize (float distance, float fov) |
| Calculates frustum size at a given distance and FOV angle (degrees). | |
| static Mat4d | CalculateProjectionMatrixFromCameraMatrixValues (double fx, double fy, double cx, double cy, double width, double height, double near=0.01, double far=2000.0) |
| Builds a 4×4 projection matrix from OpenCV-style camera intrinsics. | |
| static void | CalculateProjectionMatrixFromCameraMatrixValues (double fx, double fy, double cx, double cy, double width, double height, Span< double > destination, double near=0.01, double far=2000.0) |
| Span(16) overload: writes projection matrix in row-major order. | |
| static Matrix4x4 | CalculateProjectionMatrixFromCameraMatrixValues (float fx, float fy, float cx, float cy, float width, float height, float near=0.01f, float far=2000f) |
| Calculates a projection matrix from OpenCV-style camera intrinsic parameters, suitable for use as Camera.projectionMatrix in Unity. | |
| static Matrix4x4 | ComposeMatrix (Vector3 localPosition, Quaternion localRotation, Vector3 localScale) |
| Composes a TRS matrix from position, rotation and scale. | |
| static Mat4d | ComposeTrsMatrix (in Vec3d translation, in Vec4d rotation, in Vec3d scale) |
| Composes a TRS matrix from translation, unit quaternion (x,y,z,w), and scale. | |
| static void | ComposeTrsMatrix (in Vec3d translation, in Vec4d rotation, in Vec3d scale, Span< double > destination) |
| Span(16) overload: writes ComposeTrsMatrix result in row-major order. | |
| static PoseData | ConvertMatrixToPoseData (in Mat4d matrix) |
| Converts a TRS matrix to PoseData (translation + rotation; scale discarded). | |
| static PoseData | ConvertMatrixToPoseData (ReadOnlySpan< double > matrix) |
| Span(16) overload for ConvertMatrixToPoseData(in Mat4d). | |
| static PoseData | ConvertMatrixToPoseData (ref Matrix4x4 matrix) |
| Converts a Unity transform matrix to PoseData using Unity Quaternion.LookRotation. | |
| static Mat4d | ConvertPoseDataToMat4 (in PoseData poseData) |
| Converts PoseData to a TRS matrix with unit scale. No coordinate-system conversion. | |
| static void | ConvertPoseDataToMat4 (in PoseData poseData, Span< double > destination) |
| Span(16) overload: writes ConvertPoseDataToMat4 result in row-major order. | |
| static Matrix4x4 | ConvertPoseDataToMatrix (ref PoseData poseData, bool convertToUnityCoordinates=true) |
| Converts PoseData to a Unity transform matrix. | |
| static Vec4d | ConvertRvecToRotation (Mat rvec) |
| Converts an OpenCV rotation vector Mat to a unit quaternion (x, y, z, w). | |
| static void | ConvertRvecToRotation (Mat rvec, out Quaternion rotation) |
| Converts an OpenCV rotation vector Mat to a Unity Quaternion. | |
| static Vec4d | ConvertRvecToRotation (ReadOnlySpan< double > rvec) |
| Converts an OpenCV rotation vector (Rodrigues) to a unit quaternion (x, y, z, w). | |
| static void | ConvertRvecToRotation (ReadOnlySpan< double > rvec, out Quaternion rotation) |
| Converts an OpenCV rotation vector (Rodrigues) to a Unity Quaternion. | |
| static PoseData | ConvertRvecTvecToPoseData (Mat rvec, Mat tvec) |
| Converts rvec and tvec Mats to PoseData. | |
| static PoseData | ConvertRvecTvecToPoseData (ReadOnlySpan< double > rvec, ReadOnlySpan< double > tvec) |
| Converts rvec and tvec to PoseData. | |
| static Vec3d | ConvertTvecToTranslation (Mat tvec) |
| Converts an OpenCV translation vector Mat to Vec3d. | |
| static void | ConvertTvecToTranslation (Mat tvec, out Vector3 translation) |
| Converts an OpenCV translation vector Mat to a Unity Vector3. | |
| static Vec3d | ConvertTvecToTranslation (ReadOnlySpan< double > tvec) |
| Converts an OpenCV translation vector to Vec3d. | |
| static void | ConvertTvecToTranslation (ReadOnlySpan< double > tvec, out Vector3 translation) |
| Converts an OpenCV translation vector to a Unity Vector3. | |
| static Mat4d | CopyMatrix4x4ToMat4d (in Matrix4x4 matrix) |
| Copies a Unity Matrix4x4 to engine-independent Mat4d (row-major, 16 elements). | |
| static Dictionary< int, PoseData > | CreatePoseDataDict (ReadOnlySpan< int > ids, ReadOnlySpan< double > rvecs, ReadOnlySpan< double > tvecs) |
| Creates a pose data dictionary from marker ids and packed rvecs/tvecs (length = ids.Length * 3). | |
| static void | DecomposeMatrix (ref Matrix4x4 matrix, out Vector3 localPosition, out Quaternion localRotation, out Vector3 localScale) |
| Extract position, rotation and scale from TRS matrix. | |
| static void | DecomposeTrsMatrix (in Mat4d matrix, out Vec3d translation, out Vec4d rotation, out Vec3d scale) |
| Decomposes a TRS matrix into translation, rotation quaternion (x,y,z,w), and scale. Rotation is recovered from the orthonormalized basis (robust matrix-to-quaternion), independent of any engine LookRotation implementation. | |
| static void | DecomposeTrsMatrix (ReadOnlySpan< double > matrix, out Vec3d translation, out Vec4d rotation, out Vec3d scale) |
| Span(16) overload for DecomposeTrsMatrix(in Mat4d, out Vec3d, out Vec4d, out Vec3d). | |
| static Quaternion | ExtractRotationFromMatrix (ref Matrix4x4 matrix) |
| Extract rotation quaternion from transform matrix via Quaternion.LookRotation. | |
| static Vector3 | ExtractScaleFromMatrix (ref Matrix4x4 matrix) |
| Extract scale from transform matrix. | |
| static Vector3 | ExtractTranslationFromMatrix (ref Matrix4x4 matrix) |
| Extract translation from transform matrix. | |
| static void | LowpassPoseData (ref PoseData oldPose, ref PoseData newPose, float posThreshold, float rotThreshold) |
| Suppresses position and rotation changes in newPose that are below the given thresholds. | |
| static void | LowpassPoseDataDict (Dictionary< int, PoseData > oldDict, Dictionary< int, PoseData > newDict, float posThreshold, float rotThreshold) |
| Applies LowpassPoseData for each key present in both dictionaries. | |
| static void | SafeDrawFrameAxes (Mat image, Mat cameraMatrix, Mat distCoeffs, Mat rvec, Mat tvec, float length, int thickness=3) |
| Safe version of drawFrameAxes that skips drawing when tvec has a near-zero Z component. | |
| static void | SetTransformFromMatrix (Transform transform, ref Matrix4x4 matrix) |
| Set transform component from TRS matrix. | |
| static void | SmoothingFilterPoseData (ref PoseData oldPose, ref PoseData newPose, float positionSmoothingFactor, float rotationSmoothingFactor) |
| Applies exponential moving average smoothing (translation lerp + rotation nlerp). | |
| static void | SmoothingFilterPoseDataDict (Dictionary< int, PoseData > oldDict, Dictionary< int, PoseData > newDict, float positionSmoothingFactor, float rotationSmoothingFactor) |
| Applies SmoothingFilterPoseData for each key present in both dictionaries. | |
OpenCV AR utilities (engine-independent: drawing, frustum, projection, pose, Mat4d TRS).
OpenCV AR utilities — Unity Integration partial (Quaternion/Vector3 sugar, Unity projection, TRS / Transform / handedness).
|
static |
Recovers OpenCV-style intrinsic values (fx, fy, cx, cy) from a projection matrix produced by CalculateProjectionMatrixFromCameraMatrixValues. Result is stored in a Mat4d with m00=fx, m11=fy, m02=cx, m12=cy, m22=1.
| projectionMatrix | Row-major projection matrix from CalculateProjectionMatrixFromCameraMatrixValues. |
| width | Image width (pixels) used when the projection matrix was built. |
| height | Image height (pixels) used when the projection matrix was built. |
|
static |
Calculates OpenCV-style intrinsic camera parameters from a Unity-compatible projection matrix. Always assumes Unity Camera.projectionMatrix convention. No optional flag.
| projectionMatrix | Unity-compatible projection matrix. |
| width | Image width (pixels) used when the projection matrix was built. |
| height | Image height (pixels) used when the projection matrix was built. |
|
static |
Span(16) overload: recovers intrinsics from a row-major projection matrix.
| projectionMatrix | Row-major projection matrix (length must be 16). |
| width | Image width (pixels) used when the projection matrix was built. |
| height | Image height (pixels) used when the projection matrix was built. |
| ArgumentException | Thrown when projectionMatrix .Length is not 16. |
|
static |
Calculates distance from frustum size and FOV angle (degrees).
| frustumSize | Frustum width (or height) at the target distance. |
| fov | Field of view angle in degrees. |
|
static |
Calculates FOV angle (degrees) from frustum size and distance.
| frustumSize | Frustum width (or height) at distance . |
| distance | Distance from the camera along the view axis. |
|
static |
Calculates frustum size at a given distance and FOV angle (degrees).
| distance | Distance from the camera along the view axis. |
| fov | Field of view angle in degrees. |
|
static |
Builds a 4×4 projection matrix from OpenCV-style camera intrinsics.
Convention (fixed for this core API): OpenCV image origin is top-left (Y down); the returned matrix uses a Y-up principal-point mapping (m12 = -1 + 2*cy/height), matching a common AR camera projection matrix layout in row-major Mat4d elements. No engine runtime is required.
| fx | Focal length along the X axis (pixels). |
| fy | Focal length along the Y axis (pixels). |
| cx | Principal point X coordinate (pixels). |
| cy | Principal point Y coordinate (pixels). |
| width | Image width (pixels). |
| height | Image height (pixels). |
| near | Near clipping plane distance. |
| far | Far clipping plane distance. |
|
static |
Span(16) overload: writes projection matrix in row-major order.
| fx | Focal length along the X axis (pixels). |
| fy | Focal length along the Y axis (pixels). |
| cx | Principal point X coordinate (pixels). |
| cy | Principal point Y coordinate (pixels). |
| width | Image width (pixels). |
| height | Image height (pixels). |
| destination | Destination span (length must be 16). |
| near | Near clipping plane distance. |
| far | Far clipping plane distance. |
| ArgumentException | Thrown when destination .Length is not 16. |
|
static |
Calculates a projection matrix from OpenCV-style camera intrinsic parameters, suitable for use as Camera.projectionMatrix in Unity.
Always converts from OpenCV's right-handed camera coordinates (Z-forward, Y-down) to Unity's left-handed camera coordinates (Z-forward, Y-up). No optional flag.
| fx | Focal length along the X axis (pixels). |
| fy | Focal length along the Y axis (pixels). |
| cx | Principal point X coordinate (pixels). |
| cy | Principal point Y coordinate (pixels). |
| width | Image width (pixels). |
| height | Image height (pixels). |
| near | Near clipping plane distance. |
| far | Far clipping plane distance. |
|
static |
Composes a TRS matrix from position, rotation and scale.
| localPosition | Local position. |
| localRotation | Local rotation. |
| localScale | Local scale. |
|
static |
Composes a TRS matrix from translation, unit quaternion (x,y,z,w), and scale.
| translation | Translation vector. |
| rotation | Unit quaternion (x, y, z, w). |
| scale | Per-axis scale. |
|
static |
Span(16) overload: writes ComposeTrsMatrix result in row-major order.
| translation | Translation vector. |
| rotation | Unit quaternion (x, y, z, w). |
| scale | Per-axis scale. |
| destination | Destination span (length must be 16). |
| ArgumentException | Thrown when destination .Length is not 16. |
|
static |
Converts a TRS matrix to PoseData (translation + rotation; scale discarded).
| matrix | Row-major 4×4 TRS matrix. |
|
static |
Span(16) overload for ConvertMatrixToPoseData(in Mat4d).
| matrix | Row-major matrix elements (length must be 16). |
| ArgumentException | Thrown when matrix .Length is not 16. |
|
static |
Converts a Unity transform matrix to PoseData using Unity Quaternion.LookRotation.
| matrix | Unity transform matrix. |
|
static |
Converts PoseData to a TRS matrix with unit scale. No coordinate-system conversion.
| poseData | Pose data (translation + rotation). |
|
static |
Span(16) overload: writes ConvertPoseDataToMat4 result in row-major order.
| poseData | Pose data (translation + rotation). |
| destination | Destination span (length must be 16). |
| ArgumentException | Thrown when destination .Length is not 16. |
|
static |
Converts PoseData to a Unity transform matrix.
| poseData | Pose data. |
| convertToUnityCoordinates | When true (default), converts from OpenCV's right-handed coordinate system to Unity's left-handed coordinate system by inverting the Y-axis. Enable this when applying an OpenCV-estimated pose to a Unity object. When false, returns the matrix without coordinate-system conversion. |
Converts an OpenCV rotation vector Mat to a unit quaternion (x, y, z, w).
| rvec | Rotation vector Mat (continuous, CV_64F, at least 3 elements). |
| ArgumentException | Thrown when rvec is not continuous or does not contain at least 3 CV_64F elements. |
|
static |
Converts an OpenCV rotation vector Mat to a Unity Quaternion.
| rvec | Rotation vector Mat (continuous, CV_64F, at least 3 elements). |
| rotation | Converted Unity rotation quaternion. |
|
static |
Converts an OpenCV rotation vector (Rodrigues) to a unit quaternion (x, y, z, w).
| rvec | Rotation vector (at least 3 elements). |
| ArgumentException | Thrown when rvec .Length is less than 3. |
|
static |
Converts an OpenCV rotation vector (Rodrigues) to a Unity Quaternion.
Same name as the core ConvertRvecToRotation(ReadOnlySpan<double>) API; the out Quaternion parameter selects this Unity overload (C# does not allow overloading by return type alone).
| rvec | Rotation vector (at least 3 elements). |
| rotation | Converted Unity rotation quaternion. |
|
static |
Converts rvec and tvec Mats to PoseData.
| rvec | Rotation vector Mat (continuous, CV_64F, at least 3 elements). |
| tvec | Translation vector Mat (continuous, CV_64F, at least 3 elements). |
|
static |
Converts rvec and tvec to PoseData.
| rvec | Rotation vector (at least 3 elements). |
| tvec | Translation vector (at least 3 elements). |
|
static |
Converts an OpenCV translation vector Mat to a Unity Vector3.
| tvec | Translation vector Mat (continuous, CV_64F, at least 3 elements). |
| translation | Converted Unity position vector. |
|
static |
|
static |
Converts an OpenCV translation vector to a Unity Vector3.
Same name as the core ConvertTvecToTranslation(ReadOnlySpan<double>) API; the out Vector3 parameter selects this Unity overload.
| tvec | Translation vector (at least 3 elements). |
| translation | Converted Unity position vector. |
|
static |
|
static |
Creates a pose data dictionary from marker ids and packed rvecs/tvecs (length = ids.Length * 3).
| ids | Marker identifiers. |
| rvecs | Packed rotation vectors (3 doubles per id). |
| tvecs | Packed translation vectors (3 doubles per id). |
| ArgumentException | Thrown when ids.Length * 3 does not equal rvecs .Length and tvecs .Length. |
|
static |
Extract position, rotation and scale from TRS matrix.
| matrix | Transform matrix. |
| localPosition | Extracted local position. |
| localRotation | Extracted local rotation. |
| localScale | Extracted local scale. |
|
static |
Decomposes a TRS matrix into translation, rotation quaternion (x,y,z,w), and scale. Rotation is recovered from the orthonormalized basis (robust matrix-to-quaternion), independent of any engine LookRotation implementation.
| matrix | Row-major 4×4 TRS matrix. |
| translation | Extracted translation. |
| rotation | Extracted unit quaternion (x, y, z, w). |
| scale | Extracted per-axis scale. |
|
static |
Span(16) overload for DecomposeTrsMatrix(in Mat4d, out Vec3d, out Vec4d, out Vec3d).
| matrix | Row-major matrix elements (length must be 16). |
| translation | Extracted translation. |
| rotation | Extracted unit quaternion (x, y, z, w). |
| scale | Extracted per-axis scale. |
| ArgumentException | Thrown when matrix .Length is not 16. |
|
static |
Extract rotation quaternion from transform matrix via Quaternion.LookRotation.
| matrix | Transform matrix. |
|
static |
Extract scale from transform matrix.
| matrix | Transform matrix. |
|
static |
Extract translation from transform matrix.
| matrix | Transform matrix. |
|
static |
Suppresses position and rotation changes in newPose that are below the given thresholds.
| oldPose | Previous pose (read-only reference). |
| newPose | New pose to filter; small changes are replaced with values from oldPose . |
| posThreshold | Position threshold (same units as translation). |
| rotThreshold | Rotation threshold in degrees. |
|
static |
Applies LowpassPoseData for each key present in both dictionaries.
| oldDict | Previous pose dictionary. |
| newDict | New pose dictionary; matching entries are filtered in place. |
| posThreshold | Position threshold (same units as translation). |
| rotThreshold | Rotation threshold in degrees. |
| ArgumentNullException | Thrown when oldDict or newDict is null. |
|
static |
Safe version of drawFrameAxes that skips drawing when tvec has a near-zero Z component.
| image | Target image to draw on. |
| cameraMatrix | 3×3 camera intrinsic matrix. |
| distCoeffs | Distortion coefficients (may be empty). |
| rvec | Rotation vector (Rodrigues, CV_64F, at least 3 elements). |
| tvec | Translation vector (CV_64F, at least 3 elements). Drawing is skipped when Z < 1e-4. |
| length | Axis length in world units. |
| thickness | Line thickness in pixels. |
|
static |
Set transform component from TRS matrix.
| transform | Target Transform. |
| matrix | TRS matrix to apply to transform . |
|
static |
Applies exponential moving average smoothing (translation lerp + rotation nlerp).
| oldPose | Previous pose used as the smoothing reference. |
| newPose | New pose to smooth; updated in place. |
| positionSmoothingFactor | 0 = no filtering, 1 = maximum filtering. |
| rotationSmoothingFactor | 0 = no filtering, 1 = maximum filtering. |
|
static |
Applies SmoothingFilterPoseData for each key present in both dictionaries.
| oldDict | Previous pose dictionary. |
| newDict | New pose dictionary; matching entries are smoothed in place. |
| positionSmoothingFactor | 0 = no filtering, 1 = maximum filtering. |
| rotationSmoothingFactor | 0 = no filtering, 1 = maximum filtering. |
| ArgumentNullException | Thrown when oldDict or newDict is null. |