blob: dbdad30daf84ebc18246e0c5dd0a3287c98bb295 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# Example program, i.e the public API
#
# When $this has camera slice /slice/ {
# Wish $this displays camera slice $slice
# }
# Callback: extract out a camera slice
When the image library is /imageLib/ &\
the quad library is /quadLib/ &\
the pose library is /poseLib/ &\
the quad changer is /quadChange/ &\
/someone/ wishes /p/ has camera slice &\
camera /cam/ has intrinsics /cameraIntrinsics/ &\
camera /cam/ has frame /frame/ at timestamp /timestamp/ &\
/p/ has quad /q/ {
fn quadChange
package require linalg
namespace import \
::math::linearalgebra::sub \
::math::linearalgebra::norm \
set fWidth [$imageLib Image_width $frame]
set fHeight [$imageLib Image_height $frame]
# Convert quad to camera coordinates
tracy zoneBegin
tracy zoneName "quadChange"
set q [quadChange $q $cam]
set vertices [lmap v [$quadLib vertices $q] {
$poseLib project $cameraIntrinsics \
$fWidth $fHeight $v
}]
tracy zoneEnd
lassign [$quadLib vertices $q] topLeft topRight bottomRight bottomLeft
# These dimensions are in meters. We have to start from these quad
# dimensions (not projected camera-plane dimensions) to maintain
# aspect ratio.
set quadWidth [::math::max [norm [sub $topRight $topLeft]] \
[norm [sub $bottomRight $bottomLeft]]]
set quadHeight [::math::max [norm [sub $bottomRight $topRight]] \
[norm [sub $bottomLeft $topLeft]]]
# Scale quadWidth and quadHeight to appropriate pixel dimensions
# by finding the scale factor from projected vertices
lassign $vertices topLeftProj topRightProj bottomRightProj bottomLeftProj
set projectedWidth [::math::max [norm [sub $topRightProj $topLeftProj]] \
[norm [sub $bottomRightProj $bottomLeftProj]]]
set projectedHeight [::math::max [norm [sub $bottomRightProj $topRightProj]] \
[norm [sub $bottomLeftProj $topLeftProj]]]
# Use the larger dimension to determine pixels per meter
set maxProjected [::math::max $projectedWidth $projectedHeight]
set max3D [::math::max $quadWidth $quadHeight]
set pixelsPerMeter [expr {$maxProjected / $max3D}]
# Scale to pixels while maintaining aspect ratio
set sliceWidth [expr {int($quadWidth * $pixelsPerMeter)}]
set sliceHeight [expr {int($quadHeight * $pixelsPerMeter)}]
tracy zoneBegin
tracy zoneName "warpQuad"
set slice [$imageLib warpQuad $frame $vertices \
$sliceWidth $sliceHeight]
tracy zoneEnd
Claim $p has camera slice $slice \
-destructor [list $imageLib imageFree $slice]
}
# Auto-trigger callback for `when has camera slice` statements
When when /p/ has camera slice /slice/ /lambda/ with environment /e/ {
Wish -nonatomically $p has camera slice
}
# Display a camera slice (for backward compatibility).
When /someone/ wishes /p/ displays camera slice /slice/ {
Wish $p displays image $slice
}
|