Polynomial Curves Incorrect Results ?

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Polynomial Curves Incorrect Results ?

Post by djkprojects »

Hello all,

I'm working on a project and decided to explore ImageMagick library as all the alternatives I'm familiar with seem to be not efficient enough to perform tasks required by this project. ImageMagick command line so far seems to be fastest option however I'm having a problem with applying curves to RGB channels.

I've downloaded the im_fx_curves perl script to generate a,b,c..n factors but it seems to be giving incorrect results:

Code: Select all

perl im_fx_curves.pl -p 0,0 62,45 128,127 204,187 255,255
gives (1):

Code: Select all

 0.291*u^4 - 1.500*u^3 + 2.434*u^2 - 0.276*u
Now if I use this function with X-coords as input to verify Y-coords I'm getting:

Code: Select all

0,0   62,3951761    128,75008833    204,491347155   255,1205707069
(i've rounded all the numbers up to the nearest integer)

I didn't investigate im_fx_curves code so I assume there is an issue with it or Math::Polynomial lib

I have created a function that finds a,b,c...n factors using "polynomial least squares fittings" that for the same input points returns (2):

Code: Select all

 0.000000290732318603847*u^4 - 0.00014996946075633902*u^3 + 0.02434208901325919*u^2 - 0.2762101120900123*u - 0.0000000000001016845989170083
As you can see the values are pretty similar - just the dots are in different positions

When I apply my generated function to an image I get very similar results to what I get in Photoshop CS5 curves adjustment tool for R, G, B or RGB channels. The result when using -function Polynomial with im_fx_curves factors is very different. My code is as below:

Code: Select all

$cmd= "convert $in -fx '0.291*u^4 - 1.500*u^3 + 2.434*u^2 - 0.276*u'  $out";

or

$cmd= "convert $in -function Polynomial '0.291,-1.500,2.434,-0.276,0'  $out";
that is triggered via PHP exec(), not directly from a command line but that shouldn't make any different.

Applying factors from (2) results in black image and I believe that is because IM rounds up all the values to 3 decimals.

Can you please advise why am I getting so different results - am I missing something ?

Thanks
djkprojects
Posts: 21
Joined: 2012-07-06T04:44:48-07:00
Authentication code: 13

Re: Polynomial Curves Incorrect Results ?

Post by djkprojects »

Hello,

To answer my question: to simulate Photoshop Curve adjustment tool all the inputs have to be first scaled from 0-255 range down to 0-1 and then applied to im_fx_curves (or directly to gnuplot) to get abc..n parameters. It works perfectly now :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Polynomial Curves Incorrect Results ?

Post by anthony »

Yes IM generally uses normalized values to avoid problems between IM Q16 (16 bit values) and IM Q8 (whcih photoshop uses).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply