Repository navigation
Expand file tree
/
Copy pathdemo-co-registration.sh
More file actions
executable file
·309 lines (284 loc) · 11 KB
/
Copy pathdemo-co-registration.sh
File metadata and controls
executable file
·309 lines (284 loc) · 11 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
set -e
echo "=== Terrain Intelligence Generator - Co-registration Demo ==="
echo ""
# Every VICAR tool below runs through tig, which starts and reuses the
# container, mounts this workspace, and translates host paths.
WORKSPACE="$(pwd)/workspace-coreg"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if ! command -v tig &> /dev/null; then
echo "ERROR: tig not found on PATH."
echo ""
echo "Install it with: pip install tig-cli"
exit 1
fi
IMAGE_LIST=""
IMAGES=()
OVERLAP=40
DENSITY=50
GRID_SPACING=15
MAX_RESIDUAL=10
SOLUTION_ID="COREG"
RUN_NAV2=false
print_usage() {
echo "Usage: $0 [OPTIONS] [IMAGE ...]"
echo ""
echo "Co-register a set of overlapping images: tiepoints (marsautotie) ->"
echo "pointing solution (marsnav) -> nav table consumed by the mosaickers."
echo ""
echo "Options:"
echo " --list FILE Text file listing input images, one per line"
echo " --overlap PCT marschkovl minimum overlap percentage (default: $OVERLAP)"
echo " --density N marsautotie tiepoint density; lower = more (default: $DENSITY)"
echo " --grid-spacing N marsautotie candidate grid spacing (default: $GRID_SPACING)"
echo " --max-residual PX marsnav outlier removal threshold (default: $MAX_RESIDUAL)"
echo " --solution-id ID Solution ID written into the nav table (default: $SOLUTION_ID)"
echo " --nav2 Also run marsnav2 (Ceres bundle adjustment) for comparison"
echo ""
echo "Examples:"
echo " $0 --list frames.lis"
echo " $0 --density 100 --max-residual 5 NLB_*.IMG"
echo ""
echo "Requirements:"
echo " - tig-cli installed (pip install tig-cli) and a running Docker daemon"
echo " - Mission calibration on MARS_CONFIG_PATH (see docs/reference/calibration-data.md)"
echo " - Images from the same site with real overlap and usable initial pointing"
echo " - Up to 200 images (marsautotie/marsnav limit)"
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
--list)
[ -z "$2" ] && { echo "ERROR: --list requires a FILE argument"; print_usage; }
IMAGE_LIST="$2"
shift 2
;;
--overlap)
[ -z "$2" ] && { echo "ERROR: --overlap requires a value"; print_usage; }
OVERLAP="$2"
shift 2
;;
--density)
[ -z "$2" ] && { echo "ERROR: --density requires a value"; print_usage; }
DENSITY="$2"
shift 2
;;
--grid-spacing)
[ -z "$2" ] && { echo "ERROR: --grid-spacing requires a value"; print_usage; }
GRID_SPACING="$2"
shift 2
;;
--max-residual)
[ -z "$2" ] && { echo "ERROR: --max-residual requires a value"; print_usage; }
MAX_RESIDUAL="$2"
shift 2
;;
--solution-id)
[ -z "$2" ] && { echo "ERROR: --solution-id requires a value"; print_usage; }
SOLUTION_ID="$2"
shift 2
;;
--nav2)
RUN_NAV2=true
shift
;;
--help|-h)
print_usage
;;
-*)
echo "ERROR: Unknown option: $1"
print_usage
;;
*)
IMAGES+=("$1")
shift
;;
esac
done
# Resolve symlinks as well as relative components: tig resolves a path before
# mapping it to its container location, and an unresolved symlink target is not
# reachable from inside the container.
abspath() {
[ -z "$1" ] && return 0
local dir
readlink -f "$1" 2>/dev/null && return 0
# Fall back to the path as given, so the file checks below report it.
dir=$(cd "$(dirname "$1")" 2>/dev/null && pwd -P) || { echo "$1"; return 0; }
echo "${dir%/}/$(basename "$1")"
}
# tig mounts $HOME at its host path and the rest of the filesystem read-only under
# /host, and it translates paths in arguments but not inside a list file. So the
# list has to carry the path the container will see.
container_path() {
case "$1" in
"$HOME"/*) echo "$1" ;;
*) echo "/host$1" ;;
esac
}
INPUTS=()
if [ -n "$IMAGE_LIST" ]; then
IMAGE_LIST=$(abspath "$IMAGE_LIST")
if [ ! -f "$IMAGE_LIST" ]; then
echo "ERROR: Image list not found: $IMAGE_LIST"
exit 1
fi
while IFS= read -r line || [ -n "$line" ]; do
[ -n "$line" ] || continue
INPUTS+=("$(abspath "$line")")
done < "$IMAGE_LIST"
fi
for image in "${IMAGES[@]}"; do
INPUTS+=("$(abspath "$image")")
done
if [ "${#INPUTS[@]}" -lt 2 ]; then
echo "ERROR: At least two input images are required"
print_usage
fi
for input in "${INPUTS[@]}"; do
if [ ! -f "$input" ]; then
echo "ERROR: Input image not found: $input"
exit 1
fi
done
# Find calibration files using helper script. After argument parsing, so that
# --help and usage errors work without calibration installed.
if [ -f "$SCRIPT_DIR/find-calibration.sh" ]; then
source "$SCRIPT_DIR/find-calibration.sh"
# Guard the call: under 'set -e' a failed lookup would otherwise end the
# script before the help below is printed.
calibration_setup || true
if [ -z "$CALIB_DIR" ] && [ -z "$CALIB_IN_IMAGE" ]; then
echo "ERROR: MARS calibration not found."
echo ""
print_calibration_help
exit 1
fi
else
echo "ERROR: find-calibration.sh not found next to this script."
echo "Set MARS_CONFIG_PATH to a mission calibration directory and retry."
exit 1
fi
if [ -n "${CALIB_IN_IMAGE:-}" ]; then
# Bundled in the image: nothing to mount, and MARS_CONFIG_PATH must stay
# unset, since it is read as a host path to mount.
echo "Using calibration ${CALIB_IN_IMAGE_DESC:-already in the container}: $CALIB_IN_IMAGE"
unset MARS_CONFIG_PATH
CALIB_NOTE="with calibration ${CALIB_IN_IMAGE_DESC:-already in the container}"
else
echo "Using calibration from: $CALIB_DIR"
# tig mounts this read-only at /usr/local/vicar/mars_calib and points the MARS
# programs at it. Resolve it now: tig runs from the workspace directory below,
# where a relative calibration path would no longer resolve.
CALIB_DIR="$(cd "$CALIB_DIR" && pwd)"
export MARS_CONFIG_PATH="$CALIB_DIR"
CALIB_NOTE="with MARS_CONFIG_PATH=$CALIB_DIR exported"
fi
mkdir -p "$WORKSPACE"
echo "✓ Created workspace: $WORKSPACE"
echo ""
cd "$WORKSPACE"
# marstie and marsautotie refuse to overwrite an existing tiepoint file, and a
# leftover nav table would make a failed solve look like a success.
rm -f frames.lis overlap_left.lis overlap_right.lis marschkovl.log \
tiepoints.tpt tiepoints_kept.tpt pointing.nav marsnav.log marsautotie.log \
tiepoints_nav2.tpt pointing_nav2.nav marsnav2.log
for input in "${INPUTS[@]}"; do
container_path "$input" >> frames.lis
done
echo "Input frames: ${#INPUTS[@]}"
echo ""
# Step 1: which frames actually overlap
echo "Step 1: Checking overlap (marschkovl)..."
# marschkovl prints an "Overlap for" line for every pair it evaluates, with the
# measured percentage; only the pairs above the threshold reach the two lists.
tig marschkovl inp=frames.lis out=\( overlap_left.lis overlap_right.lis \) \
overlap="$OVERLAP" > marschkovl.log 2>&1 || CHKOVL_STATUS=$?
if [ -n "${CHKOVL_STATUS:-}" ] || [ ! -f overlap_left.lis ]; then
echo " ❌ ERROR: marschkovl failed. Last lines of $WORKSPACE/marschkovl.log:"
tail -5 marschkovl.log || true
exit 1
fi
echo " ✓ $(wc -l < overlap_left.lis) pair(s) with >= ${OVERLAP}% overlap"
echo " (pairs listed in overlap_left.lis / overlap_right.lis;"
echo " measured percentages for every pair in marschkovl.log)"
echo ""
# Step 2: tiepoints
echo "Step 2: Generating tiepoints (marsautotie)..."
# Log first, then report: these programs write their output file before abending,
# so the exit status is the only reliable signal, as for marschkovl above.
tig marsautotie inp=frames.lis out=tiepoints.tpt \
density="$DENSITY" grid_spacing="$GRID_SPACING" > marsautotie.log 2>&1 || AUTOTIE_STATUS=$?
grep -E "Found [0-9]+ tiepoints|not represented" marsautotie.log || true
if [ -n "${AUTOTIE_STATUS:-}" ]; then
echo " ❌ ERROR: marsautotie failed. Last lines of $WORKSPACE/marsautotie.log:"
tail -5 marsautotie.log || true
exit 1
fi
if [ ! -f tiepoints.tpt ]; then
echo " ❌ ERROR: marsautotie produced no tiepoint file"
echo " Frames may not overlap, or initial pointing may be too far off."
exit 1
fi
echo " ✓ $(grep -c '<tie ' tiepoints.tpt) tiepoints in tiepoints.tpt"
echo ""
# Step 3: pointing solution. MARSNAV requires tiepoints to exist first.
echo "Step 3: Solving for corrected pointing (marsnav)..."
tig marsnav inp=frames.lis out=pointing.nav in_tpt=tiepoints.tpt \
out_tpt=tiepoints_kept.tpt out_solution_id="$SOLUTION_ID" \
-remove max_residual="$MAX_RESIDUAL" max_remove=50 > marsnav.log 2>&1 || NAV_STATUS=$?
grep -E "^Input image .* not represented|not connected" marsnav.log || true
if [ -n "${NAV_STATUS:-}" ]; then
echo " ❌ ERROR: marsnav failed. Last lines of $WORKSPACE/marsnav.log:"
tail -5 marsnav.log || true
exit 1
fi
if [ ! -f pointing.nav ] || [ ! -f tiepoints_kept.tpt ]; then
echo " ❌ ERROR: marsnav produced no nav table or no surviving-tiepoint file"
exit 1
fi
grep -m1 "Commanded mean pixel error" marsnav.log | \
sed 's/^/ initial: /' || true
grep "Final solution mean pixel error" marsnav.log | tail -1 | \
sed 's/^/ final: /' || true
echo " ✓ $(grep -c '<tie ' tiepoints_kept.tpt) of $(grep -c '<tie ' tiepoints.tpt) tiepoints kept"
echo " ✓ Corrected pointing written to pointing.nav (solution id $SOLUTION_ID)"
echo ""
echo " Per-image pointing corrections:"
sed -n '/Img ----corrected----/,$p' marsnav.log | sed 's/^/ /'
echo ""
# Step 4: optional bundle adjustment for comparison
if $RUN_NAV2; then
echo "Step 4: Bundle adjustment (marsnav2)..."
tig marsnav2 inp=frames.lis out=pointing_nav2.nav in_tpt=tiepoints.tpt \
out_tpt=tiepoints_nav2.tpt out_solution_id="$SOLUTION_ID" \
> marsnav2.log 2>&1 || NAV2_STATUS=$?
if [ -n "${NAV2_STATUS:-}" ]; then
echo " ⚠ marsnav2 failed; see $WORKSPACE/marsnav2.log:"
tail -5 marsnav2.log || true
fi
grep -E "initial tracks found|observations tracks|Solution mean error|Solution median error" \
marsnav2.log | sed 's/^/ /' || true
sed -n '/There are disconnected groups/,/^ *$/p' marsnav2.log | sed 's/^/ /' || true
echo ""
fi
echo "=== Demo Complete ==="
echo ""
echo "Generated files in: $WORKSPACE"
echo " - frames.lis : Input list, absolute paths"
echo " - overlap_*.lis : Overlapping pairs found by marschkovl"
echo " - marschkovl.log : Measured overlap percentage for every pair"
echo " - tiepoints.tpt : Tiepoints from marsautotie (XML format)"
echo " - marsautotie.log : Full marsautotie printout"
echo " - tiepoints_kept.tpt : Tiepoints surviving marsnav outlier removal"
echo " - pointing.nav : Corrected pointing, solution id $SOLUTION_ID"
echo " - marsnav.log : Full solver printout, including residuals"
$RUN_NAV2 && echo " - pointing_nav2.nav : Bundle-adjusted pointing (marsnav2)"
echo ""
echo "The nav table is an input to the mosaic programs, which is where the"
echo "corrected pointing takes effect (from $WORKSPACE,"
echo "$CALIB_NOTE):"
echo " tig marsmos inp=frames.lis out=mosaic.img navtable=pointing.nav"
echo " tig marsmap inp=frames.lis out=mosaic.img navtable=pointing.nav"
echo ""
echo "The container stays up for the next run. To remove it:"
echo " tig --shutdown"