[CLOSED] possible bug +distort polynomial

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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

[CLOSED] possible bug +distort polynomial

Post by fmw42 »

Tested with IM 6.9.10.1 Q16 Mac OSX

Ultimately I am trying to use +distort polynomial for a 2 or 3 order polynomial. But I could not get it to work properly. So I made a simple test case with a first order transformation and compared +distort affine to +distort polynomial. Here are my test. Essentially, I think the + form of distort polynomial is not working properly.

Input:
Image

Affine is working:

Code: Select all

convert -size 300x300 xc:red red.png
cp_pts="0,0 299,299  299,0 599,299  299,299 599,599 0,299 299,599"
convert -size 600x600 xc:black \
\( red.png -virtual-pixel black +distort affine "$cp_pts" \) \
-flatten red1.png
Image


Disort Polynomial is not working:

Code: Select all

convert -size 300x300 xc:red red.png
cp_pts="0,0 299,299  299,0 599,299  299,299 599,599 0,299 299,599"
convert -size 600x600 xc:black \
\( red.png -virtual-pixel black +distort polynomial "1 $cp_pts" \) \
-flatten red2.png
Image

Anthony's example of the - form seems to work. See his grid example at http://www.imagemagick.org/Usage/distorts/#polynomial
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: possible bug +distort polynomial

Post by snibgo »

@Fred: the "-distort polynomial" operation is not shown at http://www.imagemagick.org/script/comma ... hp#distort . Can it be added, please?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug +distort polynomial

Post by fmw42 »

I will try to add it the next time I do edits. I put in all your previous ones by the way last week.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: possible bug +distort polynomial

Post by magick »

Fred, you'll need to check with Anthony. His usage pages show the last ImageMagick version he validated was 6.9.3-0 and this version returns the same results exhibited here suggesting that polynomial distortion is working as designed or there is a bug Anthony is not aware of.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug +distort polynomial

Post by fmw42 »

magick wrote: 2018-06-18T16:54:51-07:00 Fred, you'll need to check with Anthony. His usage pages show the last ImageMagick version he validated was 6.9.3-0 and this version returns the same results exhibited here suggesting that polynomial distortion is working as designed or there is a bug Anthony is not aware of.
It looks to me that -distort works fine, but +distort is not keeping virtual canvas. Is that not something you can fix? Or need I try to get Anthony to fix it.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: possible bug +distort polynomial

Post by magick »

Sanity check. The https://www.imagemagick.org/Usage/distorts/#polynomial example appears to work as expected:

Code: Select all

convert mandrill_grid.jpg -matte -virtual-pixel black -distort Polynomial \
     '1.5   0,0 26,0   128,0 114,23   128,128 128,100   0,128 0,123' mandrill_poly_1.5.jpg
Change to +distort, it returns expect results. The usage docs say +distort will attempt resize the distorted image so it will contain the whole of the input image (if possible). Does these commands return expected results for you. If so, it suggests polynomial distortion is worked as designed.

Are your polynomial coefficients correct? If you are confident they are, we will investigate further and determine if there is a bug in IM triggered by your coefficients.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug +distort polynomial

Post by fmw42 »

I am confident I have it correct. I am confident +distort is not working correctly from my examples above. The affine works properly to keep the virtual canvas so that when flattened according to the offset coordinates, it gets shifted. The polynomial does not do the offset for the same set of control points and a first order polynomial.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: possible bug +distort polynomial

Post by anthony »

The difference in -distort and +distort is supposed to only be in the calculation of the 'viewport' of the final result.

You can also override the viewport of BOTH of those operators using -define distort:viewport=WxH+X+Y
See http://www.imagemagick.org/Usage/distor ... t_viewport

You can get more detail of what the operators decide on (in terms of viewport and calculations) using -verbose
As mentioned in http://www.imagemagick.org/Usage/distorts/#polynomial
That should let you see how polynomial differs to Affine.

