There are many functions in Mathematica, which return expressions with TransformationFunction head, for example RotationTransform. Unfortunately, there is no PerspectiveTransform amongst them.

How to create custom transformation functions in general? There is no explanation in docs. It is only said that "TransformationFunction works like Function." what does it mean? That writing TransformationFunction is just writing a Function?

Also it is said that when possible "matrix forms for transformations can be obtained from TransformationFunction objects using TransformationMatrix."

Can I have some samples of custom TransformationFunction with and without matrix?


If I provide my transformation (homogenous) matrix it produces "nonaffine" error:

enter image description here

enter image description here

enter image description here


Now trying to form transformation via LinearFractionalTransform and effect ios the same

enter image description here

  • 1
    $\begingroup$ have a look at FindGeometricTransform and the Transformation options $\endgroup$
    – Stefan
    Jun 18 '13 at 8:21
  • $\begingroup$ Ok, FindGeometricTransform can return perspective transform. But what about generating custom one? $\endgroup$
    – Suzan Cioc
    Jun 18 '13 at 8:31
  • $\begingroup$ Just provide the transformation matrix as an argument, e.g. custom = TransformationFunction[Array[Plus,{4,4}]] $\endgroup$ Jun 18 '13 at 9:13
  • $\begingroup$ @SimonWoods does not work; see my update $\endgroup$
    – Suzan Cioc
    Jun 18 '13 at 9:40
  • $\begingroup$ The problem in your update is with GeometricTransformation, which can only deal with affine transformations. $\endgroup$ Jun 18 '13 at 10:33

You can define any matrix as a transformation in LinearFractionalTransform:

t = LinearFractionalTransform[{{1, 2, 1}, {4, 5, 3}, {1, 1, 9}}]

However, this can only be applied to a geometric object using

 Graphics[GeometricTransformation[Rectangle[], t]]

if the matrix is affine.

The subtlety is that the TransformationFunction itself need not be affine, but that GeometricTransformation requires an affine transformation. In 2D, the distinction is that an affine transformation represents a mapping A.x+b where A is a 2 by 2 matrix and b is a 2 by 1 vector. These six numbers become the top two rows of the Transformation matrix, and the bottom row is {0,0,1}. GeometricTransformation gives an error if this is not the case. In the comments, Simon Woods points out that after issuing the error, GeometricTransformation then calculates the output as if the bottom row were {0,0,1}, effectively truncating the bottom row. This behavior is alluded to in the help file, which says: GeometricTransformation[g,{m,v}] effectively applies an affine transform to g." Apparently, this same restriction applies to the form `GeometricTransformation[g, tfn] as well.

If you wish to transform images through your non-affine mapping, there is the function ImagePerspectiveTransformation which allows application of any matrix transformation to a 2D image. The first example in the help file shows a transformation with a matrix that is clearly non-affine. So if you have an image to manipulate, or if you are willing to rasterize your current object, this can be done straightforwardly.

  • $\begingroup$ You used different values and I don't understand how this relates with my case. $\endgroup$
    – Suzan Cioc
    Jun 18 '13 at 10:22
  • $\begingroup$ Ah, you meant that to get transformation represented by arbitrary homogenous matrix it should be passed to LinearFractionalTransform, not to TransformationFunction? $\endgroup$
    – Suzan Cioc
    Jun 18 '13 at 10:24
  • $\begingroup$ @Suzan, yes. ${}$ $\endgroup$
    – J. M.'s torpor
    Jun 18 '13 at 10:44
  • $\begingroup$ @J. M. it also does not work $\endgroup$
    – Suzan Cioc
    Jun 18 '13 at 10:54
  • $\begingroup$ @J. M., LinearFractionalTransform just passes the matrix through to TransformationFunction. The real issue here is that GeometricTransformation can't handle non-affine transformations. $\endgroup$ Jun 18 '13 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.