Skip to content

Fix lensfun-based distortion correction - #1681

Open
duddel wants to merge 1 commit into
CyberTimon:mainfrom
duddel:ptlens_fix
Open

Fix lensfun-based distortion correction#1681
duddel wants to merge 1 commit into
CyberTimon:mainfrom
duddel:ptlens_fix

Conversation

@duddel

@duddel duddel commented Sep 4, 2026

Copy link
Copy Markdown

Description

Applying lensfun data to my lens (Nikkor Z 24-70mm f/4 S) yields, what looks like, overcompensation, especially in image center.

For comparison, I checked darktable 5.6.0 against RapidRAW 1.6.2. Both use the exact same lensfun value for my lens, but the first tool generates matching results to the in-camera jpeg processing. I think darktable uses the lensfun reference implementation.

After digging into the code, I found 3 issues.

(1)
lens_dist_amt has an arbitrary scale of 2.5. While this makes sense to increase sensitivity in certain cases, here it does not. The lens_distortion_amount from GUI is scaled to [0, 2], default 1.0 (slider value 100). To get the vanilla correction factor, the slider value would need to be 40.

(2)
The half_diagonal is used for the normalized radius (ptlens, poly3, poly5), but it must be the "half short edge of the image". Vignetting still uses the half diagonal.

(3)
The poly3 model defines only k1, which boils down to ptlens model with a=0, b=k1, c=0. (Not relevant for my observation, but still an issue)

I hope I found all occurrences where this is relevant.

This PR should fix most of the issue, but we might also need to consider image aspect ratio vs. calibration aspect ratio.

Also, not sure if TCA also needs a treatment.

References: lensfun code and docs.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Performance improvement
  • Code refactoring
  • Documentation update
  • UI/UX improvement
  • Build/CI or Dependency update

Changes Made

(1)
Removed the 2.5 scaling factor

(2)
Using "half short edge of the image" now

(3)
added distinct poly3 case in extract_dist_params()

Screenshots/Videos

Can supply example image if required.

Testing

  • These changes were tested locally by a human and confirmed to work.
  • I haven't added any automated tests to the code because the codebase currently lacks a test suite.

Test Configuration:

  • OS: Ubuntu 24.04
  • Hardware: Ryzen 5000 series, Nvidia RTX 3060

Checklist

  • My code follows the project's code style
  • I haven't added unnecessary AI-generated code comments
  • My changes generate no new warnings or errors

Additional Notes

AI Disclaimer:

Please state the involvement of AI in this PR:

  • This PR is created by an AI agent
  • This PR is mostly AI-generated but edited/merged together by a human
  • This PR was handwritten with AI assistance (spell check, logic suggestions, error resolving)
  • This PR contains only blood, sweat, and coffee (AI-free)

@duddel
duddel requested a review from CyberTimon as a code owner September 4, 2026 14:25
@duddel

duddel commented Sep 4, 2026

Copy link
Copy Markdown
Author

Sounds similar to #990.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant