lenscorrection and vignette filters
Hi all, I found a bug in the documentation of the lenscorrection filter: https://www.ffmpeg.org/ffmpeg-all.html#lenscorrection It's written there "0.5 means no correction" for the coefficients k1 and k2. This can't be right, or the formula is wrong. It's obvious that the formula makes no correction if k1=k2=0. For positive k1 and k2, r_tgt is always smaller than r_src. That means in the target image there are undefined pixels at the edges. Which color is used to fill these pixels? There should be a "fillcolor" parameter as in the rotate filter. Other question: Which formula is used for the vignette filter? Thanks, Michael
I found a bug in the documentation of the lenscorrection filter: https://www.ffmpeg.org/ffmpeg-all.html#lenscorrection
It's written there "0.5 means no correction" for the coefficients k1 and k2. This can't be right, or the formula is wrong. It's obvious that the formula makes no correction if k1=k2=0.
The remarks "0.5 means no correction" in the documentation are wrong. This can easily be tested: ffmpeg -i input.png -vf lenscorrection=k1=0.5:k2=0.5 output.png It's clearly visible that the output is not the same as the input. ffmpeg -i input.png -vf lenscorrection=k1=0:k2=0 output.png Now the output is the same as the input. It is clear that 0 means no correction. But unfortunately this isn't the only error in the documentation. I haven't yet found out which formula is actually used in the lenscorrection filter, but one thing is absolutely sure: It's not the formula from the documentation. I've done some tests. The input picture is 5472x3648. Half of the diagonal is 3288. With k1=0.1, half of the diagonal becomes 3030. With k1=0.2, half of the diagonal becomes 2856. With k1=0.3, half of the diagonal becomes 2723. With k2=0.1, half of the diagonal becomes 3058. None of these results can be explained by the documented formula. Michael
On Tue, Feb 23, 2016 at 10:00:47 +0100, Michael Koch wrote:
The remarks "0.5 means no correction" in the documentation are wrong. This can easily be tested:
I know nothing about lens correction formulas, I trust you on this. :-)
But unfortunately this isn't the only error in the documentation. I haven't yet found out which formula is actually used in the lenscorrection filter, but one thing is absolutely sure: It's not the formula from the documentation.
I can't help you much, but you are free to look at the source code, and see whether you can see the formula from it: https://github.com/FFmpeg/FFmpeg/blob/master/libavfilter/vf_lenscorrection.c I think filter_frame() calls filter_slice(), but don't quote me on that. Cheers, Moritz
I can't help you much, but you are free to look at the source code, and see whether you can see the formula from it:
https://github.com/FFmpeg/FFmpeg/blob/master/libavfilter/vf_lenscorrection.c
I did already try that, but there are no comments in the source code, and it's really difficult to understand how it works. Reverse engineering from the results seems easier for me. Michael
Am 23.02.2016 um 10:00 schrieb Michael Koch:
I found a bug in the documentation of the lenscorrection filter: https://www.ffmpeg.org/ffmpeg-all.html#lenscorrection
It's written there "0.5 means no correction" for the coefficients k1 and k2. This can't be right, or the formula is wrong. It's obvious that the formula makes no correction if k1=k2=0.
The remarks "0.5 means no correction" in the documentation are wrong. This can easily be tested:
ffmpeg -i input.png -vf lenscorrection=k1=0.5:k2=0.5 output.png It's clearly visible that the output is not the same as the input.
ffmpeg -i input.png -vf lenscorrection=k1=0:k2=0 output.png Now the output is the same as the input. It is clear that 0 means no correction.
But unfortunately this isn't the only error in the documentation. I haven't yet found out which formula is actually used in the lenscorrection filter, but one thing is absolutely sure: It's not the formula from the documentation.
After spending many hours with this problem, finally I figured out what's going on. This is the formula from the documentation: r_src = r_tgt * (1 + k1 * (r_tgt / r_0)^2 + k2 * (r_tgt / r_0)^4) The formula is correct, but it must be clarified that it describes the behaviour of the lens. It does not describe the behaviour of the lenscorrection filter. r_src is the radial coordinate in object space. r_tgt is the radial coordinate in the image, which was taken with a distorted lens. The behaviour of the lenscorrection filter can be described by the inverse of the above function. However the inverse function is difficult to derive. For the simplified case k2=0 I found a solution: r_tgt = 2 * sqrt(1 / (3 * k1)) * sinh(arcsinh(r_src / (2 * k1 * sqrt(1 / (3 * k1)) ^ 3)) / 3) Michael
Am 23.02.2016 um 18:51 schrieb Michael Koch:
r_src is the radial coordinate in object space. r_tgt is the radial coordinate in the image, which was taken with a distorted lens.
Sorry, I confused these two. This is correct: r_tgt is the radial coordinate in object space. r_src is the radial coordinate in the image, which was taken with a distorted lens. this was wrong. Michael
Michael Koch <astroelectronic <at> t-online.de> writes:
The formula is correct, but it must be clarified that it describes the behaviour of the lens. It does not describe the behaviour of the lenscorrection filter.
Please consider sending a patch that updates the documentation. Carl Eugen
Please consider sending a patch that updates the documentation.
I recommend to make the following changes in chapter 38.73.1: The filter accepts the following options: cx Relative x-coordinate of the center of the distortion. This value has a range [0,1] and is expressed as fractions of the image width. The default value is 0.5. cy Relative y-coordinate of the center of the distortion. This value has a range [0,1] and is expressed as fractions of the image height. The default value is 0.5. k1 Coefficient of the quadratic correction term. 0 means no correction. The default value is 0. A positive value shifts the border towards the center, and the new border of the output image is filled with an unpredictable color. A negative value shifts the border away from the center, so that some content is lost in the output image. k2 Coefficient of the double quadratic correction term. 0 means no correction. The default value is 0. This is the formula that describes the distortion of the lens: r_img = r_src * (1 + k1 * (r_src / r_0)^2 + k2 * (r_src / r_0)^4) where r_img is the distance from the center of distortion in the distorted image, r_src is the distance from the center of distortion in object space and r_0 is halve of the image diagonal in object space. The lenscorrection filter does apply the inverse of the above formula. Michael
participants (3)
-
Carl Eugen Hoyos -
Michael Koch -
Moritz Barsnick