Added new video posiitioning test
authorAlexander Reshetnikov <no@email>
Fri, 23 Mar 2012 08:53:56 +0000 (08:53 +0000)
committerAlexander Reshetnikov <no@email>
Fri, 23 Mar 2012 08:53:56 +0000 (08:53 +0000)
modules/highgui/test/test_video_pos.cpp [new file with mode: 0755]

diff --git a/modules/highgui/test/test_video_pos.cpp b/modules/highgui/test/test_video_pos.cpp
new file mode 100755 (executable)
index 0000000..b1e401f
--- /dev/null
@@ -0,0 +1,167 @@
+/*M///////////////////////////////////////////////////////////////////////////////////////\r
+//\r
+//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
+//\r
+//  By downloading, copying, installing or using the software you agree to this license.\r
+//  If you do not agree to this license, do not download, install,\r
+//  copy or use the software.\r
+//\r
+//\r
+//                          License Agreement\r
+//                For Open Source Computer Vision Library\r
+//\r
+// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
+// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
+// Third party copyrights are property of their respective owners.\r
+//\r
+// Redistribution and use in source and binary forms, with or without modification,\r
+// are permitted provided that the following conditions are met:\r
+//\r
+//   * Redistribution's of source code must retain the above copyright notice,\r
+//     this list of conditions and the following disclaimer.\r
+//\r
+//   * Redistribution's in binary form must reproduce the above copyright notice,\r
+//     this list of conditions and the following disclaimer in the documentation\r
+//     and/or other materials provided with the distribution.\r
+//\r
+//   * The name of the copyright holders may not be used to endorse or promote products\r
+//     derived from this software without specific prior written permission.\r
+//\r
+// This software is provided by the copyright holders and contributors "as is" and\r
+// any express or implied warranties, including, but not limited to, the implied\r
+// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
+// In no event shall the Intel Corporation or contributors be liable for any direct,\r
+// indirect, incidental, special, exemplary, or consequential damages\r
+// (including, but not limited to, procurement of substitute goods or services;\r
+// loss of use, data, or profits; or business interruption) however caused\r
+// and on any theory of liability, whether in contract, strict liability,\r
+// or tort (including negligence or otherwise) arising in any way out of\r
+// the use of this software, even if advised of the possibility of such damage.\r
+//\r
+//M*/\r
+\r
+#include "test_precomp.hpp"\r
+#include "opencv2/highgui/highgui.hpp"\r
+\r
+#ifdef HAVE_FFMPEG\r
+\r
+#include "ffmpeg_codecs.hpp"\r
+\r
+using namespace cv;\r
+using namespace std;\r
+\r
+class CV_PositioningTest : public cvtest::BaseTest\r
+{\r
+public:\r
+    void CreateTestVideo(const string& format, int codec);\r
+    void run(int);\r
+};\r
+\r
+void CV_PositioningTest::CreateTestVideo(const string& format, int codec)\r
+{\r
+ const size_t frame_count = 2500;\r
+\r
+ stringstream s; s << codec;\r
+\r
+ cv::VideoWriter writer(ts->get_data_path()+"video/test_video_"+s.str()+format, codec, 25, cv::Size(640, 480), false);\r
+\r
+ for (size_t i = 0; i < frame_count; ++i)\r
+ {\r
+   cv::Mat mat(480, 640, CV_8UC1);\r
+   size_t n = 32, tmp = i;\r
+   vector<char> tmp_code; tmp_code.clear();\r
+\r
+    while ( tmp > 1 )\r
+    {\r
+        tmp_code.push_back(tmp%2);\r
+        tmp /= 2;\r
+    }\r
+    tmp_code.push_back(1);\r
+\r
+    vector<char> i_code(n);\r
+    for (size_t j = 0; j < n; ++j)\r
+    {\r
+    char val = j < n - tmp_code.size() ? 0 : tmp_code.at(j+tmp_code.size()-n);\r
+    i_code.push_back(val);\r
+    }\r
+\r
+    const size_t w = 480/n;\r
+\r
+    for (size_t j = 0; j < n; ++j)\r
+    {\r
+        for (size_t k = w*j; k < w*(j+1); ++k)\r
+        mat.row(k) = i_code[j] ? 255*cv::Mat::ones(1, 640, CV_8UC1) : cv::Mat::zeros(1, 640, CV_8UC1);\r
+    }\r
+\r
+    writer << mat;\r
+\r
+    //imshow("test image", mat); waitKey();\r
+\r
+ }\r
+\r
+ writer.~VideoWriter();\r
+}\r
+\r
+void CV_PositioningTest::run(int)\r
+{\r
+    const size_t n_codec = sizeof(codec_bmp_tags)/sizeof(codec_bmp_tags[0]);\r
+\r
+    const string format[] =  {"avi", "mov", "mp4", "mpg", "wmv"};\r
+    const size_t n_format = sizeof(format)/sizeof(format[0]);\r
+\r
+    for (size_t i = 0; i < n_format; ++i)\r
+    for (size_t j = 0; j < n_codec; ++j)\r
+    {\r
+     CreateTestVideo(format[i], codec_bmp_tags[j].tag);\r
+\r
+     stringstream s; s << codec_bmp_tags[j].tag;\r
+       \r
+    cv::VideoCapture cap(ts->get_data_path()+"video/test_video_"+s.str()+format[i]);\r
+       cap.set(CV_CAP_PROP_POS_FRAMES, 0.0);\r
+\r
+       int N = cap.get(CV_CAP_PROP_FRAME_COUNT);\r
+\r
+       vector <int> idx;\r
+\r
+       RNG rng(N);\r
+       idx.clear();\r
+       for( int i = 0; i < N-1; i++ )\r
+       idx.push_back(rng.uniform(0, N));\r
+    idx.push_back(N-1);\r
+       swap(idx.at(rng.uniform(0, N-1)), idx.at(N-1));\r
+\r
+    for (int i = 0; i < N; ++i)\r
+       {\r
+               cap.set(CV_CAP_PROP_POS_FRAMES, (double)idx[i]);\r
+\r
+               cv::Mat img;  cap.retrieve(img);\r
+\r
+               const double thresh = 128.0;\r
+\r
+               const size_t n = 32, w = img.rows/n;\r
+\r
+               int index = 0, deg = n-1;\r
+\r
+               for (size_t j = 0; j < n; ++j)\r
+               {\r
+                       cv::Mat mat = img.rowRange(w*j, w*(j+1)-1);\r
+                       Scalar mat_mean = cv::mean(mat);\r
+                       if (mat_mean[0] > thresh)\r
+                       {\r
+                               index += (2<<deg);\r
+                       }\r
+\r
+                       deg--;\r
+               }\r
+\r
+               if (index != idx[i])\r
+               {\r
+            ts->printf(ts->LOG, "Required position: %d   Returned position: %d\n   FAILED", idx[i], index);\r
+               }\r
+       }\r
+    }\r
+}\r
+\r
+TEST(Highgui_Positioning, regression) { CV_PositioningTest test; test.safe_run(); }\r
+\r
+#endif\r