Skip to content

calibrateCamera does not enforce lens model constraints #15992

Closed

Description

@midjji

calibrateCamera using k1, k2, k3 should enforce that k1r^2 + k2r^4 +k3*r^6 must be either monotonically increasing or monotonically decreasing as this is a constraint of real lens, or atleast all real lenses for which this (Brown) lens model is used. A marginally harder constraint which could be used instead is that they should all have the same sign.

Current state: this not enforced by the optimizer.

The most common case during which this constraint is violated in practice is when the chessboard observations are not well spread in the image. Note that this is the source of many a failed calibration. The ring pattern, or reflections, which it creates near the edges of the sampled pixels will be familiar to many when the full space where there are pixels are computed as undistortion. In particular this type of failure will fool most people as the reprojection error will be low, and the undistortion will appear reasonable in the center of the image.

Because the optimizer does not support mixed integer inequality constraints, and because such an optimizer will not fix the problem of the poor sampling anyways I propose to add a notice to the calibrateCamera docs which describes the problem. pull requiest incoming.

Note that this problem is so common that the opencv documentation explicity states that k>0 is (typically) barrel distortion. (This comment is explicitly there because people thought failed calibrations where actually successful and wrote the error into the documentation.)

Note there are several more unfullfilled constraints on k4:6 and tangential parameters. The discussion above assumes they are set to zero. Otherwize these more complicated constraints must be fullfilled aswell.

Activity

  1. catree commented on Dec 15, 2019

    @catree
    Contributor

    If I understand correctly, when the calibration procedure is done "badly", this can produce this kind of undistortion images?

    Mathematically speaking, this can be checked by verifying that the estimated k1r^2 + k2r^4 + k3*r^6 function is monotone?


    Since it is a common error, ideally a note/warning should be added wherever the camera calibration topic appears in the doc:

    Ideally, an example image of the produced "artifact" should be added.
    If it is feasible easily, why not add a check at the end of the calibration.cpp sample so as to warn the user when the estimated parameters could produce this kind of issue?


    Also, a more general update, improvement of the camera calibration tutorials should be considered:

    A set of guidelines for correct camera calibration would be very useful.

    So far, common source of errors I believe are encountered by beginners:

    • not using a flat chessboard pattern, often a paper sheet is used and not glued correctly
    • blurry images
    • too few images, too many images
    • acquired images are too similar, does not cover the whole image space, ...

    So what camera calibration procedure should be adopted? So far I have found the following information:

    Or some advice similar to:

    Also, which calibration pattern should be preferred between the asymmetric chessboard and the asymmetric circle grid pattern?


    Related #14596

  2. midjji commented on Dec 16, 2019

    @midjji
    ContributorAuthor

    Camera calibrate can(theoretically) always produce this kind of error, regardless of how well you perform the calibration, but its less likely if you do it well.

    Yes, thats a fairly typical example, but there are other variants which are more subtle, a single sharp ring distortion at one position e.g.

    If and only if your distortion model only contains the k1:3 and they are monotone, this type of distortion/error does not occur, the real criteria is that lenses are bijective for rays, which can fail to be fullfilled if you have a combination of e.g. k4:6 or tangential etc. The full function beeing monotonic along each r is necc and sufficient though. (havent looked at the s parameters, but odds are they should never be used anyways)

    The most common error people make is holding, either the calibration pattern or the camera, by hand. Meaning it moves while you capture the image. Motion blur causes critical failure, depth of field blur is less of a problem in most cases, and if it is a big problem, you need a different camera model.
    Having poor lighting conditions(office lighting) is another, which worsens the above.
    The second most common error is to use more parameters than you should, most use 4+8 params because its default. but if your lense is symetric(it is, or toss it) and your pixels square(it is, or you got something very rare which is mentioned in its docs), then fx==fy, minor deviation is just that the optimal parameter set will not minimize the observation error, large deviation is more likely to be calibration failure than assymetric lense or uneven sensor pixels. Similarily, tangential distortion most likely means your camera sensor is improperly mounted or lose, meaning if you actually have this, shake the camera, calibrate again, and see if it changed, if it did, toss the camera. Finally, use e.g. akikake information or something similar to determine which of the k_i you should use, odds are k1, k2 are sufficient.

    Both work fine, the circle patterns can be slightly more robust/easier to use when chessboard does not work, but gives lower final accuracy, but there are line/gradient based methods which give an order or two of magnitude of improvment. There are no good commercial or opensource implementations of the latter though, and they are a pain in the ass to use in practice.

    I have no intention of writing a guide, but I dont want the opencv doc to try and hide the problem as it has been.

  3. catree commented on Dec 19, 2019

    @catree
    Contributor

    Thanks for the valuable information.

    I think I will open another thread for suggestions about camera calibration tutorial improvement. I put here some information I found interesting and that could help other people.


    Possible issue with circle grid pattern in the OpenCV implementation is that it seems it does not take into account the perspective bias when locating the circle/ellipse centroid, #7312

    On a related topic: #15577


    akikake information

    Typo, I don't know the method but I think it should be Akaike information criterion

    there are line/gradient based methods which give an order or two of magnitude of improvment.

    Feel free to add references.

    Are there some good references about the camera calibration process? From the pure theoretical point of view, I guess that the Zhang's paper or Heikkila one are good references.


    Some new approaches to guide the user during the camera calibration process:

  4. catree commented on Mar 29, 2020

    @catree
    Contributor

    Found this repository Accurate geometric camera calibration:

    Thomas Schöps, Viktor Larsson, Marc Pollefeys, Torsten Sattler, "Why Having 10,000 Parameters in Your Camera Model is Better Than Twelve", arXiv 2019

    After founding this repository, Bundle Adjusted Direct RGB-D SLAM:

    Thomas Schöps, Torsten Sattler, Marc Pollefeys, "BAD SLAM: Bundle Adjusted Direct RGB-D SLAM", CVPR 2019.

  5. midjji commented on Mar 29, 2020

    @midjji
    ContributorAuthor
  6. VladimirVLF commented on Dec 29, 2020

    @VladimirVLF

    I have been dealing with this problem in practice quite a lot recently, with many perfect calibration snapshots and "bad" distortion results after the calibration, especially near the corners - the farthest points from the optical axis. Now I can see I am not the only one.

    The requirement of the distortion transformation being bijective sounds reasonable and quite general. As I understand, when the distortion transformation is not bijective, its Jacobian becomes zero, and we start seeing the mentioned reflection effects, etc. The distortion Jacobian is not difficult to express analytically from the distortion formulae. Would it be an idea to constrain the optimization algorithm with a condition of non-zero Jacobian within the image dimension domain? Maybe someone has already tried that?

  7. midjji commented on Dec 30, 2020

    @midjji
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

category: calib3dcategory: documentation

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions