Construct Intrinsic Matrix
Construct a camera intrinsic matrix from its parameters.
The camera intrinsic matrix K describes the internal parameters of a camera that transform 3D camera coordinates to 2D pixel coordinates:
K=​fx​00​sfy​0​cx​cy​1​​
where:
- fx​,fy​ are the focal lengths in pixels (horizontal and vertical)
- (cx​,cy​) is the principal point (usually near image center)
- s is the skew coefficient (usually 0 for modern cameras)
The focal length in pixels is computed as: fpixels​=fmm​×sensor_widthimage_width​
For a standard camera with square pixels, fx​=fy​ and s=0.
Example:
intrinsic_matrix(1000, 1000, 320, 240, 0)
[[1000, 0, 320], [0, 1000, 240], [0, 0, 1]]
- Building intrinsic matrix with fx=fy=1000, center at (320, 240):
- Row 0: [fx, skew, cx] = [1000, 0, 320]
- Row 1: [0, fy, cy] = [0, 1000, 240]
- Row 2: [0, 0, 1] = [0, 0, 1]
- Result: standard pinhole camera matrix with square pixels.
Constraints:
- fx, fy: focal lengths in pixels (positive floats)
- cx, cy: principal point coordinates in pixels
- skew: skew coefficient (default 0)
- Return 3x3 matrix as nested list
More from CV: Structure from Motion and SLAM
The task is to compute and assemble the 3×3 intrinsic matrix K from given camera parameters; conceptually this is a straightforward parameter-to-matrix mapping problem rather than an algorithmically complex one.
1. Background Knowledge
In pinhole camera geometry, a 3D point in camera coordinates (Xc​,Yc​,Zc​) is projected to image (pixel) coordinates (u,v) via a projection equation of the form:
λ​uv1​​=K​Xc​Yc​Zc​​​,where K is the intrinsic matrix and λ is a scale factor. K encodes how real-world distances (in meters or millimeters) map to pixel units on the sensor, as well as where the principal point (optical center) lies in the image.
The intrinsic matrix is typically written as:
K=​fx​00​sfy​0​cx​cy​1​​,where:
- fx​,fy​: focal length in pixels along x and y,
- (cx​,cy​): principal point coordinates in pixels,
- s: skew (non-orthogonality between image axes, usually 0 for modern cameras).
To get focal length in pixels from focal length in millimeters, you use the relationship between sensor size and image resolution:
fpixels​=fmm​×sensor_width_mmimage_width_pixels​(and similarly for height if needed).
2. Algorithm / Approach
This problem is essentially:
- Convert focal length units (mm → pixels) using sensor size and image size.
- Assign parameters into the 3×3 matrix in the correct positions.
- Use known simplifications for standard cameras:
- Square pixels: fx​=fy​,
- Zero skew: s=0,
- Principal point near image center: often (cx​,cy​)=(\frac{W}{2},\frac{H}{2}) if not otherwise specified.
The implementation pattern is: compute scalar parameters → construct a fixed‑shape matrix → return it.
3. Step-by-Step Strategy
Assuming you are given:
- Focal length fmm​ in millimeters,
- Sensor width (and optionally height) in millimeters,
- Image width W and height H in pixels,
- Principal point (cx​,cy​) (or you assume center),
- Skew s (often 0),
you can proceed as:
- Compute pixel focal length(s)
- If square pixels:
f_pixels = f_mm * (image_width_pixels / sensor_width_mm)
fx = fy = f_pixels
- If non-square pixels (if problem gives separate sensor height or pixel aspect ratio), compute:
fx = f_mm * (image_width_pixels / sensor_width_mm)
fy = f_mm * (image_height_pixels / sensor_height_mm)
- Determine principal point
- If given explicitly, use those values.
- If not, use image center approximation:
cx = image_width_pixels / 2.0
cy = image_height_pixels / 2.0
- Set skew
- For most problems: s = 0.0.
- Assemble matrix In code-like form:
K = [[fx, s, cx],
[0, fy, cy],
[0, 0, 1]]
- Return or print the matrix in the required format (e.g., NumPy array).
4. Common Pitfalls
-
Mixing units: Make sure all focal lengths are converted to pixels before filling K. Do not directly plug fmm​ into the matrix.
-
Using wrong image dimension: When computing fpixels​, match sensor width with image width, and sensor height with image height.
-
Integer vs float division: In languages like Python 2 or some other languages, integer division can truncate. Ensure you use float division to get precise fx​,fy​.
-
Principal point off by 0.5: Some conventions use (W/2,H/2), others (W/2−0.5,H/2−0.5). Follow the convention used by your platform or tests; in many educational problems, W/2,H/2 is expected.
-
Matrix order: Do not swap rows or columns. The matrix must match the canonical form:
5. Time & Space Complexity
-
Time complexity: All operations are basic arithmetic and constant-size matrix construction, so O(1).
-
Space complexity: You store a fixed 3×3 matrix and a few scalars, so O(1) additional space.