@@ -33,11 +33,15 @@ int main()
33
33
vs_handle display_00 = vs_gui_add_display (" Stabilized Image" ,0 ,0 ,display_size);
34
34
vs_handle display_01 = vs_gui_add_display (" Captured Image" ,0 ,display_size,display_size);
35
35
36
+ // The variables used to store how the current image should be shifted to align it with the stored key-frame
37
+ // Note this is a very simple method in that it only aligns via translation, and does not account for rotation & scaling
36
38
int alignment_x = 0 ; int alignment_y = 0 ;
37
- int vel_x = 0 ; int vel_y = 0 ;
39
+
40
+ // Varibles used to keep track of alignment "velocity", allows more robust alignment under motion by estimating the new alignment each frame using this "velocity"
41
+ int alignment_vel_x = 0 ; int alignment_vel_y = 0 ;
38
42
39
- int use_inertia = 1 ;
40
- vs_gui_add_switch (" use_inertia " ,1 ,&use_inertia );
43
+ int use_velocity = 1 ;
44
+ vs_gui_add_switch (" use_velocity " ,1 ,&use_velocity );
41
45
42
46
bool set_anchor_image = false ;
43
47
auto gui_btn_set_anchor_image = vs_gui_add_button (" set_anchor_image" );
@@ -49,6 +53,12 @@ int main()
49
53
int edge_threshold = 12 ;
50
54
vs_gui_add_slider (" edge_threshold x" ,0 ,127 ,edge_threshold,&edge_threshold);
51
55
56
+ int alignment_strength = 0 ;
57
+ int minimum_alignment_strength = 2000 ;
58
+ vs_gui_add_slider (" minimum_alignment_strength" ,0 ,4000 ,minimum_alignment_strength,&minimum_alignment_strength);
59
+
60
+ int show_current_edge_image = 1 ;
61
+ vs_gui_add_switch (" show_captured_image" ,1 ,&show_current_edge_image);
52
62
53
63
// Frame Loop
54
64
while (1 )
@@ -58,6 +68,13 @@ int main()
58
68
vs_disable_frame_trigger ();
59
69
vs_frame_loop_control ();
60
70
71
+ if (alignment_strength < minimum_alignment_strength)
72
+ {
73
+ set_anchor_image = true ;
74
+ vs_post_text (" insufficient edges" );
75
+ }
76
+ alignment_strength = 0 ;
77
+
61
78
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62
79
// CAPTURE FRAME
63
80
@@ -120,19 +137,13 @@ int main()
120
137
OR (DREG_combined_edges,DREG_horizontal_edges);// OR WITH HORIZONTAL EDGE RESULT
121
138
scamp5_kernel_end ();
122
139
123
-
124
- int position_threshold = 48 ;
125
- if (abs (alignment_x) > position_threshold || abs (alignment_y) > position_threshold)
126
- {
127
- set_anchor_image = true ;
128
- }
129
140
if (set_anchor_image)
130
141
{
131
142
set_anchor_image = false ;
132
143
alignment_x = 0 ;
133
144
alignment_y = 0 ;
134
- vel_x = 0 ;
135
- vel_y = 0 ;
145
+ alignment_vel_x = 0 ;
146
+ alignment_vel_y = 0 ;
136
147
137
148
scamp5_kernel_begin ();
138
149
MOV (DREG_keyframe_edges,DREG_combined_edges);
@@ -147,26 +158,26 @@ int main()
147
158
// IMAGE ALIGNMENT
148
159
// This code updates the "position" at which the captured image aligns with the stored key-frame
149
160
150
- if (use_inertia )
161
+ if (use_velocity )
151
162
{
152
- alignment_x += vel_x ;
153
- alignment_y += vel_y ;
163
+ alignment_x += alignment_vel_x ;
164
+ alignment_y += alignment_vel_y ;
154
165
}
155
166
156
- // Copy the latest edge frame and shift it to the previous position determined to align with the edge key-frame
167
+ // Copy the latest edge frame and shift it to the position determined to align with the edge key-frame
157
168
scamp5_kernel_begin ();
158
169
MOV (S4,S1);
159
170
scamp5_kernel_end ();
160
171
scamp5_shift (S4,alignment_x,alignment_y);
161
172
162
- // The directions to shift and test for better alignment (i.e. noshift,left,right,down,up)
173
+ // The shifts to test and see if they improve image alignment with the keyframe
163
174
int test_shift_directions[][5 ] =
164
175
{
165
- {0 ,0 },
166
- {1 ,0 },
167
- {-1 , 0 },
168
- {0 ,1 },
169
- {0 , -1 },
176
+ {0 ,0 },// no shift
177
+ {1 ,0 },// left
178
+ {-1 , 0 },// right
179
+ {0 ,1 },// down
180
+ {0 , -1 },// up
170
181
};
171
182
172
183
// Array to store the global summations for each tested shift
@@ -175,14 +186,14 @@ int main()
175
186
// Test each shift
176
187
for (int n = 0 ; n < 5 ; n++)
177
188
{
178
- int x = test_shift_directions[n][0 ];
179
- int y = test_shift_directions[n][1 ];
189
+ int shift_x = test_shift_directions[n][0 ];
190
+ int shift_y = test_shift_directions[n][1 ];
180
191
181
192
// Copy the shifted edge frame & apply the test shift to it
182
193
scamp5_kernel_begin ();
183
194
MOV (S6,S4);
184
195
scamp5_kernel_end ();
185
- scamp5_shift (S6,x,y );
196
+ scamp5_shift (S6,shift_x,shift_y );
186
197
187
198
scamp5_in (F,127 );
188
199
scamp5_kernel_begin ();
@@ -193,38 +204,57 @@ int main()
193
204
mov (D,F);// D = 127 in PEs where the edges of
194
205
ALL ();
195
206
scamp5_kernel_end ();
207
+
196
208
direction_global_sums[n] = scamp5_global_sum_64 (D);
209
+
210
+ // The highest value sum will have the best alignment, ie. the most overlap between the edge from the current frame and the key-frame
211
+ if (alignment_strength < direction_global_sums[n])
212
+ {
213
+ alignment_strength = direction_global_sums[n];
214
+ }
197
215
}
198
216
199
217
if (direction_global_sums[0 ] > direction_global_sums[1 ] &&
200
- direction_global_sums[0 ] > direction_global_sums[2 ] &&
201
- direction_global_sums[0 ] > direction_global_sums[3 ] &&
202
- direction_global_sums[0 ] > direction_global_sums[4 ])
218
+ direction_global_sums[0 ] > direction_global_sums[2 ] &&
219
+ direction_global_sums[0 ] > direction_global_sums[3 ] &&
220
+ direction_global_sums[0 ] > direction_global_sums[4 ])
203
221
{
204
-
222
+ // The best alignment was when no shift was applied at all so nothing need be updated
205
223
}
206
224
else
207
225
{
208
- if (direction_global_sums[1 ] < direction_global_sums[2 ])
209
- {
210
- alignment_x--;
211
- vel_x--;
212
- }
213
- else
214
- {
215
- alignment_x++;
216
- vel_x++;
217
- }
226
+ // Determine which horizontal and vertical shift directions that improved alignment, and update the alignment & "velocity"
218
227
219
- if (direction_global_sums[3 ] < direction_global_sums[4 ])
228
+ // Did either shifting left or right result in a better alignment than not shifting at all?
229
+ if (direction_global_sums[0 ] < direction_global_sums[1 ] || direction_global_sums[0 ] < direction_global_sums[2 ])
220
230
{
221
- alignment_y--;
222
- vel_y--;
231
+ // Compare the summation from shifting left and right
232
+ if (direction_global_sums[1 ] < direction_global_sums[2 ])
233
+ {
234
+ alignment_x--;
235
+ alignment_vel_x--;
236
+ }
237
+ else
238
+ {
239
+ alignment_x++;
240
+ alignment_vel_x++;
241
+ }
223
242
}
224
- else
243
+
244
+ // Did either shifting left or right result in a better alignment than not shifting at all?
245
+ if (direction_global_sums[0 ] < direction_global_sums[3 ] || direction_global_sums[0 ] < direction_global_sums[4 ])
225
246
{
226
- alignment_y++;
227
- vel_y++;
247
+ // Compare the summation from shifting up and down
248
+ if (direction_global_sums[3 ] < direction_global_sums[4 ])
249
+ {
250
+ alignment_y--;
251
+ alignment_vel_y--;
252
+ }
253
+ else
254
+ {
255
+ alignment_y++;
256
+ alignment_vel_y++;
257
+ }
228
258
}
229
259
}
230
260
@@ -245,11 +275,11 @@ int main()
245
275
scamp5_kernel_end ();
246
276
scamp5_output_image (S6,display_00);
247
277
248
- scamp5_output_image (S1,display_01);
249
-
250
-
251
-
252
- // scamp5_output_image(DREG_keyframe_edges,display_02 );
278
+ if (show_current_edge_image)
279
+ {
280
+ scamp5_output_image (S1,display_01);
281
+ }
282
+ vs_post_text ( " alignment (%d,%d), velocity (%d,%d), strength %d \n " ,alignment_x,alignment_y,alignment_vel_x,alignment_vel_y,alignment_strength );
253
283
}
254
284
int image_output_time_microseconds = output_timer.get_usec ();// get the time taken for image output
255
285
0 commit comments