Debian Codegraph @

mrgingham-rotate-corners

raw
1 #!/bin/bash
2
3 # Takes a processed corners.vnl result, and rotates some of the chessboard
4 # observations. Used if some of the cameras in the calibrated set were mounted
5 # sideways or upside-down
6
7
8
9 # Option parsing is the adapted sample in
10 # /usr/share/doc/util-linux/examples/getopt-example.bash
11
12 usage="
13 < corners.vnl \\
14 mrgingham-rotate-corners [--gridn N] \\
15 --90 REGEX_CAM_90deg \\
16 --180 REGEX_CAM_180deg \\
17 --270 REGEX_CAM_270deg \\
18 [... more rotation selections ...] \\
19 > corners-rotated.vnl
20
21 Adjust mrgingham corner detections from rotated cameras
22
23 Synopsis:
24
25 # camera A is rightside-up
26 # camera B is mounted sideways
27 # cameras C,D are upside-down
28 mrgingham --gridn N \\
29 'frame*-cameraA.jpg' \\
30 'frame*-cameraB.jpg' \\
31 'frame*-cameraC.jpg' \\
32 'frame*-cameraD.jpg' | \\
33 mrgingham-rotate-corners --gridn N \\
34 --90 cameraB --180 'camera[CD]'
35
36 The mrgingham chessboard detector finds a chessboard in an image, but it has no
37 way to know whether the detected chessboard was upside-down or otherwise
38 rotated: the chessboard itself has no detectable marking to make this clear. In
39 the usual case, the cameras as all mounted in the same orientation, so they all
40 detect the same orientation of the chessboard, and there is no problem. However,
41 if some cameras are mounted sideways or upside-down, the sequence of corners
42 will correspond to different corners between the cameras with different
43 orientations. This can be addressed by this tool. This tool ingests mrgingham
44 detections, and outputs them after correcting the chessboard observations
45 produced by rotated cameras.
46
47 Each rotation option is an awk regular expression used to select images from
48 specific cameras. The regular expression is tested against the image filenames.
49 Each rotation option may be given multiple times. Any files not matched by any
50 rotation option are passed through unrotated.
51 "
52
53 ARGS=$(getopt -n $0 -l help,gridn:,90:,180:,270: -o "h" -- "$@")
54
55 if [ $? -ne 0 ]; then
56 echo "Usage: $usage" > /dev/stderr
57 exit 1
58 fi
59
60 eval set -- "$ARGS"
61 unset ARGS
62
63
64 gridn=10
65 cam_rotate90=()
66 cam_rotate180=()
67 cam_rotate270=()
68
69 while true; do
70 case "$1" in
71 '--help'|'-h')
72 echo "Usage: $usage"
73 exit 0
74 ;;
75 '--gridn')
76 gridn=$2
77 shift 2
78 continue
79 ;;
80 '--90')
81 cam_rotate90+=($2)
82 shift 2
83 ;;
84 '--180')
85 cam_rotate180+=($2)
86 shift 2
87 ;;
88 '--270')
89 cam_rotate270+=($2)
90 shift 2
91 ;;
92 '--')
93 shift
94 break
95 ;;
96 *)
97 echo "Error parsing options!" > /dev/stderr
98 exit 1
99 ;;
100 esac
101 done
102
103 if (( $# )); then
104 echo "Extra arguments given" > /dev/stderr
105 echo "Usage: $usage" > /dev/stderr
106 exit 1
107 fi
108
109 cam_rotate90_select_expression="0"
110 for f in ${cam_rotate90[@]}; do
111 cam_rotate90_select_expression+=" || filename ~ \"$f\""
112 done
113 cam_rotate180_select_expression="0"
114 for f in ${cam_rotate180[@]}; do
115 cam_rotate180_select_expression+=" || filename ~ \"$f\""
116 done
117 cam_rotate270_select_expression="0"
118 for f in ${cam_rotate270[@]}; do
119 cam_rotate270_select_expression+=" || filename ~ \"$f\""
120 done
121
122 Nx=$gridn
123 Ny=$gridn
124 Nxy=$((Nx*Ny))
125
126 ICORNER_FUNCTION="
127 if(filename!=filename_prev) {
128
129 N=i+1
130 if(!(N == 0 || N == 1 || N == $Nxy)) {
131 print \"# File '\"filename\"': expected \"$Nxy\" points but received \"N > \"/dev/stderr\";
132 failed=1;
133 exit 1
134 }
135
136 i =0;
137 ix=0;
138 iy=0;
139 filename_prev=filename;
140 }
141 else {
142 i++;
143 ix++;
144 if(ix == $Nx) {
145 ix = 0;
146 iy++;
147 }
148 }
149
150 if($cam_rotate90_select_expression)
151 return ($Nx-1-ix)*$Ny + iy;
152 if($cam_rotate270_select_expression)
153 return ix*$Ny + ($Ny-1-iy);
154 if($cam_rotate180_select_expression)
155 return ($Nx*$Ny-1 - i);
156 return i;
157 "
158
159 END_EXPRESSION="
160 N=i+1
161 if(!failed && !(N == 0 || N == 1 || N == $Nxy)) {
162 print \"# The last file in the data file expected \"$Nxy\" points but received \"N > \"/dev/stderr\";
163 exit 1
164 }
165 "
166
167
168 vnl-filter \
169 --skipcomments \
170 --end "$END_EXPRESSION" \
171 --sub "icorner() { $ICORNER_FUNCTION }" \
172 -p icorner='icorner()',. \
173 | vnl-sort -k filename -k icorner.n \
174 | vnl-filter -p '!icorner'
175
176 exit ${PIPESTATUS[0]}