pub struct PinholeCamera {
pub fx: f64,
pub fy: f64,
pub cx: f64,
pub cy: f64,
pub k1: f64,
pub k2: f64,
pub p1: f64,
pub p2: f64,
}Expand description
Pinhole camera with radial and tangential (Brown–Conrady) distortion.
Coordinate convention: X right, Y down, Z forward (optical axis).
Projection (ideal): u = fx * X/Z + cx, v = fy * Y/Z + cy.
Distortion model (normalised coordinates xn = X/Z, yn = Y/Z):
r² = xn² + yn²
xd = xn*(1 + k1*r² + k2*r⁴) + 2*p1*xn*yn + p2*(r² + 2*xn²)
yd = yn*(1 + k1*r² + k2*r⁴) + p1*(r² + 2*yn²) + 2*p2*xn*yn
u = fx*xd + cx, v = fy*yd + cyFields§
§fx: f64Focal length in pixels along X.
fy: f64Focal length in pixels along Y.
cx: f64Principal point X (pixels).
cy: f64Principal point Y (pixels).
k1: f64Radial distortion coefficient k1.
k2: f64Radial distortion coefficient k2.
p1: f64Tangential distortion coefficient p1.
p2: f64Tangential distortion coefficient p2.
Implementations§
Source§impl PinholeCamera
impl PinholeCamera
Sourcepub fn new(
fx: f64,
fy: f64,
cx: f64,
cy: f64,
k1: f64,
k2: f64,
p1: f64,
p2: f64,
) -> Self
pub fn new( fx: f64, fy: f64, cx: f64, cy: f64, k1: f64, k2: f64, p1: f64, p2: f64, ) -> Self
Create a new camera with all parameters explicit.
Sourcepub fn ideal(fx: f64, fy: f64, cx: f64, cy: f64) -> Self
pub fn ideal(fx: f64, fy: f64, cx: f64, cy: f64) -> Self
Create a camera with no distortion (k1=k2=p1=p2=0).
Sourcepub fn project(&self, point_3d: &[f64; 3]) -> Result<[f64; 2]>
pub fn project(&self, point_3d: &[f64; 3]) -> Result<[f64; 2]>
Project a 3-D point to a 2-D pixel using the ideal pinhole model (distortion coefficients are ignored).
Returns Err if Z ≤ 0.
§Example
use scirs2_vision::camera::PinholeCamera;
let cam = PinholeCamera::ideal(800.0, 800.0, 320.0, 240.0);
let px = cam.project(&[0.0, 0.0, 1.0]).unwrap();
assert!((px[0] - 320.0).abs() < 1e-9);
assert!((px[1] - 240.0).abs() < 1e-9);Sourcepub fn project_distorted(&self, point_3d: &[f64; 3]) -> Result<[f64; 2]>
pub fn project_distorted(&self, point_3d: &[f64; 3]) -> Result<[f64; 2]>
Project a 3-D point to a 2-D pixel applying radial and tangential distortion.
Returns Err if Z ≤ 0.
§Example
use scirs2_vision::camera::PinholeCamera;
let cam = PinholeCamera::new(800.0, 800.0, 320.0, 240.0, 0.1, 0.0, 0.0, 0.0);
let px = cam.project_distorted(&[0.0, 0.0, 1.0]).unwrap();
// On the principal axis distortion has no effect.
assert!((px[0] - 320.0).abs() < 1e-9);
assert!((px[1] - 240.0).abs() < 1e-9);Sourcepub fn backproject(&self, pixel: &[f64; 2], depth: f64) -> [f64; 3]
pub fn backproject(&self, pixel: &[f64; 2], depth: f64) -> [f64; 3]
Back-project a 2-D pixel plus a known depth into a 3-D point (inverse of the ideal projection, distortion not reversed here).
§Arguments
pixel–[u, v]pixel coordinates.depth– depth along the optical axis (Z), must be positive.
§Example
use scirs2_vision::camera::PinholeCamera;
let cam = PinholeCamera::ideal(800.0, 800.0, 320.0, 240.0);
let pt = cam.backproject(&[320.0, 240.0], 5.0);
assert!((pt[2] - 5.0).abs() < 1e-9);Sourcepub fn undistort_point(&self, pixel: &[f64; 2]) -> [f64; 2]
pub fn undistort_point(&self, pixel: &[f64; 2]) -> [f64; 2]
Iteratively undistort a distorted pixel coordinate.
Uses Newton-style fixed-point iteration (max 20 steps) to invert the Brown–Conrady distortion model.
§Example
use scirs2_vision::camera::PinholeCamera;
let cam = PinholeCamera::new(800.0, 800.0, 320.0, 240.0, 0.1, 0.0, 0.0, 0.0);
// A point on the principal axis is unaffected by distortion.
let undist = cam.undistort_point(&[320.0, 240.0]);
assert!((undist[0] - 320.0).abs() < 1e-9);
assert!((undist[1] - 240.0).abs() < 1e-9);Sourcepub fn intrinsic_matrix(&self) -> Array2<f64>
pub fn intrinsic_matrix(&self) -> Array2<f64>
Return the 3×3 intrinsic (calibration) matrix K as an Array2<f64>.
K = [[fx, 0, cx],
[ 0, fy, cy],
[ 0, 0, 1]]§Example
use scirs2_vision::camera::PinholeCamera;
let cam = PinholeCamera::ideal(400.0, 400.0, 200.0, 150.0);
let k = cam.intrinsic_matrix();
assert_eq!(k.shape(), &[3, 3]);
assert!((k[[0, 0]] - 400.0).abs() < 1e-9);Sourcepub fn distortion_coeffs(&self) -> [f64; 4]
pub fn distortion_coeffs(&self) -> [f64; 4]
Distortion coefficient vector [k1, k2, p1, p2].
Trait Implementations§
Source§impl Clone for PinholeCamera
impl Clone for PinholeCamera
Source§fn clone(&self) -> PinholeCamera
fn clone(&self) -> PinholeCamera
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PinholeCamera
impl Debug for PinholeCamera
Source§impl PartialEq for PinholeCamera
impl PartialEq for PinholeCamera
impl StructuralPartialEq for PinholeCamera
Auto Trait Implementations§
impl Freeze for PinholeCamera
impl RefUnwindSafe for PinholeCamera
impl Send for PinholeCamera
impl Sync for PinholeCamera
impl Unpin for PinholeCamera
impl UnsafeUnpin for PinholeCamera
impl UnwindSafe for PinholeCamera
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> Scalar for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.