#![no_std]
use cv_core::nalgebra::{Matrix3, Point2, Point3, Vector2, Vector3};
use cv_core::{
Bearing, CameraModel, CameraPoint, CameraToCamera, FeatureMatch, ImagePoint, KeyPoint, Pose,
Projective, TriangulatorRelative,
};
use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Into};
use num_traits::Float;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, AsMut, AsRef, Deref, DerefMut, From, Into)]
pub struct NormalizedKeyPoint(pub Point2<f64>);
impl NormalizedKeyPoint {
pub fn from_camera_point(point: CameraPoint) -> Option<Self> {
Point2::from_homogeneous(point.bearing_unnormalized()).map(Self)
}
pub fn with_depth(self, depth: f64) -> CameraPoint {
(self.coords * depth).push(depth).to_homogeneous().into()
}
pub fn with_distance(self, distance: f64) -> CameraPoint {
(distance * *self.bearing()).to_homogeneous().into()
}
pub fn virtual_image_point(self) -> Point3<f64> {
self.coords.push(1.0).into()
}
}
impl Bearing for NormalizedKeyPoint {
fn bearing_unnormalized(&self) -> Vector3<f64> {
self.0.coords.push(1.0)
}
fn from_bearing_vector(bearing: Vector3<f64>) -> Self {
Self((bearing.xy() / bearing.z).into())
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct CameraIntrinsics {
pub focals: Vector2<f64>,
pub principal_point: Point2<f64>,
pub skew: f64,
}
impl CameraIntrinsics {
pub fn identity() -> Self {
Self {
focals: Vector2::new(1.0, 1.0),
skew: 0.0,
principal_point: Point2::new(0.0, 0.0),
}
}
pub fn focals(self, focals: Vector2<f64>) -> Self {
Self { focals, ..self }
}
pub fn focal(self, focal: f64) -> Self {
Self {
focals: Vector2::new(focal, focal),
..self
}
}
pub fn principal_point(self, principal_point: Point2<f64>) -> Self {
Self {
principal_point,
..self
}
}
pub fn skew(self, skew: f64) -> Self {
Self { skew, ..self }
}
#[rustfmt::skip]
pub fn matrix(&self) -> Matrix3<f64> {
Matrix3::new(
self.focals.x, self.skew, self.principal_point.x,
0.0, self.focals.y, self.principal_point.y,
0.0, 0.0, 1.0,
)
}
}
impl CameraModel for CameraIntrinsics {
type Projection = NormalizedKeyPoint;
fn calibrate<P>(&self, point: P) -> NormalizedKeyPoint
where
P: ImagePoint,
{
let centered = point.image_point() - self.principal_point;
let y = centered.y / self.focals.y;
let x = (centered.x - self.skew * y) / self.focals.x;
NormalizedKeyPoint(Point2::new(x, y))
}
fn uncalibrate(&self, projection: NormalizedKeyPoint) -> KeyPoint {
let y = projection.y * self.focals.y;
let x = projection.x * self.focals.x + self.skew * projection.y;
let centered = Point2::new(x, y);
KeyPoint(centered + self.principal_point.coords)
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct CameraIntrinsicsK1Distortion {
pub simple_intrinsics: CameraIntrinsics,
pub k1: f64,
}
impl CameraIntrinsicsK1Distortion {
pub fn new(simple_intrinsics: CameraIntrinsics, k1: f64) -> Self {
Self {
simple_intrinsics,
k1,
}
}
}
impl CameraModel for CameraIntrinsicsK1Distortion {
type Projection = NormalizedKeyPoint;
fn calibrate<P>(&self, point: P) -> NormalizedKeyPoint
where
P: ImagePoint,
{
let NormalizedKeyPoint(distorted) = self.simple_intrinsics.calibrate(point);
let r2 = distorted.coords.norm_squared();
let undistorted = (distorted.coords / (1.0 + self.k1 * r2)).into();
NormalizedKeyPoint(undistorted)
}
fn uncalibrate(&self, projection: NormalizedKeyPoint) -> KeyPoint {
let NormalizedKeyPoint(undistorted) = projection;
let u2 = undistorted.coords.norm_squared();
let r2_mul_k1 = -(2.0 * self.k1 * u2 + Float::sqrt(1.0 - 4.0 * self.k1 * u2) - 1.0)
/ (2.0 * self.k1 * u2);
self.simple_intrinsics.uncalibrate(NormalizedKeyPoint(
(undistorted.coords * (1.0 + r2_mul_k1)).into(),
))
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct CameraSpecification {
pub pixels: Vector2<usize>,
pub pixel_dimensions: Vector2<f64>,
}
impl CameraSpecification {
pub fn from_sensor(pixels: Vector2<usize>, sensor_dimensions: Vector2<f64>) -> Self {
Self {
pixels,
pixel_dimensions: Vector2::new(
sensor_dimensions.x / pixels.x as f64,
sensor_dimensions.y / pixels.y as f64,
),
}
}
pub fn from_sensor_square(pixels: Vector2<usize>, sensor_width: f64) -> Self {
let pixel_width = sensor_width / pixels.x as f64;
Self {
pixels,
pixel_dimensions: Vector2::new(pixel_width, pixel_width),
}
}
pub fn intrinsics_centered(&self, focal: f64) -> CameraIntrinsics {
CameraIntrinsics::identity()
.focal(focal)
.principal_point(self.pixel_dimensions.map(|p| p as f64 / 2.0 - 0.5).into())
}
}
pub fn pose_reprojection_error(
pose: CameraToCamera,
m: FeatureMatch<NormalizedKeyPoint>,
triangulator: impl TriangulatorRelative,
) -> Option<[Vector2<f64>; 2]> {
let FeatureMatch(a, b) = m;
triangulator
.triangulate_relative(pose, a, b)
.and_then(|point_a| {
let reproject_a = NormalizedKeyPoint::from_camera_point(point_a)?;
let point_b = pose.transform(point_a);
let reproject_b = NormalizedKeyPoint::from_camera_point(point_b)?;
Some([a.0 - reproject_a.0, b.0 - reproject_b.0])
})
}
pub fn average_pose_reprojection_error(
pose: CameraToCamera,
m: FeatureMatch<NormalizedKeyPoint>,
triangulator: impl TriangulatorRelative,
) -> Option<f64> {
pose_reprojection_error(pose, m, triangulator)
.map(|errors| errors.iter().map(|v| v.norm()).sum::<f64>() * 0.5)
}