To start with remove the final crop, it should be irrelevent to the problem and just may cover up details. You can include the crop directly in the distorting (and even use floating point offsets) using the
distort viewport expert option
Now your input image has a virtual canvas! That is the actual image may not be where you are expecting it to be!
out-interim.png PNG 401x74 1430x1734+0+42 8-bit DirectClass 616B 0.000u 0:00.000
This is caused by using -crop without a final +repage when the virtual canvas of the crop is not relevant.
Remember Distort is designed to preserve and work with virtual canvas images. As such it distorts the virtual canvas and not the actual image. That was done on purpose!
However adding a +repage before the distort does not seem to help in this case, though changing the final coordinate from 39 to 0 does make the resulting image at least partially visible!
Code: Select all
convert "out-interim.png" \
-mattecolor none -matte -virtual-pixel transparent \
-distort BilinearForward "0,0 241,29 400,0 165,41 400,31 166,52 0,31 241,0" \
show:
Second you have twisted the image so you are looking at its backside! It has been flipped in some way.
Bilinear Distortions were never really meant to be use in a situation where you get hour-glass or flipped images.
Basically the input image and coordinates just really do not make much sense. so you get a non-sense result.
I would like to repeat a previous question --
What generated this image and these coordinates?
--------------------------------------------------
Implementation Explanation
--------------------------------------------------
Lets have a look at flipped images to see what IM is doing...
Hour-glass like shapes are an intermediate form that will at least keep some of the image visible, even though they are not really 'sensible'. For example swapping the left coordinates of a rose image...
Code: Select all
convert rose: -mattecolor blue -background dodgerblue -virtual-pixel background \
-distort BilinearForward "0,0 0,46 0,46 0,0 70,0 70,0 70,46 70,46" \
rose_leftswap.png
However forming an hourglass by swapping the two top coordinates makes the top half the hourglass 'disappear'
Code: Select all
convert rose: -mattecolor blue -background dodgerblue -virtual-pixel background \
-distort BilinearForward "0,0 70,0 0,46 0,46 70,0 0,0 70,46 70,46" \
rose_topswap.png
If the bottom two coordinates were also then swapped, then the whole image disappears as the image becomes 'flipped over' from left to right.
The image does not disappear if all the coordinates were flipped top to bottom!
This I would regard as a bug But one that will need some work to resolve.
The reason for the different results is due to the need to solve a quadratic function. There are two valid ways to solving the quadratic Y first then substitute for X, or X first then substitute for Y, but only one is implemented. You can see what is implemented by turning on -verbose
Code: Select all
convert rose: -mattecolor blue -background dodgerblue -virtual-pixel background \
-verbose -distort BilinearForward "0,0 70,0 0,46 0,46 70,0 0,0 70,46 70,46" \
+verbose null:
Code: Select all
BilinearForward Mapping Equations:
i = -1.000000*x -1.521739*y +0.043478*x*y +70.000000;
j = +0.000000*x +1.000000*y +0.000000*x*y +0.000000;
BilinearForward Distort, FX Equivelent:
-fx 'ii=i+page.x-69.500000; jj=j+page.y+0.500000;
bb=0.000000*ii -0.043478*jj -1.000000;
rt=bb*bb -0.173913*(0.000000*ii+1.000000*jj);
yy=( -bb + sqrt(rt) ) / 0.086957;
xx=(ii +1.521739*yy)/(-1.000000 +0.043478*yy);
(rt < 0 ) ? red : p{ xx-page.x-.5, yy-page.x-.5 }'
As you can see from the FX equivelent code currently we solve Y first, the substitute to solve for X. If we did it the other way, the the image would disappear on a one side top-bottom flip.
If we can determine when it is better to solve it one way, over the other way then we can fix this problem by solving for the appropriate case. Fred Weinhaus may be able to figure out when to do this, and how we can correct our implementation.
ASIDE: I would still like to get a proper bilinear distort of any-quad to any-quad sorted out, though I really just have no time for programming due to heavy work commitments.