Jump to content
You must now use your email address to sign in [click for more info] ×

S-curve macro to stretch and squeeze


John Rostron

Recommended Posts

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:

Original.png.7ed7df44a02d3b78990d986b449e5ed8.png

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:

Horizontal.png.bc02df76c2ce90ec90784acad0e9c897.png

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:

Vertical.png.12daa6de012c09638f6918cc815261d5.png

Here is the macro as a single macro and as a library:

S-curve.afmacro

S-curve.afmacros

John

Windows 10, Affinity Photo 1.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Link to comment
Share on other sites

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):

1683612184_Straightcurve.png.f6375e7379a976fe5b4408f513807600.png

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): 

819697854_CurvedCurve.PNG.7870f686cad5b87d10481b85670986a4.PNG

Note that the curve crosses the straight line at the half-way position.

There is a maximum adjustment that could be applied:

1202738629_MaxCurve.PNG.b5c1825ec563b8aecdce75ce9e94faff.PNG

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

Windows 10, Affinity Photo 1.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Link to comment
Share on other sites

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

Windows 10, Affinity Photo 1.10.5 Designer 1.10.5 and Publisher 1.10.5 (mainly Photo), now ex-Adobe CC

CPU: AMD A6-3670. RAM: 16 GB DDR3 @ 666MHz, Graphics: 2047MB NVIDIA GeForce GT 630

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.