Skip to content

fix(calib3d): read distCoeffs in standard order in initWideAngleProjMap 🤖🤖🤖 - #30797

Merged
abhishek-gola merged 1 commit into
opencv:4.xfrom
SIDDARTHAREDDY8:oss-bhai-2026-11-12-opencv-30744
Nov 15, 2026
Merged

abhishek-gola merged 1 commit into
opencv:4.xfrom
SIDDARTHAREDDY8:oss-bhai-2026-11-12-opencv-30744

Conversation

@SIDDARTHAREDDY8

@SIDDARTHAREDDY8 SIDDARTHAREDDY8 commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #30744

initWideAngleProjMap unpacked distCoeffs as (k1, k2, k3, p1, p2, ...)
while the documented coefficient order, and the one initUndistortRectifyMap
uses, is (k1, k2, p1, p2, k3, ...). With a nonzero tangential coefficient
the projection map came out wrong. This reorders the unpacking to the
standard model and adds a regression test.

Retargeted to 4.x per review (the function lives in modules/calib3d there).
Also fixed two further issues in the same function from the review: the
radial factor now uses the rational model
kr = (1 + k1*r2 + k2*r4 + k3*r6)/(1 + k4*r2 + k5*r4 + k6*r6) (was off by
about 14 px with 8 coefficients), and the principal point is now read from
the converted camera matrix instead of scenter (which was read before
the conversion, so cx/cy were always 0 and the map was shifted by about
400 px for a real camera matrix).

Repro from the issue (distCoeffs [0, 0, 0.1, 0, 0], fx=fy=500,
PROJ_SPHERICAL_EQRECT, 640x480, pixel (450, 350)):

  • before: u=212.139, v=197.349
  • after: u=227.979, v=228.751 (matches the expected values in the issue)

New test Calib3d_initWideAngleProjMap.issue_30744 in
modules/calib3d/test/test_undistort.cpp (next to regression_14467) uses
a real principal point and 8 coefficients and round-trips the map through
undistortPoints; it fails with each of the three bugs present and passes
with the fix. The 15 undistort tests pass.

Note: this PR was prepared with AI assistance; the fix and the test were
verified by building OpenCV from source and running the issue's repro and
the new regression test locally.

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code
    under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra
    repository, if applicable
  • The feature is well documented and sample code can be built with the
    project CMake

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

The coefficient unpacking now matches the documented calib3d order and the undistortPoints call used earlier in the same function. The focused nonzero-p1 regression fails under the old indexing and pins the corrected map values. I found no actionable issue; LGTM pending exact-head CI.

@sanjayk9
sanjayk9 self-requested a review November 13, 2026 07:32
@sanjayk9 sanjayk9 added the backport is needed Label for maintainers. Authors of PR can ignore this label Nov 13, 2026
@sanjayk9

Copy link
Copy Markdown
Contributor

This bug is on 4.x too (calib3d/src/undistort.dispatch.cpp:693), and bug fixes go to 4.x, which is merged into 5.x regularly. Please retarget to 4.x.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 8+ coefficients the 1 must be in the numerator: kr = (1 + ((k3*r2 + k2)*r2 + k1)r2)/(1 + ((k6r2 + k5)*r2 + k4)*r2), as in initUndistortRectifyMap and undistortPoints. As written, the map is off by about 14 px.

Mat mapxy(dsize, CV_32FC2);
double k1 = k[0], k2 = k[1], k3 = k[2], p1 = k[3], p2 = k[4], k4 = k[5], k5 = k[6], k6 = k[7], s1 = k[8], s2 = k[9], s3 = k[10], s4 = k[11];
double k1 = k[0], k2 = k[1], p1 = k[2], p2 = k[3], k3 = k[4], k4 = k[5], k5 = k[6], k6 = k[7], s1 = k[8], s2 = k[9], s3 = k[10], s4 = k[11];
double fx = cameraMatrix.at<double>(0,0), fy = cameraMatrix.at<double>(1,1), cx = scenter.x, cy = scenter.y;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scenter is read before cameraMatrix is filled, so cx/cy are always 0 and the map is shifted by about 400 px for a real camera matrix. Please read them from cameraMatrix after the conversion and drop scenter.

