我有一个图像和一个3x3透视投影矩阵M
。如何在图像上应用变换?
我尝试使用warp(img, tform)
函数,但不知道如何从矩阵构造变换对象。
尝试tform = PerspectiveMap() ∘ inv(LinearMap(M))
,不知道创建转换是否正确,但它失败了:
ERROR: Inverse transformation for CoordinateTransformations.PerspectiveMap has not been defined.
答案 0 :(得分:4)
答案有两个组成部分:
对于第一个,以下就足够了:
julia> using StaticArrays, CoordinateTransformations
julia> M = @SMatrix [1 0 0; 0 1 0; -1/1000 0 1] # a 3x3 perspective transformation matrix
3×3 StaticArrays.SArray{Tuple{3,3},Float64,2,9}:
1.0 0.0 0.0
0.0 1.0 0.0
-0.001 0.0 1.0
julia> tform = PerspectiveMap() ∘ inv(LinearMap(M))
(CoordinateTransformations.PerspectiveMap() ∘ LinearMap([1.0 0.0 0.0; -0.0 1.0 0.0; 0.001 -0.0 1.0]))
julia> tform(@SVector([1,1,1])) # this takes a 3-vector as input and returns a 2-vector
2-element SVector{2,Float64}:
0.999001
0.999001
julia> push1(x) = push(x, 1)
push1 (generic function with 1 method)
julia> tform2 = PerspectiveMap() ∘ inv(LinearMap(M)) ∘ push1 # here's one that takes a 2-vector as input (appends 1 to the 2-vector)
(::#55) (generic function with 1 method)
julia> tform2(@SVector([1,1]))
2-element SVector{2,Float64}:
0.999001
0.999001
现在让我们尝试一下这张照片。我们将创建一个与输入图像具有相同索引的输出图像,但您可以选择any indices you want:
julia> using Images, TestImages
julia> img = testimage("lighthouse");
julia> imgw = warp(img, tform2, indices(img)); # 3rd argument sets the indices
julia> using ImageView
julia> imshow(imgw)