Curve in Photoshop

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
gordonyang
Posts: 4
Joined: 2011-02-24T07:31:20-07:00
Authentication code: 8675308

Curve in Photoshop

Post by gordonyang » 2011-02-24T09:12:46-07:00

Hey Guys, i try to simulate the curve in photoshop by using MagicWand.
I tried MagickFunctionImageChannel, it works well when i only has two arguments, as below:

double d[4];
d[0] = 0.0; d[1] = 0.0; d[2] = 1.0; d[3] = 0.5;
status = MagickFunctionImageChannel(magick_wand_local,RedChannel,PolynomialFunction,4,d);

but when i try to add another control point to the function, it just messed up, i cant figure out any clue to connect the value in PS curve
to MagickFunctionImageChannel.

Any idea?

User avatar
fmw42
Posts: 25559
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Curve in Photoshop

Post by fmw42 » 2011-02-24T10:43:22-07:00

Don't know how to help you, but you may be interested in looking at my script, curve and spline.

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Curve in Photoshop

Post by el_supremo » 2011-02-24T12:52:12-07:00

I think you may not be using the function correctly.
If I read the code correctly, then the arguments that you give are the coefficients of a polynomial, not the coordinates of points on a curve.
The first argument is the coefficient of the highest order in the polynomial. If the pixel value is "p", then for each such pixel in the image the new value P, given your array d[], would be

Code: Select all

P = d[0]*p^3 + d[1]*p^2 + d[2]*p + d[3];
and given your specific values for d[] this will reduce to: V = p+0.5;
I don't know if ImageMagick has the kind of curve fitting that you would need.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

gordonyang
Posts: 4
Joined: 2011-02-24T07:31:20-07:00
Authentication code: 8675308

Re: Curve in Photoshop

Post by gordonyang » 2011-02-24T19:07:51-07:00

Fred, thank you. Your script helped me very much when i study the IM.

My problem now is how to translate the script to API argument.
like


http://www.imagemagick.org/Usage/transf ... polynomial
convert gradient.png -function Polynomial 4,-4,1 func_parabola.png


just cant figure out how to assign the value of those arguments.
You know that?


Pete:
i think the formula you given should be generate by "MagickFunctionImageChannel" and function "PolynomialFunction".
After all, i cant get the value of R Channel of each pixel manually.

Gordon

User avatar
fmw42
Posts: 25559
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Curve in Photoshop

Post by fmw42 » 2011-02-24T19:38:29-07:00

splines do not conform to a simple polynomial. you should use one technique or the other. unfortunately, I really do not know what you are trying to do with control points. do you have any picture or example you could provide?

gordonyang
Posts: 4
Joined: 2011-02-24T07:31:20-07:00
Authentication code: 8675308

Re: Curve in Photoshop

Post by gordonyang » 2011-02-24T19:53:15-07:00

Hi Fred, thanks for you quick reply
http://www.flickr.com/photos/59931911@N05/5474937051/

the right corner is the original image,
the left image curves effect.

I want to simulator this effect with IM API.

Now i can do this with command line:
convert test.png -channel B -fx '(1/(1+exp(10*(.5-u)))-0.0066928509)*1.0092503' sigmoidal.png


But dont know how to use the IM Api to do this. I try to use IM on iPhone.

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Curve in Photoshop

Post by el_supremo » 2011-02-24T20:33:06-07:00

The MagickWand API has the functions which do the same the same thing as -fx on the command line.
% MagickWand *MagickFxImage(MagickWand *wand,const char *expression)
% MagickWand *MagickFxImageChannel(MagickWand *wand,const ChannelType channel,const char *expression)

This:

Code: Select all

-channel B -fx '(1/(1+exp(10*(.5-u)))-0.0066928509)*1.0092503'
would become this:

Code: Select all

new_wand = MagickFxImageChannel(magick_wand,BlueChannel,"1/(1+exp(10*(.5-u)))-0.0066928509)*1.0092503");
Of course, this won't be any faster than the command line because the expression is still interpreted within IM. To speed it up you would have to process the pixels yourself.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

gordonyang
Posts: 4
Joined: 2011-02-24T07:31:20-07:00
Authentication code: 8675308

Re: Curve in Photoshop

Post by gordonyang » 2011-02-27T22:48:34-07:00

Thanks Pete,
It is really very slow, i found a new direction, as you mention at beginning, i do need the Polynomial of the curves.
P = d[0]*p^3 + d[1]*p^2 + d[2]*p + d[3];
and pass all those vaule in *d to
MagickFunctionImageChannel(magick_wand_local,RedChannel,PolynomialFunction,4,d);

That works after i test with simple curves like:
y=-x+1; y=0.5x; y=-0.5x+0.5; y=-3x^2+3x;
Polynomial from all those coordinate of the control point on the curves.

Take y=-3x^2+3x example

then *d should be d[0]=-3; d[1] =3, then
MagickFunctionImageChannel(magick_wand_local,RedChannel,PolynomialFunction,4,d);

i got the exactly the same effect from the PS.

Now i got a new problem.......
how can i get the Polynomial from a serial of coordinates?

Any idea?


Gordon

suprandr
Posts: 1
Joined: 2011-09-14T00:34:43-07:00
Authentication code: 8675308

Re: Curve in Photoshop

Post by suprandr » 2011-09-14T01:28:55-07:00

Hey Gordon, did you solve this problem? I am really interested in this topic. Could you post some hint or solutions you've found?

Thanks

Andrea

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Curve in Photoshop

Post by anthony » 2011-10-20T21:49:33-07:00

Converting a series of coordinates into a polynomial requires a 'least squares fit' followed by the solving of simultanious equations. Both are typically done using matrix solving methods.

IM does have some matrix solving functions built in to it (MagickCore, "matrix.c"), Specifically look at the API notes for LeastSquaresAddTerms() which shows and example coding used for solving the polynomials for Affine matrices. You don't need to load all of MagickCore API to use it, just the "matrix.h", it is actually a independent module to the rest of MagickCore.

However using them from your own API is up to you.

HINT: the example given is for solving a 3x3 matrix to a 3x2 soltution.
A polynomial is a x->y equation, as only one result 'y' is needed, the result matrix is a single array
The terms array would be 4 values: x^3, x^2, x, and 1 (for a 4 term cubic polynomial).
You would then eliminate the 'least squares' 4x4 matrix and 4x1 vector -- the later holding the solution at the end.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply