Fixed trim ratio estimation for the case of homographies motion model (videostab)
authorAlexey Spizhevoy <no@email>
Mon, 23 Apr 2012 06:42:55 +0000 (06:42 +0000)
committerAlexey Spizhevoy <no@email>
Mon, 23 Apr 2012 06:42:55 +0000 (06:42 +0000)
modules/videostab/src/motion_stabilizing.cpp
samples/cpp/videostab.cpp

index cb5e7d1..31f0422 100644 (file)
@@ -616,11 +616,15 @@ static inline bool isGoodMotion(const float M[], float w, float h, float dx, flo
 {
     Point2f pt[4] = {Point2f(0,0), Point2f(w,0), Point2f(w,h), Point2f(0,h)};
     Point2f Mpt[4];
+    float z;
 
     for (int i = 0; i < 4; ++i)
     {
         Mpt[i].x = M[0]*pt[i].x + M[1]*pt[i].y + M[2];
         Mpt[i].y = M[3]*pt[i].x + M[4]*pt[i].y + M[5];
+        z = M[6]*pt[i].x + M[7]*pt[i].y + M[8];
+        Mpt[i].x /= z;
+        Mpt[i].y /= z;
     }
 
     pt[0] = Point2f(dx, dy);
@@ -640,6 +644,9 @@ static inline void relaxMotion(const float M[], float t, float res[])
     res[3] = M[3]*(1.f-t);
     res[4] = M[4]*(1.f-t) + t;
     res[5] = M[5]*(1.f-t);
+    res[6] = M[6]*(1.f-t);
+    res[7] = M[7]*(1.f-t);
+    res[8] = M[8]*(1.f-t) + t;
 }
 
 
@@ -651,11 +658,12 @@ Mat ensureInclusionConstraint(const Mat &M, Size size, float trimRatio)
     const float h = static_cast<float>(size.height);
     const float dx = floor(w * trimRatio);
     const float dy = floor(h * trimRatio);
-    const float srcM[6] =
+    const float srcM[] =
             {M.at<float>(0,0), M.at<float>(0,1), M.at<float>(0,2),
-             M.at<float>(1,0), M.at<float>(1,1), M.at<float>(1,2)};
+             M.at<float>(1,0), M.at<float>(1,1), M.at<float>(1,2),
+             M.at<float>(2,0), M.at<float>(2,1), M.at<float>(2,2)};
 
-    float curM[6];
+    float curM[9];
     float t = 0;
     relaxMotion(srcM, t, curM);
     if (isGoodMotion(curM, w, h, dx, dy))
@@ -687,11 +695,15 @@ float estimateOptimalTrimRatio(const Mat &M, Size size)
 
     Point2f pt[4] = {Point2f(0,0), Point2f(w,0), Point2f(w,h), Point2f(0,h)};
     Point2f Mpt[4];
+    float z;
 
     for (int i = 0; i < 4; ++i)
     {
         Mpt[i].x = M_(0,0)*pt[i].x + M_(0,1)*pt[i].y + M_(0,2);
         Mpt[i].y = M_(1,0)*pt[i].x + M_(1,1)*pt[i].y + M_(1,2);
+        z = M_(2,0)*pt[i].x + M_(2,1)*pt[i].y + M_(2,2);
+        Mpt[i].x /= z;
+        Mpt[i].y /= z;
     }
 
     float l = 0, r = 0.5f;
index 3420941..0563d89 100644 (file)
@@ -95,15 +95,15 @@ void printHelp()
             "      (i.e. sqrt(radius)).\n"
             "  -lps, --lin-prog-stab=(yes|no)\n"
             "      Turn on/off linear programming based stabilization method.\n"
-            "  --lp-trim-ratio=(<float_number>|auto)\n"
+            "  --lps-trim-ratio=(<float_number>|auto)\n"
             "      Trimming ratio used in linear programming based method.\n"
-            "  --lp-w1=(<float_number>|1)\n"
+            "  --lps-w1=(<float_number>|1)\n"
             "      1st derivative weight. The default is 1.\n"
-            "  --lp-w2=(<float_number>|10)\n"
+            "  --lps-w2=(<float_number>|10)\n"
             "      2nd derivative weight. The default is 10.\n"
-            "  --lp-w3=(<float_number>|100)\n"
+            "  --lps-w3=(<float_number>|100)\n"
             "      3rd derivative weight. The default is 100.\n"
-            "  --lp-w4=(<float_number>|100)\n"
+            "  --lps-w4=(<float_number>|100)\n"
             "      Non-translation motion components weight. The default is 100.\n\n"
             "  --deblur=(yes|no)\n"
             "      Do deblurring.\n"
@@ -185,11 +185,11 @@ int main(int argc, const char **argv)
                 "{ r | radius | 15 | }"
                 "{ | stdev | auto | }"
                 "{ lps | lin-prog-stab | no | }"
-                "{ | lp-trim-ratio | auto | }"
-                "{ | lp-w1 | 1 | }"
-                "{ | lp-w2 | 10 | }"
-                "{ | lp-w3 | 100 | }"
-                "{ | lp-w4 | 100 | }"
+                "{ | lps-trim-ratio | auto | }"
+                "{ | lps-w1 | 1 | }"
+                "{ | lps-w2 | 10 | }"
+                "{ | lps-w3 | 100 | }"
+                "{ | lps-w4 | 100 | }"
                 "{ | deblur | no | }"
                 "{ | deblur-sens | 0.1 | }"
                 "{ et | est-trim | yes | }"
@@ -260,11 +260,11 @@ int main(int argc, const char **argv)
             {
                 LpMotionStabilizer *stab = new LpMotionStabilizer();
                 stab->setFrameSize(Size(source->width(), source->height()));
-                stab->setTrimRatio(arg("lp-trim-ratio") == "auto" ? argf("trim-ratio") : argf("lp-trim-ratio"));
-                stab->setWeight1(argf("lp-w1"));
-                stab->setWeight2(argf("lp-w2"));
-                stab->setWeight3(argf("lp-w3"));
-                stab->setWeight4(argf("lp-w4"));
+                stab->setTrimRatio(arg("lps-trim-ratio") == "auto" ? argf("trim-ratio") : argf("lps-trim-ratio"));
+                stab->setWeight1(argf("lps-w1"));
+                stab->setWeight2(argf("lps-w2"));
+                stab->setWeight3(argf("lps-w3"));
+                stab->setWeight4(argf("lps-w4"));
                 twoPassStabilizer->setMotionStabilizer(stab);
             }
             else if (arg("stdev") == "auto")