Curves Adjustments - math help

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Curves Adjustments - math help

Post by GreenKoopa »

I would like to make a 'Curves' color adjustment. My problem is generating polynomial coefficients given control points.

Image

A curves adjustment can be achieved using -function Polynomial (usage example). The problem is deciding on coefficients. In an image program such as Photoshop or GIMP, you select a few control points that map input values to output values. There is a usage example that gives some tips on calculating polynomial coefficients given a few control points, but not for Windows. Does anyone know a way to do this on Windows or using a website?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Curves Adjustments - math help

Post by fmw42 »

you can use control points in my script, curves, if you are on linux/mac or have cygwin/windows. then you don't need a polynomial as it uses the Catmul-Rom spline to interpolate the points.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Curves Adjustments - math help

Post by anthony »

It is posible for this to be built into IM. But some programmer will need to encode the new function to generate the curve.

Distorts for example internal matrix solving to calculate the function coefficients it needs for distorting images. Maybe it can do curve fitting, as it does output results using verbose!

How many points are you thinking of using? Can you give an example?


Another possibility is to generate a LUT gradient using -sparse-colors, though the results may not be very exact.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Curves Adjustments - math help

Post by anthony »

Here is a way of generating a Shepards Sparse Color LUT gradient

generated using the same points used for the 'curves example'
0.0,0.0 1.0,0.9 0.2,0.8 0.7,0.5

To generate a gradient image is 1000 pixels long, the first number of the above coordinate pairs become a y value *1000. The second number becomes a Gray value.

Code: Select all

convert -size 20x1000 xc: -sparse-color Shepards  \
             '0,0 black          0,1000 gray(90%)
              0,200 gray(80%) 0,700 gray(50%)' \
            -rotate -90   shepards_curve_gradient.png
Here is the profile graph of the generated gradient (using a gnuplot based im_profile script).
Image
The curve generated in not very smooth, but then it isn't so much of a curve generation as a interpolation method.

But once you have a gradient, you can use it as a LUT to adjust your image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Curves Adjustments - math help

Post by fmw42 »

anthony wrote:It is posible for this to be built into IM. But some programmer will need to encode the new function to generate the curve.

Distorts for example internal matrix solving to calculate the function coefficients it needs for distorting images. Maybe it can do curve fitting, as it does output results using verbose!

How many points are you thinking of using? Can you give an example?


Another possibility is to generate a LUT gradient using -sparse-colors, though the results may not be very exact.

Anthony,

Interesting use of Sparse Color interpolation! But am I correct in that using Sparse Color is approximating and not interpolating in that it will not necessarily go exactly through the control points specified.

I also think it would be rather easy to generate a lut via IM as a new function (or directly apply the lut via -clut within the new IM function) from control point pairs (input, output in range 0 to 1 or 0 to 100% or 8-bit or 16-bit values, whatever you want) that are interpolated (go exactly through the control points) using Catmul-Rom spline as in my curves script. If you want to discuss, let me know. Of course it is not interactive, but someone could implement that in PHP if they wanted. The Catmul-Rom interpolation is rather simple. One takes 4 points and computed the interpoation for the region between the second and third. Two added points are needed, one at the beginning and one at the end, but they are just straight line extension of the first and last intervals from the given control points.

Here is your same set of points from my curves script:

Image



Fred
Last edited by fmw42 on 2010-12-20T21:28:23-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Curves Adjustments - math help

Post by anthony »

The definition of Sparse color is that the specified points are fixed. If the point is an actual pixel location (add 0.5)
then that pixel will be guaranteed to have the color specified.

They will only become approximate if you continue processing the resulting gradient image with things such as BLUR.


The thing of greater concern is the lack of other color processing functions. Shepards for example will always have a zero gradient slope at the specified points. Especially on the ends of the curve. Which is not particularly nice for some CLUT uses. It also has a tendancy for the curve to 'dip' toward an average value when far away from any control
points.

For example try this...

Code: Select all

    convert -size 20x1000 xc: -sparse-color Shepards \
                  '0,0 black      0,1000 black    
                  0,200 gray90 0,700 gray90' \
                -rotate -90 miff:- | im_profile -
The center of the curve 'sags' downward between the two 90% gray points!

It is not a particularly nice interpolation, and is a 2d interpolation being used to generate a 1D curve.
A better method would be to use Fred Wienhaus's special gradient generator script (above)


Polynomial by contrast typically has the highest gradient slope at ends.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Curves Adjustments - math help

Post by fmw42 »

see my curve that I generated to emulate your set of points. see above -- edited to add graph
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Curves Adjustments - math help

Post by GreenKoopa »

I think I need to take a break from IM to get a portable cygwin and finally learn some Linux. Gravity is about to overtake friction on this steep learning curve.
Post Reply