Comment thread modules/imgproc/test/test_imgwarp.cpp Outdated
map1, map2, PROJ_SPHERICAL_EQRECT, 0.0);

EXPECT_NEAR(map1.at<float>(350, 450), 227.979f, 1e-2);
EXPECT_NEAR(map2.at<float>(350, 450), 228.751f, 1e-2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test passes with both bugs above still present, since only p1 is set and cx = cy = 0. Please round-trip the map through undistortPoints with a real principal point and 8 coefficients, next to regression_14467 in test_undistort.cpp.

initWideAngleProjMap unpacked the distortion coefficients as
(k1, k2, k3, p1, p2, ...) while the documented order, and the one used by
initUndistortRectifyMap, is (k1, k2, p1, p2, k3, ...). With a nonzero p1
the map came out wrong (e.g. dist=[0,0,0.1,0,0] gave u=212.139, v=197.349
instead of u=227.979, v=228.751). Reorder the unpacking to match the
standard model.

Also fix two further issues in the same function: the radial factor used
kr = 1 + (num-1)/(den) instead of the rational model
kr = (1 + k1*r2 + k2*r4 + k3*r6)/(1 + k4*r2 + k5*r4 + k6*r6), which is off
by about 14 px with 8 coefficients, and the principal point was read
from scenter before the camera matrix was converted, so cx/cy were
always 0 and the map was shifted by about 400 px for a real camera
matrix. Read cx/cy from the converted camera matrix and drop scenter.

Add a regression test next to regression_14467: with a real principal
point and 8 coefficients it round-trips the map through undistortPoints
and checks the result against the spherical projection.

Fixes opencv#30744

Signed-off-by: SIDDARTHA REDDY <[email protected]>
Signed-off-by: Siddartha Reddy Chinthala <[email protected]>
@SIDDARTHAREDDY8
SIDDARTHAREDDY8 force-pushed the oss-bhai-2026-11-12-opencv-30744 branch from 434ff3b to be0972f Compare November 13, 2026 20:33
@SIDDARTHAREDDY8
SIDDARTHAREDDY8 changed the base branch from 5.x to 4.x November 13, 2026 20:33
@SIDDARTHAREDDY8 SIDDARTHAREDDY8 changed the title fix(imgproc): read distCoeffs in standard order in initWideAngleProjMap 🤖🤖🤖 fix(calib3d): read distCoeffs in standard order in initWideAngleProjMap 🤖🤖🤖 Nov 13, 2026
@SIDDARTHAREDDY8

Copy link
Copy Markdown
Author

Addressed all three points in be0972f (retargeted to 4.x): the unpacking now reads (k1, k2, p1, p2, k3, ...), kr uses the rational model (1 + k1r2 + k2r4 + k3r6)/(1 + k4r2 + k5r4 + k6r6), and cx/cy are read from the converted camera matrix with scenter dropped. The new test Calib3d_initWideAngleProjMap.issue_30744 in test_undistort.cpp uses a real principal point and 8 coefficients and round-trips the map through undistortPoints; it fails with each bug present and passes with the fix. Repro on 4.x gives u=227.979, v=228.751 as expected, and the 15 undistort tests pass.

@codex-abhishek-gola codex-abhishek-gola Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status: 🟡 Yellow

The update resolves the three review findings: it uses the standard distortion-coefficient order, applies the rational radial model, and reads the principal point after converting the camera matrix. The replacement regression now round-trips a map with a real principal point and eight coefficients, so each corrected path is exercised. I found no remaining actionable issue; exact-head CI is still in progress.

@abhishek-gola
abhishek-gola merged commit 06e251e into opencv:4.x Nov 15, 2026
32 checks passed
@abhishek-gola abhishek-gola self-assigned this Nov 15, 2026
@abhishek-gola abhishek-gola added this to the 4.15.0 milestone Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport is needed Label for maintainers. Authors of PR can ignore this bug category: calib3d

Projects

None yet

Development

Successfully merging this pull request may close these issues.

initWideAngleProjMap reads distCoeffs in a different order than the rest of calib3d

4 participants