Page 1 of 1

Curve in Photoshop

Posted: 2011-02-24T09:12:46-07:00
by gordonyang
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?

Re: Curve in Photoshop

Posted: 2011-02-24T10:43:22-07:00
by fmw42
Don't know how to help you, but you may be interested in looking at my script, curve and spline.

Re: Curve in Photoshop

Posted: 2011-02-24T12:52:12-07:00
by el_supremo
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

Re: Curve in Photoshop

Posted: 2011-02-24T19:07:51-07:00
by gordonyang
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

Re: Curve in Photoshop

Posted: 2011-02-24T19:38:29-07:00
by fmw42
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?

Re: Curve in Photoshop

Posted: 2011-02-24T19:53:15-07:00
by gordonyang
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.

Re: Curve in Photoshop

Posted: 2011-02-24T20:33:06-07:00
by el_supremo
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

Re: Curve in Photoshop

Posted: 2011-02-27T22:48:34-07:00
by gordonyang
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

Re: Curve in Photoshop

Posted: 2011-09-14T01:28:55-07:00
by suprandr
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

Re: Curve in Photoshop

Posted: 2011-10-20T21:49:33-07:00
by anthony
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.