Skip to content

Commit 8163bb2

Browse files
committed
Update EX_SIMPLE_IMAGE_STABILIZATION.hpp
1 parent 886f941 commit 8163bb2

File tree

1 file changed

+79
-49
lines changed

1 file changed

+79
-49
lines changed

EXAMPLES/EX_SIMPLE_IMAGE_STABILIZATION.hpp

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ int main()
3333
vs_handle display_00 = vs_gui_add_display("Stabilized Image",0,0,display_size);
3434
vs_handle display_01 = vs_gui_add_display("Captured Image",0,display_size,display_size);
3535

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
3638
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;
3842

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);
4145

4246
bool set_anchor_image = false;
4347
auto gui_btn_set_anchor_image = vs_gui_add_button("set_anchor_image");
@@ -49,6 +53,12 @@ int main()
4953
int edge_threshold = 12;
5054
vs_gui_add_slider("edge_threshold x",0,127,edge_threshold,&edge_threshold);
5155

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);
5262

5363
// Frame Loop
5464
while(1)
@@ -58,6 +68,13 @@ int main()
5868
vs_disable_frame_trigger();
5969
vs_frame_loop_control();
6070

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+
6178
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6279
//CAPTURE FRAME
6380

@@ -120,19 +137,13 @@ int main()
120137
OR(DREG_combined_edges,DREG_horizontal_edges);//OR WITH HORIZONTAL EDGE RESULT
121138
scamp5_kernel_end();
122139

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-
}
129140
if(set_anchor_image)
130141
{
131142
set_anchor_image = false;
132143
alignment_x = 0;
133144
alignment_y = 0;
134-
vel_x = 0;
135-
vel_y = 0;
145+
alignment_vel_x = 0;
146+
alignment_vel_y = 0;
136147

137148
scamp5_kernel_begin();
138149
MOV(DREG_keyframe_edges,DREG_combined_edges);
@@ -147,26 +158,26 @@ int main()
147158
//IMAGE ALIGNMENT
148159
//This code updates the "position" at which the captured image aligns with the stored key-frame
149160

150-
if(use_inertia)
161+
if(use_velocity)
151162
{
152-
alignment_x += vel_x;
153-
alignment_y += vel_y;
163+
alignment_x += alignment_vel_x;
164+
alignment_y += alignment_vel_y;
154165
}
155166

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
157168
scamp5_kernel_begin();
158169
MOV(S4,S1);
159170
scamp5_kernel_end();
160171
scamp5_shift(S4,alignment_x,alignment_y);
161172

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
163174
int test_shift_directions[][5] =
164175
{
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
170181
};
171182

172183
//Array to store the global summations for each tested shift
@@ -175,14 +186,14 @@ int main()
175186
//Test each shift
176187
for (int n = 0; n < 5; n++)
177188
{
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];
180191

181192
//Copy the shifted edge frame & apply the test shift to it
182193
scamp5_kernel_begin();
183194
MOV(S6,S4);
184195
scamp5_kernel_end();
185-
scamp5_shift(S6,x,y);
196+
scamp5_shift(S6,shift_x,shift_y);
186197

187198
scamp5_in(F,127);
188199
scamp5_kernel_begin();
@@ -193,38 +204,57 @@ int main()
193204
mov(D,F);//D = 127 in PEs where the edges of
194205
ALL();
195206
scamp5_kernel_end();
207+
196208
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+
}
197215
}
198216

199217
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])
203221
{
204-
222+
//The best alignment was when no shift was applied at all so nothing need be updated
205223
}
206224
else
207225
{
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"
218227

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])
220230
{
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+
}
223242
}
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])
225246
{
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+
}
228258
}
229259
}
230260

@@ -245,11 +275,11 @@ int main()
245275
scamp5_kernel_end();
246276
scamp5_output_image(S6,display_00);
247277

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);
253283
}
254284
int image_output_time_microseconds = output_timer.get_usec();//get the time taken for image output
255285

0 commit comments

Comments
 (0)