How I can apply an Projective Transform (or an Affine) to a image based on a transform matrix ?
In phyton I can do this using:
tform = transform.ProjectiveTransform(matrix=matrix)
An example in Python (from Documentation):
from skimage import data
from skimage import transform
from skimage import img_as_float
from skimage import transform
img= img_as_float([data.chelsea])
# Define the transformation matrix
matrix = [[1.0060923472904499, 6.8872896368778122e-002, -4.7977514755983762e+001], [-8.4231722023165673e-002, 9.8715396636307107e-001, 6.7039265228088230e+000], [1.7669387251995766e-015, -3.0176300815504264e-015, 9.9999999999999978e-001]];
# Create the projective transform
tform = transform.ProjectiveTransform(matrix=matrix)
tf_img = transform.warp(img, tform.inverse)
fig, ax = plt.subplots()
ax.imshow(tf_img)
ax.set_title('Projective transformation')
plt.show()
It looks like a nice API for this is an outstanding feature requests in ImageTransformations.jl. The core functionality is there as warp
, but it takes some doing to make it work currently.
opened 03:09AM - 23 Jul 21 UTC
It's better to have `Projective` transformation to `ImageTransformations.jl`.
…
```julia
# Definition of projective transformation
"""
Projective transformation.
"""
struct Projective <: Transformation
H::SArray{Tuple{3,3},Float64,2,9}
end
"""
Definition of projective transformation.
"""
function (p::Projective)(x)
x_ = SA[x[1],x[2],1.0]
x′_1, x′_2, x′_3 = p.H*x_
return SA[x′_1/x′_3, x′_2/x′_3]
end
"""
Inverse of projective transformation.
"""
function Base.inv(p::Projective)
Projective(inv(p.H))
end
```
Discussions in Slack:
https://julialang.slack.com/archives/CB1R90P8R/p1626929588093000
https://julialang.slack.com/archives/CB1R90P8R/p1626983453103200
>>I feel the right place to add this is in CoordinateTransformations.jl
>Or we could 1) first introduce a high-level API in ImageTransformations.jl with this Projective as internal type, and then 2) migrate it to upstream CoordinateTransformations, and finally 3) use the upstream version.
(The quoted words are by @johnnychen94)
1 Like
Swirl effect using warp operation · ImageTransformations might be a good example to start with.
Simply put, you need a coordinate map function f
that receives SVector{N}
and outputs SVector{N]
, just like the swirl_map
function in the above example.
As @mbauman said, wrapping this into a function would be a good feature for ImageTransformations. I’m happy to see and merge a PR, but I don’t have time to do the devs myself.