gpt4 book ai didi

julia - 透视在 Julia 中扭曲图像

转载 作者:行者123 更新时间:2023-12-02 00:42:33 25 4
gpt4 key购买 nike

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

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

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

错误:尚未定义 CoordinateTransformations.PerspectiveMap 的逆变换。

最佳答案

答案有两个组成部分:

  • 您必须定义一个将 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

关于julia - 透视在 Julia 中扭曲图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45772848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com