Procrustes method

This example illustrates the orthogonal Procrustes method using the Julia language.

This page comes from a single Julia file: procrustes.jl.

You can access the source code for such Julia documentation using the 'Edit on GitHub' link in the top right. You can view the corresponding notebook in nbviewer here: procrustes.ipynb, or open it in binder here: procrustes.ipynb.

Setup

Add the Julia packages used in this demo. Change false to true in the following code block if you are using any of the following packages for the first time.

if false
    import Pkg
    Pkg.add([
        "InteractiveUtils"
        "LinearAlgebra"
        "MIRTjim"
        "Random"
    ])
end

Tell Julia to use the following packages. Run Pkg.add() in the preceding code block first, if needed.

using InteractiveUtils: versioninfo
using LinearAlgebra: svd, norm, Diagonal
using MIRTjim: prompt
using Random: seed!

The following line is helpful when running this jl-file as a script; this way it will prompt user to hit a key after each image is displayed.

isinteractive() && prompt(:prompt);

Coordinate data

Coordinates from rotated image example in Ch. 5 (n-05-norm/fig/)

A = [-59 -25 49;
    6 -33 20]
B = [-54.1 -5.15 32.44;
    -24.3 -41.08 41.82]
2×3 Matrix{Float64}:
 -54.1   -5.15  32.44
 -24.3  -41.08  41.82

Procrustes solution steps

C = B * A'
2×2 Matrix{Float64}:
 4910.21   494.15
 4509.88  2046.24
(U,s,V) = svd(C)
s
2-element Vector{Float64}:
 6898.985269845125
 1133.3421659234132
Q = U * V'
2×2 Matrix{Float64}:
 0.866057  -0.499946
 0.499946   0.866057

Fitting residual

residual = B - Q * A # small!
2×3 Matrix{Float64}:
 -0.00298597    0.0031963   0.00214782
  0.000474745  -0.00148289  0.0015143

Rotation angle in degrees:

acos(Q[1]) * (180/π) # very close to 30° as expected
29.996427682008132

Fitting function

function procrustes(A, B)
    C = B * A'
    (U,s,V) = svd(C)
    Q = U*V'
    scale = sum(s) / norm(A,2)^2
    return Q, scale
end
procrustes (generic function with 1 method)

Explore additional special cases

Three points along a line, symmetrical:

A = [-1 0 1; 0 0 0]
B = [0 0 0; -2 0 2]
Q, scale = procrustes(A, B)
([-0.0 -1.0; 1.0 0.0], 1.9999999999999996)

Check:

@assert B ≈ scale * Q * A

Three points along a line, not symmetrical:

A = [-1 0 2; 0 0 0]
B = [0 0 0; -2 0 4]
Q, scale = procrustes(A, B)
([-0.0 -1.0; 1.0 0.0], 1.9999999999999996)

Check:

@assert B ≈ scale * Q * A

A single point - works fine!

A = [1; 0]
B = [2; 2] # different length!
Q, scale = procrustes(A, B)
([0.7071067811865472 -0.7071067811865475; 0.7071067811865475 0.7071067811865475], 2.8284271247461903)

Check:

@assert B ≈ scale * Q * A

Angle:

rad2deg(acos(Q[1]))
45.00000000000003

Examine some other options for Q

(U,s,V) = svd(B*A')
Q1 = U*V'
@assert B ≈ scale * Q1 * A # same as above

Q2 = U * Diagonal([1, 0]) * V' # (not unitary)
2×2 Matrix{Float64}:
 0.707107  0.0
 0.707107  0.0
@assert B ≈ scale * Q2 * A # also works for this case!

Q3 = U * Diagonal([1, -1]) * V' # is unitary
2×2 Matrix{Float64}:
 0.707107   0.707107
 0.707107  -0.707107
@assert B ≈ scale * Q3 * A # also works for this case!

Examine effect of noise

seed!(0)
σ = 0.1
An = A + σ * randn(size(A))
Bn = B + σ * randn(size(B))
Q_n, scale_n = procrustes(An, Bn)
([0.7387847859905656 -0.6739414217785354; 0.6739414217785354 0.7387847859905656], 2.692570901515366)

Angle:

rad2deg(acos(Q_n[1]))
42.37199938398432

Reproducibility

This page was generated with the following version of Julia:

using InteractiveUtils: versioninfo
io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
12-element Vector{SubString{String}}:
 "Julia Version 1.10.1"
 "Commit 7790d6f0641 (2024-02-13 20:41 UTC)"
 "Build Info:"
 "  Official https://julialang.org/ release"
 "Platform Info:"
 "  OS: Linux (x86_64-linux-gnu)"
 "  CPU: 4 × AMD EPYC 7763 64-Core Processor"
 "  WORD_SIZE: 64"
 "  LIBM: libopenlibm"
 "  LLVM: libLLVM-15.0.7 (ORCJIT, znver3)"
 "Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)"
 ""

And with the following package versions

import Pkg; Pkg.status()
Status `~/work/book-la-demo/book-la-demo/docs/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [aaaa29a8] Clustering v0.15.7
  [35d6a980] ColorSchemes v3.24.0
  [3da002f7] ColorTypes v0.11.4
⌅ [c3611d14] ColorVectorSpace v0.9.10
  [717857b8] DSP v0.7.9
  [72c85766] Demos v0.1.0 `~/work/book-la-demo/book-la-demo`
  [e30172f5] Documenter v1.2.1
  [4f61f5a4] FFTViews v0.3.2
  [7a1cc6ca] FFTW v1.8.0
  [587475ba] Flux v0.14.12
⌅ [a09fc81d] ImageCore v0.9.4
  [71a99df6] ImagePhantoms v0.7.2
  [b964fa9f] LaTeXStrings v1.3.1
  [7031d0ef] LazyGrids v0.5.0
  [599c1a8e] LinearMapsAA v0.11.0
  [98b081ad] Literate v2.16.1
  [7035ae7a] MIRT v0.17.0
  [170b2178] MIRTjim v0.23.0
  [eb30cadb] MLDatasets v0.7.14
  [efe261a4] NFFT v0.13.3
  [6ef6ca0d] NMF v1.0.2
  [15e1cf62] NPZ v0.4.3
  [0b1bfda6] OneHotArrays v0.2.5
  [429524aa] Optim v1.9.2
  [91a5bcdd] Plots v1.40.1
  [f27b6e38] Polynomials v4.0.6
  [2913bbd2] StatsBase v0.34.2
  [d6d074c3] VideoIO v1.0.9
  [b77e0a4c] InteractiveUtils
  [37e2e46d] LinearAlgebra
  [44cfe95a] Pkg v1.10.0
  [9a3f8284] Random
Info Packages marked with ⌅ have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated`

This page was generated using Literate.jl.