LensDistortionData
A description of estimated lens distortion that can be used to rectify images.
Declaration
struct LensDistortionDataOverview
See also: AVCameraCalibrationData
Example of a per-pixel undistort function using this data:
func undistortPoint(lensDistortionData: LensDistortionData,
imageSize: SIMD2<Float>,
point: SIMD2<Float>) -> SIMD2<Float> {
// The lookup table holds the relative radial magnification for n linearly spaced radii.
// The first position corresponds to radius = 0
// The last position corresponds to the largest radius found in the image.
// Determine the maximum radius.
let opticalCenter: SIMD2<Float> = lensDistortionData.center
let deltaXMax: Float = max(opticalCenter.x, imageSize.x - opticalCenter.x)
let deltaYMax: Float = max(opticalCenter.y, imageSize.y - opticalCenter.y)
let radiusMax: Float = sqrt(deltaXMax * deltaXMax + deltaYMax * deltaYMax)
// Determine the vector from the optical center to the given point.
let centerToPoint: SIMD2<Float> = point - opticalCenter
// Determine the radius of the given point.
let pointRadius: Float = length(centerToPoint)
// Look up the relative radial magnification to apply in the provided lookup table
var magnification: Float = 1.0
let radialLut = lensDistortionData.radialLookupTable
if pointRadius < radiusMax {
// Linear interpolation based on piecewise linear function of normalized radius
let value = pointRadius * Float(radialLut.count - 1) / radiusMax
let index = Int(value)
let fraction = value - Float(index)
let magLeft = radialLut[index]
let magRight = radialLut[index + 1]
magnification = ( 1.0 - fraction ) * magLeft + fraction * magRight
} else {
magnification = radialLut[radialLut.count - 1]
}
// Apply radial magnification
let centerToRectifiedPoint: SIMD2<Float> = centerToPoint + magnification * centerToPoint
// Construct output
return opticalCenter + centerToRectifiedPoint
}