// so we can just subtract away the mean difference here.
du -= grad_sum * (warped_sum - template_sum) * (1.0 / (patch_size * patch_size));
- u += (H_inv * du) * inv_image_size;
+ // Do the actual update.
+ u -= (H_inv * du) * inv_image_size;
}
- // Reject if we moved too far.
- if (length((u - initial_u) * image_size) > patch_size) {
+ // Reject if we moved too far. Also reject if the patch goes out-of-bounds
+ // (the paper does not mention this, but the code does, and it seems to be
+ // critical to avoid really bad behavior at the edges).
+ if ((length((u - initial_u) * image_size) > patch_size) ||
+ u.x * image_size.x < -(patch_size * 0.5f) ||
+ (1.0 - u.x) * image_size.x < -(patch_size * 0.5f) ||
+ u.y * image_size.y < -(patch_size * 0.5f) ||
+ (1.0 - u.y) * image_size.y < -(patch_size * 0.5f)) {
u = initial_u;
}