Distortion parameters "a,b,c" to "k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]]" (OpenCV)

789 views
Skip to first unread message

Finfa811

unread,
Jun 29, 2016, 8:22:53 AM6/29/16
to hugin and other free panoramic software
Hi there,


I've calibrated my camera and estimated the a,b,c lens distortion parameters, and now I would like to apply some OpenCV code to my images.

How do I convert my a,b,c parameters to OpenCV style "k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]]"?


Thanks.

Erik Krause

unread,
Jun 30, 2016, 4:38:51 PM6/30/16
to hugi...@googlegroups.com
Am 29.06.2016 um 14:22 schrieb Finfa811:

> How do I convert my a,b,c parameters to OpenCV style "k_1, k_2, p_1, p_2[,
> k_3[, k_4, k_5, k_6]]"?

The a,b,c parameters define a polynomial curve which describes the
distortion of the image. See
http://wiki.panotools.org/Lens_correction_model for details.

OpenCV apparently uses a different polynomial. As far as I know there is
no easy way to transform one polynomial into another. Your best bet is
probably to use hugin to correct for lens distortion...

--
Erik Krause
http://www.erik-krause.de

Finfa811

unread,
Jul 1, 2016, 2:34:08 AM7/1/16
to hugin and other free panoramic software
I've found a solution in the following link:

  • a = k2 * (width/2)3
  • b = 0
  • c = k1* (width/2)
  • d = 1
The minimum input that OpenCV uses for its polynomial is k1k2p1p2 (4 parameters). In my case, just the parameter c was not zero (other than d), so only k1 will be used. The result is pretty much the same.

Thank you for your help anyway.

Torsten Bronger

unread,
Jul 1, 2016, 3:26:13 AM7/1/16
to hugi...@googlegroups.com
Hallöchen!

Finfa811 writes:

> I've found a solution in the following link:
>
> *http://www.panotools.org/dersch/barrel/barrel.html*
>
> - a = k2 * (width/2)3
> - b = 0
> - c = k1* (width/2)
> - d = 1
>
> The minimum input that OpenCV uses for its polynomial is k1k2p1p2
> (4 parameters). In my case, just the parameter c was not zero
> (other than d), so only k1 will be used. The result is pretty much
> the same.

I'm suprised that this works.

First, the only coefficients of the same power in r in both models
are b and k1. Both apply to r^3. OpenCV's k2 and k3 apply to r^5
and r^7, respectively, both of which don't have counterparts in
Hugin.

Secondly, d in Hugin is 1-a-b-c rather than 1. The difference is
noticable for real-world lenses in my experience.

I think the only way to make both compatible is to use only b in
Hugin for calibration, take the different coordinate system into
account (i.e. scaling factor to the power of 3), and take d!=1 into
account by dividing the resulting k1 by (1-b)^3.

(Beware that I haven't checked the calculation, and one can easily
make mistakes in these things.)

Tschö,
Torsten.

--
Torsten Bronger Jabber ID: torsten...@jabber.rwth-aachen.de

Bruno Postle

unread,
Jul 1, 2016, 4:51:40 AM7/1/16
to hugi...@googlegroups.com
Yes, there is no direct conversion possible unless you limit the
parameters. However given the two formulas, this is a curve fitting
problem - it ought to be possible to get a very good approximation
with a spreadsheet solver.

--
Bruno

bugbear

unread,
Jul 1, 2016, 4:55:37 AM7/1/16
to hugi...@googlegroups.com
'Bruno Postle' via hugin and other free panoramic software wrote:
> Yes, there is no direct conversion possible unless you limit the
> parameters. However given the two formulas, this is a curve fitting
> problem - it ought to be possible to get a very good approximation
> with a spreadsheet solver.
>

If they are both good and useful models of the same
distortions, they should indeed map onto each other,
"somehow", at least for realistic and/or common cases.

BugBear

Finfa811

unread,
Jul 1, 2016, 5:04:01 AM7/1/16
to hugin and other free panoramic software, bro...@physik.rwth-aachen.de
In my case it is working, maybe it's just coincidence because I've checked the polynomials and you are right. Probably the ccd width value is the correct one that returns a valid solution in my case.

In any case it would be nice to try with different lenses. 

Torsten Bronger

unread,
Jul 1, 2016, 5:25:20 AM7/1/16
to hugi...@googlegroups.com
Hallöchen!

'Bruno Postle' via hugin and other free panoramic software writes:

> Yes, there is no direct conversion possible unless you limit the
> parameters. However given the two formulas, this is a curve fitting
> problem - it ought to be possible to get a very good approximation
> with a spreadsheet solver.

In case of non-matching powers, I failed to achieve this. I tried
to convert Adobe's ACM model (Lightroom) to Hugin by re-sampling the
Adobe polynomial. The results were unusable. I suspected this was
due to the fact that two terms with different powers are orthogonal.

However, if all powers are availiable in the destination model, you
have a fighting chance, even to convert exactly.

We recently had a similar problem on the Lensfun mailing list.
There, a general a,b,c,d model should be mapped to Hugin's
a,b,c,(1-a-b-c) model. This works exactly because you have scaling
as one degree of freedom. This way, one coefficient (no matter
which!) can be set to an arbitrary non-zero value. The conversion
is not easy, though.
Reply all
Reply to author
Forward
0 new messages