2017-08-19 104 views
1

我有一個圖像和3x3透視投影矩陣M。我如何在圖像上應用變換?視角在茱莉亞扭曲圖像

我試圖使用warp(img, tform)函數,但不知道如何從矩陣構造變換對象。

試過tform = PerspectiveMap() ∘ inv(LinearMap(M)),不知道這是否是一個正確的創建轉換,但它失敗:

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

回答

4

有兩個組成部分答案:

  • 你必須定義一個將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

imgw看起來是這樣的:Warped image