Help converting a transformation matrix

OK!
I got it figured out, it’s of course much simpler than I thought. My original goal was to convert matlab’s transformation matrix to a CoordinateTransformations one. I can do that without decomposing them into translation, rotation, scale, and shear (or what not). I can simply do:

M = LinearMap(SMatrix{3,3, Float64}(T'))
tform = PerspectiveMap() ∘ M ∘ push1

where T is matlab’s transformation matrix (and therefore needs a transpose, '), and push1 is simply:

push1(x) = CoordinateTransformations.push(x, 1)

All of this is from Tim Holy’s answer on SO to a related question (Perspective warp an image in Julia - Stack Overflow). Thank you @tim.holy and @touste !

So now, the coordinate transformation in Julia , tform from above, generates the same results as Matlab’s T when applied to 2D coordinates. Yay! :fireworks:

But I’m still stuck on one irritating detail: When I apply this projective transformation matrix to an image with ImageTransformations.warp I run into problems related to the fact that the transformation tform is not invertible. The only way I managed to solve this (I think) is:

wimg = warp(img, itform, ImageTransformations.autorange(img, tform))

where itform is simply:

itform = PerspectiveMap() ∘ inv(M) ∘ push1

and to plot it (with Makie):

image!(ax, ImageTransformations.autorange(img, tform)..., parent(wimg))

where ax is a axis from MakieLayout.

This seems some what convoluted, which is totally fine, but I’m just worried I messed it up in some way…

I’ll also flag for future readers the issue of the convention about where the origin is in an image. In Matlab it’s in the top left corner of the image, so when you detect the checkerboard corners with detectCheckerboardPoints you get xy coordinates that are plottable (they plot correctly on the image) but cannot be used as indices (you will not find the corner in the x column and y row, rather ImageHeight - y). This complicates things even further… I think @tim.holy solved this ambiguity in Julia with ImageAxes.jl.

1 Like