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