-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3d6e92
commit c470e8a
Showing
25 changed files
with
647 additions
and
23 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using MultipleViewGeometry, Base.Test | ||
using MultipleViewGeometry.ModuleCostFunction | ||
using MultipleViewGeometry.ModuleTypes | ||
using MultipleViewGeometry.ModuleConstraints | ||
using MultipleViewGeometry.ModuleConstruct | ||
using MultipleViewGeometry.ModuleDraw | ||
using MultipleViewGeometry.ModuleTriangulation | ||
using BenchmarkTools, Compat | ||
using StaticArrays | ||
using MAT, Plots | ||
|
||
# Load MATLAB matrices that represent a pair of images and that contain | ||
# a set of manually matched corresponding points. | ||
file = matopen("./data/teapot.mat") | ||
X = read(file,"pts3D") | ||
X = X[:,1:10:end] | ||
close(file) | ||
|
||
# Fix random seed. | ||
srand(1234) | ||
plotlyjs() | ||
|
||
#π³ = [Point3DH(x,y,z,1.0) for x=-1:0.5:10 for y=-1:0.5:10 for z=2:-0.1:1] | ||
|
||
π³ = [Point3DH(X[1,i],X[2,i],X[3,i],1.0) for i = 1:size(X,2)] | ||
|
||
#X = reinterpret(Float64,map(SVector{4,Float64},π³),(4,length(π³))) | ||
#Z = reinterpret(SVector{4,Float64},(3000,4)) | ||
# reinterpret(SVector{4,Float64}, X, (size(X,2),))# | ||
# reinterpret(SVector{4,Float64}, Z, (size(Z,2),))# | ||
# reinterpret(SVector{4,Float64}, X, (3000,1)) | ||
|
||
# Intrinsic and extrinsic parameters of camera one. | ||
πβ = eye(3) | ||
πβ = eye(3) | ||
πβ = [-250.0, 0.0, 2500.0] | ||
πβ = [0.0, 0.0, 0.0] | ||
πβ = [πβ πβ] | ||
|
||
# Intrinsic and extrinsic parameters of camera two. | ||
πβ = eye(3) | ||
πβ = eye(3) | ||
πβ = [250.0, 0.0, 2500.0] | ||
πβ = [0.0, 0.0, 0.0] | ||
πβ = [πβ πβ] | ||
|
||
# Camera projection matrices. | ||
πβ = construct(ProjectionMatrix(),πβ,πβ,πβ) | ||
πβ = construct(ProjectionMatrix(),πβ,πβ,πβ) | ||
|
||
# Set of corresponding points. | ||
β³ = project(Pinhole(),πβ,π³) | ||
β³ΚΉ = project(Pinhole(),πβ,π³) | ||
|
||
# Convert the array of Point3DH into a 4 x N matrix to simplify | ||
# the plotting of the data points. | ||
#X = reinterpret(Float64,map(SVector{4,Float64},π³),(4,length(π³))) | ||
|
||
# Visualise the data points | ||
p1 = Plots.plot(X[1,:],X[2,:],X[3,:],seriestype = :scatter, ms=1,grid = false, box = :none, legend = false) | ||
draw!(WorldCoordinateSystem3D(), 450, p1) | ||
draw!(Camera3D(), πβ, πβ, πβ, 250, p1) | ||
draw!(Camera3D(), πβ, πβ, πβ, 250, p1) | ||
|
||
|
||
# Plot the projections of the point cloud in the image pair. | ||
p2 = Plots.plot(); | ||
for n = 1:length(β³) | ||
m = β³[n] | ||
Plots.plot!([m[1]],[m[2]], grid = false, box = :none, legend = false, | ||
seriestype = :scatter, ms = 2, markercolor=:Black, | ||
aspect_ratio = :equal) | ||
end | ||
|
||
p3 = Plots.plot(); | ||
for n = 1:length(β³ΚΉ) | ||
mΚΉ = β³ΚΉ[n] | ||
Plots.plot!([mΚΉ[1]],[mΚΉ[2]], grid = false, box = :none, legend = false, | ||
seriestype = :scatter, ms = 2, markercolor=:Black, | ||
aspect_ratio = :equal) | ||
end | ||
|
||
# Visualise the 3D point cloud, as well as the projected images. | ||
l = @layout [ a; [b c] ] | ||
p4 = Plots.plot(p1,p2, p3, layout = l) | ||
|
||
|
||
# π΄ = triangulate(DirectLinearTransform(),πβ,πβ,(β³,β³ΚΉ)) | ||
# | ||
# Y = reinterpret(Float64,map(SVector{4,Float64},π΄),(4,length(π΄))) | ||
# # Visualise the data points | ||
# p5 = Plots.plot(Y[1,:],Y[2,:],Y[3,:],seriestype = :scatter, ms=1,grid = false, box = :none, legend = false) | ||
# draw!(WorldCoordinateSystem3D(), 450, p1) | ||
# draw!(Camera3D(), πβ, πβ, πβ, 250, p1) | ||
# draw!(Camera3D(), πβ, πβ, πβ, 250, p1) | ||
|
||
# Estimate of the fundamental matrix and the true fundamental matrix. | ||
π = estimate(FundamentalMatrix(), DirectLinearTransform(), (β³, β³ΚΉ)) | ||
π = construct(EssentialMatrix(), π , πβ, πβ) | ||
|
||
πβ, πβ = construct(ProjectionMatrix(), π, (β³, β³ΚΉ)) | ||
|
||
π΄ = triangulate(DirectLinearTransform(),πβ,πβ,(β³,β³ΚΉ)) | ||
Y = reinterpret(Float64,map(SVector{4,Float64},π΄),(4,length(π΄))) | ||
# Visualise the data points | ||
p5 = Plots.plot(Y[1,:],Y[2,:],Y[3,:],seriestype = :scatter, ms=1,grid = false, box = :none, legend = false) | ||
draw!(WorldCoordinateSystem3D(), 450/1000, p1) | ||
# draw!(Camera3D(), πβ, πβ[1:3,1:3], πβ[:,4], 250/1000, p1) | ||
# draw!(Camera3D(), πβ, πβ[1:3,1:3], πβ[:,4], 250/1000, p1) | ||
# draw!(Camera3D(), πβ, πβ, πβ, 250, p1) | ||
# draw!(Camera3D(), πβ, πβ, πβ, 250, p1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ModuleConstraints | ||
using MultipleViewGeometry.ModuleTypes, MultipleViewGeometry.ModuleMathAliases, MultipleViewGeometry.ModuleOperators | ||
using StaticArrays | ||
export satisfy, EpipolarConstraint, Constraint | ||
|
||
include("constraints.jl") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
abstract type Constraint end | ||
|
||
type EpipolarConstraint <: Constraint | ||
end | ||
|
||
# Triangulation from Two Views Revisited: Hartley-Sturm vs. Optimal Correction | ||
function satisfy(entity::FundamentalMatrix, constraint::EpipolarConstraint, π ::AbstractArray, π::Tuple{AbstractArray, Vararg{AbstractArray}}) | ||
β³, β³ΚΉ = π | ||
πͺ = similar(β³) | ||
πͺΚΉ = similar(β³ΚΉ) | ||
|
||
N = length(β³) | ||
πβ = SMatrix{3,3,Float64,3^2}([1 0 0; 0 1 0; 0 0 0]) | ||
|
||
I = 10 | ||
for n = 1:N | ||
π¦ = β³[n] | ||
π¦ΚΉ = β³ΚΉ[n] | ||
π¦β = init_correction_view_1(π , π¦, π¦ΚΉ, πβ) | ||
π¦βΚΉ= init_correction_view_2(π , π¦, π¦ΚΉ, πβ) | ||
for i = 1:I | ||
π¦β = π¦ - π¦β | ||
π¦βΚΉ = π¦ΚΉ - π¦βΚΉ | ||
π¦β = update_correction_view_1(π , π¦, π¦β, π¦β, π¦ΚΉ, π¦βΚΉ, π¦βΚΉ, πβ) | ||
π¦βΚΉ = update_correction_view_2(π , π¦, π¦β, π¦β, π¦ΚΉ, π¦βΚΉ, π¦βΚΉ, πβ) | ||
end | ||
πͺ[n] = π¦β | ||
πͺΚΉ[n] = π¦βΚΉ | ||
end | ||
πͺ ,πͺΚΉ | ||
end | ||
|
||
function init_correction_view_1(π ::AbstractArray, π¦::AbstractVector, π¦ΚΉ::AbstractVector, πβ::AbstractArray) | ||
π¦ - dot(π¦,π *π¦ΚΉ)*πβ*π *π¦ΚΉ / ( dot(π *π¦ΚΉ,πβ*π *π¦ΚΉ) + dot(π '*π¦, πβ*π '*π¦) ) | ||
end | ||
|
||
function init_correction_view_2(π ::AbstractArray, π¦::AbstractVector, π¦ΚΉ::AbstractVector, πβ::AbstractArray) | ||
π¦ΚΉ - dot(π¦,π *π¦ΚΉ)*πβ*π '*π¦ / ( dot(π *π¦ΚΉ,πβ*π *π¦ΚΉ) + dot(π '*π¦, πβ*π '*π¦) ) | ||
end | ||
|
||
function update_correction_view_1(π ::AbstractArray, π¦::AbstractVector, π¦β::AbstractVector, π¦β::AbstractVector, π¦ΚΉ::AbstractVector, π¦βΚΉ::AbstractVector, π¦βΚΉ::AbstractVector, πβ::AbstractArray) | ||
π¦ - ( ( dot(π¦β,π *π¦βΚΉ) + dot(π *π¦βΚΉ, π¦β) + dot(π '*π¦β, π¦βΚΉ) ) * πβ*π *π¦βΚΉ) / (dot(π *π¦βΚΉ, πβ*π *π¦βΚΉ) + dot(π '*π¦β, πβ*π '*π¦β) ) | ||
end | ||
|
||
function update_correction_view_2(π ::AbstractArray, π¦::AbstractVector, π¦β::AbstractVector, π¦β::AbstractVector, π¦ΚΉ::AbstractVector, π¦βΚΉ::AbstractVector, π¦βΚΉ::AbstractVector, πβ::AbstractArray) | ||
π¦ΚΉ - ( ( dot(π¦β,π *π¦βΚΉ) + dot(π *π¦βΚΉ, π¦β) + dot(π '*π¦β, π¦βΚΉ) ) * πβ*π '*π¦β) / (dot(π *π¦βΚΉ, πβ*π *π¦βΚΉ) + dot(π '*π¦β, πβ*π '*π¦β) ) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
module ModuleConstruct | ||
using MultipleViewGeometry | ||
using MultipleViewGeometry.ModuleTypes, MultipleViewGeometry.ModuleMathAliases, MultipleViewGeometry.ModuleOperators | ||
using MultipleViewGeometry.ModuleProjection | ||
using StaticArrays | ||
export construct | ||
|
||
include("construct_projectionmatrix.jl") | ||
include("construct_fundamentalmatrix.jl") | ||
include("construct_essentialmatrix.jl") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function construct( e::EssentialMatrix, π ::AbstractArray, πβ::AbstractArray, πβ::AbstractArray) | ||
if (size(πβ) != (3,3)) || (size(πβ) != (3,3)) | ||
throw(ArgumentError("Expect 3 x 3 calibration matrices.")) | ||
end | ||
if (size(π ) != (3,3)) | ||
throw(ArgumentError("Expect 3 x 3 fundamental matrix.")) | ||
end | ||
# Equation 9.12 Chapter 9 from Hartley & Zisserman | ||
π = πβ'*π *πβ | ||
MMatrix{3,3,Float64,3*3}(π) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.