John Rostron Posted May 3, 2020 Posted May 3, 2020 In a recent posting, @MaximusNZhad been creating panoramas, but had found that the upper and lower parts of the image were compressed with respect to the more central parts. He posted a message in my thread on Wrapping a label round a bottle since this also involved such stretching and squeezing. I posed a temporary solution to his/her problem, but I present here a more general solution. This is based on the S-curve, familiar to those using the Curves tool. The default shape for the curve is a straight line between the origin and the (w, h) position at the top right. The parameters of the macro will distort this linear relationship. I will elaborate on the mathematical aspects in a later posing in this thread. Consider a simple grid: By varying a parameter a, the image can be distorted horizontally. Values of a larger than 0.5 will stretch the left and right whilst compressing the centre; values less than half will stretch the centre whilst stretching the left and right. See the image below: By varying a parameter b, the image can be distorted vertically. Values of b larger than 0.5 will stretch the top and bottom whilst compressing the centre; values less than half will stretch the centre whilst stretching the top and bottom. See the image below: Here is the macro as a single macro and as a library: S-curve.afmacro S-curve.afmacros John Paul Mudditt, Dan C, MaximusNZ and 3 others 3 3 Quote Windows 11, Affinity Photo 2.4.2 Designer 2.4.2 and Publisher 2.4.2 (mainly Photo). CPU: Intel Core i5 8500 @ 3.00GHz. RAM: 32.0GB DDR4 @ 1063MHz, Graphics: 2047MB NVIDIA GeForce GTX 1050
John Rostron Posted May 3, 2020 Author Posted May 3, 2020 Consider the original chequerboard above. If we map the output position against the input position, we get a straight line relationship, like this (taken from the Curves Adjustment, simply because it is of similar shape): We can modify this curve to stretch the ends and flatten the middle by adjusting two positions. (This gives the S-curve in the title): Note that the curve crosses the straight line at the half-way position. There is a maximum adjustment that could be applied: The equations that create such a curve are: x=x+x*(x-w/2)*(x-w)*4*(0.5-a)/w/w y=y+y*(y-h/2)*(y-h)*4*(0.5-b)/h/h The expression x*(x-w/2)*(x-w)/w/w is a cubic equation chosen so that the roots (zero values) are at zero, half and one. The a and b parameters allow us to modify the horizontal and vertical axes respectively. The parameters are included as (0.5-a) and (0.5-b). If a parameter is set to 0.5 (the default), then this expression becomes zero and the equation has no effect. if it exceeds 0.5 then it stretches/squeezes one way and if it is less than 0.5 it does it the other way. The constant 4 scales the expression so that the curve never exceeds the maximum shape shown in the third image above. I did wonder whether this macro could correct mild versions of pincushion or barrel distortion. John Paul Mudditt 1 Quote Windows 11, Affinity Photo 2.4.2 Designer 2.4.2 and Publisher 2.4.2 (mainly Photo). CPU: Intel Core i5 8500 @ 3.00GHz. RAM: 32.0GB DDR4 @ 1063MHz, Graphics: 2047MB NVIDIA GeForce GTX 1050
John Rostron Posted May 5, 2020 Author Posted May 5, 2020 On 5/3/2020 at 11:59 AM, John Rostron said: I did wonder whether this macro could correct mild versions of pincushion or barrel distortion It does after a fashion, but I thought that applying the equation to the radius in a Polar Co-ordinates might be better. Here is the resulting equation: r=r+r*(r-b*sqrt(h*w)/2)*(r-c*sqrt(h*w))*4*(0.5-a)/w/h Like the Cartesian macro, the a parameter affects the magnitude and direction of the effect. The other two parameters affect the shape of this curve and can be used to tweak the final result. This version can fix pincushion or barrel distortion better than the Cartesian macro, but still leaves some room for improvement. Here is the macro: S-curve Radial.afmacro and here is the macro library above augmented with this radial macro. S-curve.afmacros A problem that I have discovered is that when running this filter as a macro it is not possible to select the origin of the polar co-ordinates (which defaults to the centre of the image). When I run this as a simple Filter > Distort > Equations, a cross-hair appears on the image, allowing me to select the origin. John GDPR-415734 and v_kyr 2 Quote Windows 11, Affinity Photo 2.4.2 Designer 2.4.2 and Publisher 2.4.2 (mainly Photo). CPU: Intel Core i5 8500 @ 3.00GHz. RAM: 32.0GB DDR4 @ 1063MHz, Graphics: 2047MB NVIDIA GeForce GTX 1050
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.