]> git.sesse.net Git - nageru/blobdiff - motion_search.frag
Reject motion search if we go beyond the image boundary; seems to fix most of the...
[nageru] / motion_search.frag
index d9d1f4eb4997c7650fb0f4902acf2c42b5ff4e4c..262dca53a9bc8aaae7abb30e83b85fdd90c01173 100644 (file)
@@ -133,11 +133,18 @@ void main()
                // 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;
        }