Radial Distortion
Implement a function to apply radial distortion to normalized image coordinates, a crucial step in correcting lens distortions in computer vision. This process involves modeling the distortion that occurs when light passes through a camera lens, causing images to appear warped.
Radial distortion is a type of distortion that occurs when light rays bend differently as they pass through a lens, resulting in a distorted image. The distortion can be modeled using the distortion coefficients k1 and k2, which describe the amount of distortion present in the lens. The relationship between the original and distorted coordinates can be described using the equations r2=x2+y2, xdistorted=x(1+k1r2+k2r4), and ydistorted=y(1+k1r2+k2r4).
To apply radial distortion, follow these steps:
- Calculate the squared distance r2 from the optical center.
- Compute the distortion factor using the coefficients k1 and k2.
- Apply the distortion factor to the original coordinates.
This technique is widely used in camera calibration to correct for lens distortions and produce more accurate images.
Example:
radial_distort([0.5, 0.5], -0.1, 0.01)
[0.4688, 0.4688]
- First, compute r2=x2+y2=0.52+0.52=0.25+0.25=0.5.
- Then compute the distortion factor: 1+k1r2+k2r4=1+(−0.1)(0.5)+0.01(0.52)=1−0.05+0.01⋅0.25=0.9525.
- Apply this factor to each coordinate: xdistorted=0.5⋅0.9525=0.47625, ydistorted=0.5⋅0.9525=0.47625, which rounds (with the problem’s precision/rounding) to approximately [0.4688,0.4688].
Constraints:
- Input point is [x, y] (normalized coordinates)
- k1, k2 are distortion coefficients
- Return distorted [x', y'] rounded to 4 decimal places
Normalized image coordinates (x,y) come from the pinhole camera model: after dividing by depth and focal length, a 3D point projects to coordinates where the principal point is at (0,0) and units are in “radii from the optical center.” In this ideal model, straight 3D lines map to straight 2D lines and all rays pass through a single pinhole. Real lenses, especially wide-angle or fisheye lenses, deviate from this ideal and bend rays, causing radial distortion: points are displaced along the line from the image center, depending only on their distance r from the center.
Radial distortion is usually modeled by a polynomial in r2, where r2=x2+y2. The given model
xd=x(1+k1r2+k2r4),yd=y(1+k1r2+k2r4)multiplies both coordinates by the same radial scaling factor. Positive/negative values of k1,k2 yield barrel or pincushion effects (bulging out or in). In this problem, you already have normalized coordinates and known distortion coefficients (k1,k2), and the task is to apply the forward distortion formula.
1. Background Knowledge (key points)
- Radial symmetry: Distortion depends only on radius r=x2+y2 from the center, not on the angle; hence both x and y share the same multiplicative factor.
- Polynomial model: Truncate the series at r4:
- r2=x2+y2
- Scale factor s(r)=1+k1r2+k2r4
- Forward vs inverse distortion:
- Forward: given ideal (x,y), compute distorted (xd,yd) — this is what you’re doing.
- Inverse (undistortion): given (xd,yd), recover (x,y) — usually harder, often solved iteratively.
2. Algorithm / Approach
This is a direct formula evaluation for each point:
- Compute r2=x2+y2.
- Compute scale s=1+k1r2+k2r4.
- Multiply:
- xd=x⋅s
- yd=y⋅s
If you’re given many points (e.g., arrays), apply the same computation element-wise (vectorization if using NumPy, etc.).
3. Step-by-Step Strategy
Given inputs: x,y,k1,k2.
- Compute radius squared:
r2 = x*x + y*y
- Compute higher power:
r4 = r2 * r2
- Compute distortion factor:
s = 1 + k1 * r2 + k2 * r4
- Apply to coordinates:
x_distorted = x * s
y_distorted = y * s
- Return or store (x_\text{distorted}, y_\text{distorted}).
If processing many points:
- Loop or vectorize the above steps over all coordinates.
4. Common Pitfalls
- Mixing squared and non-squared radius: Use r2=x2+y2, then r4=(r2)2; do not accidentally use r instead of r2.
- Forgetting normalization assumption: This formula assumes coordinates are already normalized (principal point at origin and scaled by focal length). On raw pixel coordinates you’d first need to normalize, but for this problem you can skip that.
- Numeric overflow on large radii: Very large ∣x∣,∣y∣ can make r4 huge; in typical normalized coordinates r is small (≈within a few units), so this is usually safe.
- Sign of coefficients: Wrong sign of k1 or k2 flips barrel/pincushion effect; follow the problem’s given values.
5. Time & Space Complexity
Assuming you process N points:
- Time complexity:
- O(N), since each point requires a constant number of arithmetic operations.
- Space complexity:
- O(N) if you store all distorted points in a new array.
- O(1) extra space if you overwrite the input coordinates in-place.