Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control points to curve formula for ImageMagick's '-FX' function using Gnuplot on Windows

I had many issues replicating the generated curve formula example found in the ImageMagick web site on my Windows platform until now.

I've finally worked it out and would like to share it with you.

The FX formula from the ImageMagick web site is needed to specify a custom curve adjustment in ImageMagick for a particular channel just like Photoshop. However, the examples on the web site can be rather misleading when you try to replicate it using Windows.

So my question was:

What are the exact steps I need to follow in order to replicate the example from the ImageMagick web site on Windows?

like image 816
Tremours Avatar asked Dec 14 '14 09:12

Tremours


2 Answers

Preliminaries:

  • GnuPlot is a command line mathematical program for plotting data and generating other mathematical tasks.
  • ImageMagick is a command line photo editing program.

I'm currently running Windows 7 and GnuPlot 4.6

Here are the steps.

  1. Install GnuPlot.

  2. Download the fx_control.txt file from http://www.imagemagick.org/Usage/color_mods/fx_control.txt. This file contains the control points. (These are represented by x/y coordinate pairs of 4 special points on the curve graph).

    If you need to replicate a curve in Photoshop, you can grab the coordinates from the curves window in Photoshop. Note: When I open the file in NotePad++ the x&y coordinates are on one line, however every set of control points are on a separate line. So if you are wanting to modify the control point in the file I suggest you install and use notepad++.

  3. Open GnuPlot. Once loaded you will see a command prompt similar to DOS.

  4. Paste the following text behind the gnuplot> prompt and hit return:

    f(x) = a*x**3 + b*x**2 + c*x + d
    
  5. Now paste the following text into Gnuplot and hit return. In this example, I'm assuming you have saved the 'fx_control.txt' file to the root directory on your C: drive.

    fit f(x) "c:/fx_control.txt" via a, b, c, d
    
  6. If you have done everything right, you should have seen a heap of text appear in GnuPlot.

  7. Now paste the following text into Gnuplot and hit return.

    print a,"*u^3 + ",b,"*u^2 + ",c,"*u + ",d
    
  8. GnuPlot has now displayed the curve formula which you use with the ImageMagick FX function. The result matches the example on the ImageMagick website and should be:

    7.55952380952381*u^3 + -11.9464285714286*u^2 + 5.08690476190476*u + 0.2
    
  9. To display a plot of this function, copy'n'paste the following line to the gnuplot> prompt:

    plot [0:1][0:1] 7.55952380952381*x**3 + -11.9464285714286*x**2 + 5.08690476190476*x + 0.2
    

    You should see something like this:

    Plot of function above

I hope these steps help you!

like image 191
Tremours Avatar answered Sep 30 '22 10:09

Tremours


By Anthony, the Author of ImageMagick Examples

NOTE: while the final curve shown in the above is correct, only the small segment of the curve between 0.0 and 1.0 (horizontally) and limited to 0.0 to 1.0 (vertically) is used.

To get gnuplot to limit the results of the copy-n-pasted function to those bounds use a command such as...

plot [0:1][0:1] 7.55952380952381*x**3 + -11.9464285714286*x**2 + 5.08690476190476*x + 0.2

You can also add the control points to the graph so you can see how well they fit...

plot [0:1][0:1] 7.55952380952381*x**3 + -11.9464285714286*x**2 + 5.08690476190476*x + 0.2, "c:fx_control.txt"

Just one small note that should be made clearer. - Imagemagick uses 'u' where gnuplot uses 'x' (though that can be changed) - Imagemagick uses ^ where gnuplot uses ** for mathematical exponentiation (power-of) operation. keeping this in mind will make converting the gnuplot output to ImageMagick usage much easier.

One final note. You can generally get help on using ImageMagick a lot faster by posting, or searching the ImageMagick Users Forum.

like image 34
anthony Avatar answered Sep 30 '22 11:09

anthony