NOTE for the +distort viewport "best fit" determination...
The distort uses a reverse lookup (EG: destination -> source lookup) when distorting images, as described at the top of IM Examples.
However to actually figure out the "Best Viewport" actually requires a forward lookup (Source -> Destination coord) for at least the four corners, and a few more edge coordinates for polar (circular) distorts. Polynomial is generally NOT however reversible, so it may be that while Affine is reversible, Polynomial is (in general) not. In which case +distort would fall back to -distort (no viewport fit).

If that is true, then I should mention that +distort (viewport fit) does not work on the IM Examples page on Polynomial distort, (like I do for free-form Shepards distort), But I haven't. IE: A documentation error!

If Fred would like to verify that. I will then update IM Examples Polynomial to mention that +distort is NOT available, for the same reason as it isn't for Shepards Distortions.
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: possible bug +distort polynomial

Post by fmw42 »

I think Anthony may be correct. The polynomial may not actually be reversible at least in an easy way. However, @Anthony, does polynomial support EWA or are you defaulting to something else. With EWA, you would have had to have the derivatives of the inverse to compute the ellipse.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug +distort polynomial

Post by fmw42 »

After looking into it further, there are no easy inverses for arbitrary polynomial other than first order and some very simple higher order ones. I found this quote by a Google search.
Formulas for the inverse of a polynomial, when they exist, often involve complicated combinations of roots of various degrees.
So I must assume and perhaps have a slight recollection of discussing this with Anthony a long time ago that the +distort is the same as -distort for the polynomial warp (and I assume it does not do EWA and likely is defaulting to Lanczos, but Anthony would have to confirm that).

I will try to include this in the documentation.

So this issue is closed.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [CLOSED] possible bug +distort polynomial

Post by anthony »

Lancos is a resampling filter and as such needs a resampling area (that is a Elliptical Weighted Average).
Without EWA you can't use Lancos, but you can use a 'scale-less' single point lookup... That is Interpolation!

The EWA may actually work, as the function derivative, which is what is needed for determining the sampling Ellipse, is actually easily determined for a polynomial. An easy way to check would be to use to shrink the output image (easy why to do this is use -define distort:scale=0.2 to shrink the output viewport). If you get good anti-aliasing then EWA is working, otherwise you get single point interpolation, and severe aliasing.

An alternative is to set -interpolate background (interpolate always returns background color). If you then only get the background color in the resulting image, EWA is disabled! You can verify that this happens by specifically disabling resampling (and thus EWA) using -filter point to force IM to fall back to using interpolation.

Using all the above methods, I have confirmed EWA is being used.
There are lots of ways to skin a cat, and what method you use depends
on what you want that skin for, and how messy you like the results!
-- Anthony Thyssen


Added to "Polynomial Distort" http://www.imagemagick.org/Usage/distorts/#polynomial
Due Polynomial Distortion being non-reversible, it isn't posible for IM to calculate the 'best-fit' of the destination image's viewport, for a given source image. As such the "+distort" form of the operator does not work, and falls back to a normal "-distort" operation. You can however still use the Distort Viewport option to define the viewport of the destination image.
With appropriate links of course.

I have confirmed this using -verbose IF a viewport image was created due to +distort the -verbose option would show output appropriate -size and -page commands to generate a viewport of a destination image, that is different to the input image. Setting a -define distort:viewport does generate those commands in the verbose output.
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: [CLOSED] possible bug +distort polynomial

Post by fmw42 »

Anthony, perhaps I was wrong. I though you needed derivative in both directions to do the EWA ellipse computations. I seem to remember having to give you derivatives in both directions. Am I wrong? Or was that only with regard to +distort?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: [CLOSED] possible bug +distort polynomial

Post by anthony »

Using +disort to map the source image bounds to destination image to calculate the bit-fit of the destination image viewport, needs the reverse of the distortion function. That is not possible with polynomial

EWA calculations requires the derivative (slope) of the function going from destination image to source image to determine the size and shape of the ellipical area to sample. The Polynomial Derivative is very straight forward.

Neither is however possible for Shepard's, though EWA may be able to be calculated by mapping the edges of the circle in the destination image to the source image and using the differences to get a rough calculation of the slopes. That is currently not done.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply