透视在朱莉娅扭曲图像

时间:2017-08-19 14:43:43

标签: julia

我有一个图像和一个3x3透视投影矩阵M。如何在图像上应用变换?

我尝试使用warp(img, tform)函数,但不知道如何从矩阵构造变换对象。

尝试tform = PerspectiveMap() ∘ inv(LinearMap(M)),不知道创建转换是否正确,但它失败了:

ERROR: Inverse transformation for CoordinateTransformations.PerspectiveMap has not been defined.

1 个答案:

答案 0 :(得分:4)

答案有两个组成部分:

  • 您必须定义一个将2向量转换为2向量
  • 的转换
  • 如果转换不可逆,则必须手动指定最终图像的索引范围。

对于第一个,以下就足够了:

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)

img如下所示:Original image

imgw如下所示:Warped image