Skip to content

Commit 91c2d02

Browse files
committed
update test with z cost
1 parent 0ef0dd8 commit 91c2d02

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/entities/GeneralTypes.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
struct PixelIndex{VALID, T <: Real}
44
row::T
55
col::T
6+
depth::T
67
end
7-
PixelIndex(u::T,v::T;valid::Bool=true) where {T <: Real} = PixelIndex{valid,T}(u,v)
8+
PixelIndex(u::T, v::T; valid::Bool=true, depth = T(0)) where {T <: Real} = PixelIndex{valid,T}(u, v, depth)
89

910
function Base.getindex(p::PixelIndex,i::Int)
1011
if i === 1
1112
p.row
1213
elseif i === 2
1314
p.col
15+
elseif i === 3
16+
p.depth
1417
else
1518
DomainError("Camera only has rows and columns, cannot index to $i")
1619
end
1720
end
1821

22+
1923
const Vector2 = SVector{2, Float64}
2024
const Point3 = SVector{3, Float64}
2125
const Vector3 = SVector{3, Float64}

src/services/CameraServices.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,8 @@ function projectHomogeneous(
106106
col = x * fx_z + pp_w(cam) # add center to get PixelIndex
107107
row = y * fy_z + pp_h(cam)
108108
# infront or behind
109-
if (w==0&&0<z) || 0 < (z/w)
110-
PixelIndex(row,col)
111-
else
112-
PixelIndex(0.,0.; valid=false)
113-
end
109+
depth=z/w
110+
PixelIndex(row, col; depth, valid = (w==0&&0<z) || 0 < depth)
114111
end
115112
# # right cam
116113
# u2 = (x - w*baseline) * fz + center[1]

test/multiview_manifolds.jl

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ M = SpecialEuclidean(3)
1717
@testset "Multiview optimization of point in front of 2 cameras" begin
1818
##
1919

20-
cam = CameraCalibration()
20+
cam = CameraModels.CameraCalibration()
2121

22-
obs1 = PixelIndex(240, 320)
23-
obs2 = PixelIndex(240, 315)
22+
obs1 = CameraModels.PixelIndex(240, 320)
23+
obs2 = CameraModels.PixelIndex(240, 315)
2424

2525
w_T_c1 = ArrayPartition([0; 0 ;0.],[0 0 1; -1 0 0; 0 -1 0.])
2626
w_T_c2 = ArrayPartition([0;-0.1;0.],[0 0 1; -1 0 0; 0 -1 0.])
@@ -43,14 +43,22 @@ w_T_c2 = ArrayPartition([0;-0.1;0.],[0 0 1; -1 0 0; 0 -1 0.])
4343

4444
##
4545

46-
function cost(w_Ph)
47-
c1_Ph = affine_matrix(M, inv(M,w_T_c1))*w_Ph |> SVector{4}
48-
pre1 = CameraModels.projectHomogeneous(cam,c1_Ph)
46+
function projectPointFromWorld(cam, c_H_w, w_Ph)
47+
c_Ph = c_H_w*w_Ph |> SVector{4}
48+
CameraModels.projectHomogeneous(cam,c_Ph)
49+
end
4950

50-
c2_Ph = affine_matrix(M, inv(M,w_T_c2))*w_Ph |> SVector{4}
51-
pre2 = CameraModels.projectHomogeneous(cam,c2_Ph)
52-
53-
(obs1[1]-pre1[1])^2 + (obs1[2]-pre1[2])^2 + (obs2[1]-pre2[1])^2 + (obs2[2]-pre2[2])^2
51+
function cameraResidual(cam, meas, M, w_T_c, w_Ph, κ=1000)
52+
pred = projectPointFromWorld(cam, inv(affine_matrix(M,w_T_c)), w_Ph)
53+
# experimental cost function to try force bad reprojects in front of the camera during optimization
54+
κ*(abs(pred.depth) - pred.depth)^2 + (meas[1]-pred[1])^2 + (meas[2]-pred[2])^2
55+
end
56+
57+
function cost(w_Ph)
58+
cameraResidual(cam, obs1, M, w_T_c1, w_Ph) + cameraResidual(cam, obs2, M, w_T_c2, w_Ph)
59+
# pre1 = projectPointFromWorld(cam, inv(affine_matrix(M,w_T_c1)), w_Ph)
60+
# pre2 = projectPointFromWorld(cam, inv(affine_matrix(M,w_T_c2)), w_Ph)
61+
# (obs1[1]-pre1[1])^2 + (obs1[2]-pre1[2])^2 + (obs2[1]-pre2[1])^2 + (obs2[2]-pre2[2])^2
5462
end
5563

5664
##
@@ -83,10 +91,8 @@ w_Res = Optim.optimize(
8391

8492
##
8593

86-
87-
@show w_Res.minimizer |> toNonhomogeneous
88-
89-
@test isapprox([10.56;0;0], toNonhomogeneous(w_Res.minimizer); atol=1e-3)
94+
@show w_P3 = w_Res.minimizer |> CameraModels.toNonhomogeneous
95+
@test isapprox([10.56;0;0], w_P3; atol=1e-3)
9096

9197
##
9298
end

0 commit comments

Comments
 (0)