有兩個組成部分答案:
- 你必須定義一個將2矢量轉換爲2矢量的轉換
- 如果轉換不是可逆的,那麼您必須指定最終圖像手冊的索引範圍LY。
爲先,以下就足夠了:
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](https://i.stack.imgur.com/IWb2X.png)
和imgw
看起來是這樣的:![Warped image](https://i.stack.imgur.com/Njszy.jpg)