added GPU bilateral filter + tests
[profile/ivi/opencv.git] / modules / gpu / perf / perf_video.cpp
index 6d779d6..7a64034 100644 (file)
 #include "perf_precomp.hpp"\r
 \r
-#ifdef HAVE_CUDA\r
+using namespace std;\r
+using namespace testing;\r
+\r
+namespace cv\r
+{\r
+    template<> void Ptr<CvBGStatModel>::delete_obj()\r
+    {\r
+        cvReleaseBGStatModel(&obj);\r
+    }\r
+}\r
+\r
+namespace {\r
 \r
 //////////////////////////////////////////////////////\r
 // BroxOpticalFlow\r
 \r
-GPU_PERF_TEST_1(BroxOpticalFlow, cv::gpu::DeviceInfo)\r
-{\r
-    cv::gpu::DeviceInfo devInfo = GetParam();\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
+typedef pair<string, string> pair_string;\r
+\r
+DEF_PARAM_TEST_1(ImagePair, pair_string);\r
 \r
-    cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame0_host.empty());\r
+PERF_TEST_P(ImagePair, Video_BroxOpticalFlow, Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))\r
+{\r
+    declare.time(10);\r
 \r
-    cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame1_host.empty());\r
+    cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame0.empty());\r
 \r
-    frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0);\r
-    frame1_host.convertTo(frame1_host, CV_32FC1, 1.0 / 255.0);\r
+    cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame1.empty());\r
 \r
-    cv::gpu::GpuMat frame0(frame0_host);\r
-    cv::gpu::GpuMat frame1(frame1_host);\r
-    cv::gpu::GpuMat u;\r
-    cv::gpu::GpuMat v;\r
+    frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);\r
+    frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);\r
 \r
-    cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,\r
-                                    10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame0(frame0);\r
+        cv::gpu::GpuMat d_frame1(frame1);\r
+        cv::gpu::GpuMat d_u;\r
+        cv::gpu::GpuMat d_v;\r
 \r
-    d_flow(frame0, frame1, u, v);\r
+        cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,\r
+                                        10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);\r
 \r
-    declare.time(10);\r
+        d_flow(d_frame0, d_frame1, d_u, d_v);\r
 \r
-    TEST_CYCLE()\r
+        TEST_CYCLE()\r
+        {\r
+            d_flow(d_frame0, d_frame1, d_u, d_v);\r
+        }\r
+    }\r
+    else\r
     {\r
-        d_flow(frame0, frame1, u, v);\r
+        FAIL();\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, BroxOpticalFlow, ALL_DEVICES);\r
-\r
 //////////////////////////////////////////////////////\r
 // InterpolateFrames\r
 \r
-GPU_PERF_TEST_1(InterpolateFrames, cv::gpu::DeviceInfo)\r
+PERF_TEST_P(ImagePair, Video_InterpolateFrames, Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))\r
 {\r
-    cv::gpu::DeviceInfo devInfo = GetParam();\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
+    cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame0.empty());\r
 \r
-    cv::Mat frame0_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame0_host.empty());\r
+    cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame1.empty());\r
 \r
-    cv::Mat frame1_host = readImage("gpu/perf/aloeR.jpg", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame1_host.empty());\r
+    frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);\r
+    frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);\r
 \r
-    frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0);\r
-    frame1_host.convertTo(frame1_host, CV_32FC1, 1.0 / 255.0);\r
-\r
-    cv::gpu::GpuMat frame0(frame0_host);\r
-    cv::gpu::GpuMat frame1(frame1_host);\r
-    cv::gpu::GpuMat fu, fv;\r
-    cv::gpu::GpuMat bu, bv;\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame0(frame0);\r
+        cv::gpu::GpuMat d_frame1(frame1);\r
+        cv::gpu::GpuMat d_fu, d_fv;\r
+        cv::gpu::GpuMat d_bu, d_bv;\r
 \r
-    cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,\r
-                                    10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);\r
+        cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,\r
+                                        10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);\r
 \r
-    d_flow(frame0, frame1, fu, fv);\r
-    d_flow(frame1, frame0, bu, bv);\r
+        d_flow(d_frame0, d_frame1, d_fu, d_fv);\r
+        d_flow(d_frame1, d_frame0, d_bu, d_bv);\r
 \r
-    cv::gpu::GpuMat newFrame;\r
-    cv::gpu::GpuMat buf;\r
+        cv::gpu::GpuMat d_newFrame;\r
+        cv::gpu::GpuMat d_buf;\r
 \r
-    cv::gpu::interpolateFrames(frame0, frame1, fu, fv, bu, bv, 0.5f, newFrame, buf);\r
+        cv::gpu::interpolateFrames(d_frame0, d_frame1, d_fu, d_fv, d_bu, d_bv, 0.5f, d_newFrame, d_buf);\r
 \r
-    TEST_CYCLE()\r
+        TEST_CYCLE()\r
+        {\r
+            cv::gpu::interpolateFrames(d_frame0, d_frame1, d_fu, d_fv, d_bu, d_bv, 0.5f, d_newFrame, d_buf);\r
+        }\r
+    }\r
+    else\r
     {\r
-        cv::gpu::interpolateFrames(frame0, frame1, fu, fv, bu, bv, 0.5f, newFrame, buf);\r
+        FAIL();\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, InterpolateFrames, ALL_DEVICES);\r
-\r
 //////////////////////////////////////////////////////\r
 // CreateOpticalFlowNeedleMap\r
 \r
-GPU_PERF_TEST_1(CreateOpticalFlowNeedleMap, cv::gpu::DeviceInfo)\r
+PERF_TEST_P(ImagePair, Video_CreateOpticalFlowNeedleMap, Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))\r
 {\r
-    cv::gpu::DeviceInfo devInfo = GetParam();\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
-\r
-    cv::Mat frame0_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame0_host.empty());\r
+    cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame0.empty());\r
 \r
-    cv::Mat frame1_host = readImage("gpu/perf/aloeR.jpg", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame1_host.empty());\r
+    cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame1.empty());\r
 \r
-    frame0_host.convertTo(frame0_host, CV_32FC1, 1.0 / 255.0);\r
-    frame1_host.convertTo(frame1_host, CV_32FC1, 1.0 / 255.0);\r
+    frame0.convertTo(frame0, CV_32FC1, 1.0 / 255.0);\r
+    frame1.convertTo(frame1, CV_32FC1, 1.0 / 255.0);\r
 \r
-    cv::gpu::GpuMat frame0(frame0_host);\r
-    cv::gpu::GpuMat frame1(frame1_host);\r
-    cv::gpu::GpuMat u, v;\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame0(frame0);\r
+        cv::gpu::GpuMat d_frame1(frame1);\r
+        cv::gpu::GpuMat d_u;\r
+        cv::gpu::GpuMat d_v;\r
 \r
-    cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,\r
-                                    10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);\r
+        cv::gpu::BroxOpticalFlow d_flow(0.197f /*alpha*/, 50.0f /*gamma*/, 0.8f /*scale_factor*/,\r
+                                        10 /*inner_iterations*/, 77 /*outer_iterations*/, 10 /*solver_iterations*/);\r
 \r
-    d_flow(frame0, frame1, u, v);\r
+        d_flow(d_frame0, d_frame1, d_u, d_v);\r
 \r
-    cv::gpu::GpuMat vertex, colors;\r
+        cv::gpu::GpuMat d_vertex, d_colors;\r
 \r
-    cv::gpu::createOpticalFlowNeedleMap(u, v, vertex, colors);\r
+        cv::gpu::createOpticalFlowNeedleMap(d_u, d_v, d_vertex, d_colors);\r
 \r
-    TEST_CYCLE()\r
+        TEST_CYCLE()\r
+        {\r
+            cv::gpu::createOpticalFlowNeedleMap(d_u, d_v, d_vertex, d_colors);\r
+        }\r
+    }\r
+    else\r
     {\r
-        cv::gpu::createOpticalFlowNeedleMap(u, v, vertex, colors);\r
+        FAIL();\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, CreateOpticalFlowNeedleMap, ALL_DEVICES);\r
-\r
 //////////////////////////////////////////////////////\r
 // GoodFeaturesToTrack\r
 \r
-IMPLEMENT_PARAM_CLASS(MinDistance, double)\r
+DEF_PARAM_TEST(Image_MinDistance, string, double);\r
 \r
-GPU_PERF_TEST(GoodFeaturesToTrack, cv::gpu::DeviceInfo, MinDistance)\r
+PERF_TEST_P(Image_MinDistance, Video_GoodFeaturesToTrack, Combine(Values<string>("gpu/perf/aloe.jpg"), Values(0.0, 3.0)))\r
 {\r
-    cv::gpu::DeviceInfo devInfo = GET_PARAM(0);\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
-\r
+    string fileName = GET_PARAM(0);\r
     double minDistance = GET_PARAM(1);\r
 \r
-    cv::Mat image_host = readImage("gpu/perf/aloe.jpg", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(image_host.empty());\r
+    cv::Mat image = readImage(fileName, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(image.empty());\r
 \r
-    cv::gpu::GoodFeaturesToTrackDetector_GPU detector(8000, 0.01, minDistance);\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GoodFeaturesToTrackDetector_GPU d_detector(8000, 0.01, minDistance);\r
 \r
-    cv::gpu::GpuMat image(image_host);\r
-    cv::gpu::GpuMat pts;\r
+        cv::gpu::GpuMat d_image(image);\r
+        cv::gpu::GpuMat d_pts;\r
 \r
-    detector(image, pts);\r
+        d_detector(d_image, d_pts);\r
 \r
-    TEST_CYCLE()\r
+        TEST_CYCLE()\r
+        {\r
+            d_detector(d_image, d_pts);\r
+        }\r
+    }\r
+    else\r
     {\r
-        detector(image, pts);\r
+        cv::Mat pts;\r
+\r
+        cv::goodFeaturesToTrack(image, pts, 8000, 0.01, minDistance);\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            cv::goodFeaturesToTrack(image, pts, 8000, 0.01, minDistance);\r
+        }\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, GoodFeaturesToTrack, testing::Combine(\r
-    ALL_DEVICES,\r
-    testing::Values(MinDistance(0.0), MinDistance(3.0))));\r
-\r
 //////////////////////////////////////////////////////\r
 // PyrLKOpticalFlowSparse\r
 \r
-IMPLEMENT_PARAM_CLASS(GraySource, bool)\r
-IMPLEMENT_PARAM_CLASS(Points, int)\r
-IMPLEMENT_PARAM_CLASS(WinSize, int)\r
+DEF_PARAM_TEST(ImagePair_Gray_NPts_WinSz_Levels_Iters, pair_string, bool, int, int, int, int);\r
 \r
-GPU_PERF_TEST(PyrLKOpticalFlowSparse, cv::gpu::DeviceInfo, GraySource, Points, WinSize)\r
+PERF_TEST_P(ImagePair_Gray_NPts_WinSz_Levels_Iters, Video_PyrLKOpticalFlowSparse, Combine(\r
+    Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")),\r
+    Bool(),\r
+    Values(1000, 2000, 4000, 8000),\r
+    Values(9, 13, 17, 21),\r
+    Values(1, 2, 3),\r
+    Values(1, 10, 30)))\r
 {\r
-    cv::gpu::DeviceInfo devInfo = GET_PARAM(0);\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
+    declare.time(20.0);\r
 \r
+    pair_string imagePair = GET_PARAM(0);\r
     bool useGray = GET_PARAM(1);\r
     int points = GET_PARAM(2);\r
-    int win_size = GET_PARAM(3);\r
+    int winSize = GET_PARAM(3);\r
+    int levels = GET_PARAM(4);\r
+    int iters = GET_PARAM(5);\r
 \r
-    cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);\r
-    ASSERT_FALSE(frame0_host.empty());\r
+    cv::Mat frame0 = readImage(imagePair.first, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);\r
+    ASSERT_FALSE(frame0.empty());\r
 \r
-    cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);\r
-    ASSERT_FALSE(frame1_host.empty());\r
+    cv::Mat frame1 = readImage(imagePair.second, useGray ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR);\r
+    ASSERT_FALSE(frame1.empty());\r
 \r
     cv::Mat gray_frame;\r
     if (useGray)\r
-        gray_frame = frame0_host;\r
+        gray_frame = frame0;\r
     else\r
-        cv::cvtColor(frame0_host, gray_frame, cv::COLOR_BGR2GRAY);\r
+        cv::cvtColor(frame0, gray_frame, cv::COLOR_BGR2GRAY);\r
 \r
-    cv::gpu::GpuMat pts;\r
+    cv::Mat pts;\r
+    cv::goodFeaturesToTrack(gray_frame, pts, points, 0.01, 0.0);\r
 \r
-    cv::gpu::GoodFeaturesToTrackDetector_GPU detector(points, 0.01, 0.0);\r
-    detector(cv::gpu::GpuMat(gray_frame), pts);\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_pts(pts.reshape(2, 1));\r
 \r
-    cv::gpu::PyrLKOpticalFlow pyrLK;\r
-    pyrLK.winSize = cv::Size(win_size, win_size);\r
+        cv::gpu::PyrLKOpticalFlow d_pyrLK;\r
+        d_pyrLK.winSize = cv::Size(winSize, winSize);\r
+        d_pyrLK.maxLevel = levels - 1;\r
+        d_pyrLK.iters = iters;\r
 \r
-    cv::gpu::GpuMat frame0(frame0_host);\r
-    cv::gpu::GpuMat frame1(frame1_host);\r
-    cv::gpu::GpuMat nextPts;\r
-    cv::gpu::GpuMat status;\r
+        cv::gpu::GpuMat d_frame0(frame0);\r
+        cv::gpu::GpuMat d_frame1(frame1);\r
+        cv::gpu::GpuMat d_nextPts;\r
+        cv::gpu::GpuMat d_status;\r
 \r
-    pyrLK.sparse(frame0, frame1, pts, nextPts, status);\r
+        d_pyrLK.sparse(d_frame0, d_frame1, d_pts, d_nextPts, d_status);\r
 \r
-    TEST_CYCLE()\r
+        TEST_CYCLE()\r
+        {\r
+            d_pyrLK.sparse(d_frame0, d_frame1, d_pts, d_nextPts, d_status);\r
+        }\r
+    }\r
+    else\r
     {\r
-        pyrLK.sparse(frame0, frame1, pts, nextPts, status);\r
+        cv::Mat nextPts;\r
+        cv::Mat status;\r
+\r
+        cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, cv::noArray(),\r
+                                 cv::Size(winSize, winSize), levels - 1,\r
+                                 cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, iters, 0.01));\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            cv::calcOpticalFlowPyrLK(frame0, frame1, pts, nextPts, status, cv::noArray(),\r
+                                     cv::Size(winSize, winSize), levels - 1,\r
+                                     cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, iters, 0.01));\r
+        }\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlowSparse, testing::Combine(\r
-    ALL_DEVICES,\r
-    testing::Values(GraySource(true), GraySource(false)),\r
-    testing::Values(Points(1000), Points(2000), Points(4000), Points(8000)),\r
-    testing::Values(WinSize(17), WinSize(21))));\r
-\r
 //////////////////////////////////////////////////////\r
 // PyrLKOpticalFlowDense\r
 \r
-GPU_PERF_TEST_1(PyrLKOpticalFlowDense, cv::gpu::DeviceInfo)\r
+DEF_PARAM_TEST(ImagePair_WinSz_Levels_Iters, pair_string, int, int, int);\r
+\r
+PERF_TEST_P(ImagePair_WinSz_Levels_Iters, Video_PyrLKOpticalFlowDense, Combine(\r
+    Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")),\r
+    Values(3, 5, 7, 9, 13, 17, 21),\r
+    Values(1, 2, 3),\r
+    Values(1, 10)))\r
 {\r
-    cv::gpu::DeviceInfo devInfo = GetParam();\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
+    declare.time(30);\r
 \r
-    cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame0_host.empty());\r
+    pair_string imagePair = GET_PARAM(0);\r
+    int winSize = GET_PARAM(1);\r
+    int levels = GET_PARAM(2);\r
+    int iters = GET_PARAM(3);\r
 \r
-    cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame1_host.empty());\r
+    cv::Mat frame0 = readImage(imagePair.first, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame0.empty());\r
 \r
-    cv::gpu::GpuMat frame0(frame0_host);\r
-    cv::gpu::GpuMat frame1(frame1_host);\r
-    cv::gpu::GpuMat u;\r
-    cv::gpu::GpuMat v;\r
+    cv::Mat frame1 = readImage(imagePair.second, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame1.empty());\r
 \r
-    cv::gpu::PyrLKOpticalFlow pyrLK;\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame0(frame0);\r
+        cv::gpu::GpuMat d_frame1(frame1);\r
+        cv::gpu::GpuMat d_u;\r
+        cv::gpu::GpuMat d_v;\r
+\r
+        cv::gpu::PyrLKOpticalFlow d_pyrLK;\r
+        d_pyrLK.winSize = cv::Size(winSize, winSize);\r
+        d_pyrLK.maxLevel = levels - 1;\r
+        d_pyrLK.iters = iters;\r
+\r
+        d_pyrLK.dense(d_frame0, d_frame1, d_u, d_v);\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            d_pyrLK.dense(d_frame0, d_frame1, d_u, d_v);\r
+        }\r
+    }\r
+    else\r
+    {\r
+        FAIL();\r
+    }\r
+}\r
 \r
-    pyrLK.dense(frame0, frame1, u, v);\r
+//////////////////////////////////////////////////////\r
+// FarnebackOpticalFlow\r
 \r
+PERF_TEST_P(ImagePair, Video_FarnebackOpticalFlow, Values<pair_string>(make_pair("gpu/opticalflow/frame0.png", "gpu/opticalflow/frame1.png")))\r
+{\r
     declare.time(10);\r
 \r
-    TEST_CYCLE()\r
+    cv::Mat frame0 = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame0.empty());\r
+\r
+    cv::Mat frame1 = readImage(GetParam().second, cv::IMREAD_GRAYSCALE);\r
+    ASSERT_FALSE(frame1.empty());\r
+\r
+    int numLevels = 5;\r
+    double pyrScale = 0.5;\r
+    int winSize = 13;\r
+    int numIters = 10;\r
+    int polyN = 5;\r
+    double polySigma = 1.1;\r
+    int flags = 0;\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame0(frame0);\r
+        cv::gpu::GpuMat d_frame1(frame1);\r
+        cv::gpu::GpuMat d_u;\r
+        cv::gpu::GpuMat d_v;\r
+\r
+        cv::gpu::FarnebackOpticalFlow d_farneback;\r
+        d_farneback.numLevels = numLevels;\r
+        d_farneback.pyrScale = pyrScale;\r
+        d_farneback.winSize = winSize;\r
+        d_farneback.numIters = numIters;\r
+        d_farneback.polyN = polyN;\r
+        d_farneback.polySigma = polySigma;\r
+        d_farneback.flags = flags;\r
+\r
+        d_farneback(d_frame0, d_frame1, d_u, d_v);\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            d_farneback(d_frame0, d_frame1, d_u, d_v);\r
+        }\r
+    }\r
+    else\r
     {\r
-        pyrLK.dense(frame0, frame1, u, v);\r
+        cv::Mat flow;\r
+\r
+        cv::calcOpticalFlowFarneback(frame0, frame1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            cv::calcOpticalFlowFarneback(frame0, frame1, flow, pyrScale, numLevels, winSize, numIters, polyN, polySigma, flags);\r
+        }\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, PyrLKOpticalFlowDense, ALL_DEVICES);\r
+//////////////////////////////////////////////////////\r
+// FGDStatModel\r
+\r
+DEF_PARAM_TEST_1(Video, string);\r
+\r
+PERF_TEST_P(Video, Video_FGDStatModel, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))\r
+{\r
+    declare.time(60);\r
+\r
+    string inputFile = perf::TestBase::getDataPath(GetParam());\r
+\r
+    cv::VideoCapture cap(inputFile);\r
+    ASSERT_TRUE(cap.isOpened());\r
+\r
+    cv::Mat frame;\r
+    cap >> frame;\r
+    ASSERT_FALSE(frame.empty());\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame(frame);\r
+\r
+        cv::gpu::FGDStatModel d_model(4);\r
+        d_model.create(d_frame);\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            d_frame.upload(frame);\r
+\r
+            startTimer(); next();\r
+            d_model.update(d_frame);\r
+            stopTimer();\r
+        }\r
+    }\r
+    else\r
+    {\r
+        IplImage ipl_frame = frame;\r
+        cv::Ptr<CvBGStatModel> model(cvCreateFGDStatModel(&ipl_frame));\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            ipl_frame = frame;\r
+\r
+            startTimer(); next();\r
+            cvUpdateBGStatModel(&ipl_frame, model);\r
+            stopTimer();\r
+        }\r
+    }\r
+}\r
 \r
 //////////////////////////////////////////////////////\r
-// FarnebackOpticalFlowTest\r
+// MOG\r
+\r
+DEF_PARAM_TEST(Video_Cn_LearningRate, string, int, double);\r
 \r
-GPU_PERF_TEST_1(FarnebackOpticalFlowTest, cv::gpu::DeviceInfo)\r
+PERF_TEST_P(Video_Cn_LearningRate, Video_MOG, Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4, Values(0.0, 0.01)))\r
 {\r
-    cv::gpu::DeviceInfo devInfo = GetParam();\r
-    cv::gpu::setDevice(devInfo.deviceID());\r
+    string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));\r
+    int cn = GET_PARAM(1);\r
+    float learningRate = static_cast<float>(GET_PARAM(2));\r
 \r
-    cv::Mat frame0_host = readImage("gpu/opticalflow/frame0.png", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame0_host.empty());\r
+    cv::VideoCapture cap(inputFile);\r
+    ASSERT_TRUE(cap.isOpened());\r
 \r
-    cv::Mat frame1_host = readImage("gpu/opticalflow/frame1.png", cv::IMREAD_GRAYSCALE);\r
-    ASSERT_FALSE(frame1_host.empty());\r
+    cv::Mat frame;\r
 \r
-    cv::gpu::GpuMat frame0(frame0_host);\r
-    cv::gpu::GpuMat frame1(frame1_host);\r
-    cv::gpu::GpuMat u;\r
-    cv::gpu::GpuMat v;\r
+    cap >> frame;\r
+    ASSERT_FALSE(frame.empty());\r
 \r
-    cv::gpu::FarnebackOpticalFlow farneback;\r
+    if (cn != 3)\r
+    {\r
+        cv::Mat temp;\r
+        if (cn == 1)\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+        else\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+        cv::swap(temp, frame);\r
+    }\r
 \r
-    farneback(frame0, frame1, u, v);\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame(frame);\r
+        cv::gpu::MOG_GPU d_mog;\r
+        cv::gpu::GpuMat d_foreground;\r
+\r
+        d_mog(d_frame, d_foreground, learningRate);\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            d_frame.upload(frame);\r
+\r
+            startTimer(); next();\r
+            d_mog(d_frame, d_foreground, learningRate);\r
+            stopTimer();\r
+        }\r
+    }\r
+    else\r
+    {\r
+        cv::BackgroundSubtractorMOG mog;\r
+        cv::Mat foreground;\r
+\r
+        mog(frame, foreground, learningRate);\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            startTimer(); next();\r
+            mog(frame, foreground, learningRate);\r
+            stopTimer();\r
+        }\r
+    }\r
+}\r
 \r
-    declare.time(10);\r
+//////////////////////////////////////////////////////\r
+// MOG2\r
+\r
+DEF_PARAM_TEST(Video_Cn, string, int);\r
 \r
-    TEST_CYCLE()\r
+PERF_TEST_P(Video_Cn, Video_MOG2, Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))\r
+{\r
+    string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));\r
+    int cn = GET_PARAM(1);\r
+\r
+    cv::VideoCapture cap(inputFile);\r
+    ASSERT_TRUE(cap.isOpened());\r
+\r
+    cv::Mat frame;\r
+\r
+    cap >> frame;\r
+    ASSERT_FALSE(frame.empty());\r
+\r
+    if (cn != 3)\r
     {\r
-        farneback(frame0, frame1, u, v);\r
+        cv::Mat temp;\r
+        if (cn == 1)\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+        else\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+        cv::swap(temp, frame);\r
+    }\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame(frame);\r
+        cv::gpu::MOG2_GPU d_mog2;\r
+        cv::gpu::GpuMat d_foreground;\r
+\r
+        d_mog2(d_frame, d_foreground);\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            d_frame.upload(frame);\r
+\r
+            startTimer(); next();\r
+            d_mog2(d_frame, d_foreground);\r
+            stopTimer();\r
+        }\r
+    }\r
+    else\r
+    {\r
+        cv::BackgroundSubtractorMOG2 mog2;\r
+        cv::Mat foreground;\r
+\r
+        mog2(frame, foreground);\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            startTimer(); next();\r
+            mog2(frame, foreground);\r
+            stopTimer();\r
+        }\r
+    }\r
+}\r
+\r
+//////////////////////////////////////////////////////\r
+// MOG2GetBackgroundImage\r
+\r
+PERF_TEST_P(Video_Cn, Video_MOG2GetBackgroundImage, Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))\r
+{\r
+    string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));\r
+    int cn = GET_PARAM(1);\r
+\r
+    cv::VideoCapture cap(inputFile);\r
+    ASSERT_TRUE(cap.isOpened());\r
+\r
+    cv::Mat frame;\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame;\r
+        cv::gpu::MOG2_GPU d_mog2;\r
+        cv::gpu::GpuMat d_foreground;\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            d_frame.upload(frame);\r
+\r
+            d_mog2(d_frame, d_foreground);\r
+        }\r
+\r
+        cv::gpu::GpuMat d_background;\r
+        d_mog2.getBackgroundImage(d_background);\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            d_mog2.getBackgroundImage(d_background);\r
+        }\r
+    }\r
+    else\r
+    {\r
+        cv::BackgroundSubtractorMOG2 mog2;\r
+        cv::Mat foreground;\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            mog2(frame, foreground);\r
+        }\r
+\r
+        cv::Mat background;\r
+        mog2.getBackgroundImage(background);\r
+\r
+        TEST_CYCLE()\r
+        {\r
+            mog2.getBackgroundImage(background);\r
+        }\r
+    }\r
+}\r
+\r
+//////////////////////////////////////////////////////\r
+// VIBE\r
+\r
+PERF_TEST_P(Video_Cn, Video_VIBE, Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4))\r
+{\r
+    string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));\r
+    int cn = GET_PARAM(1);\r
+\r
+    cv::VideoCapture cap(inputFile);\r
+    ASSERT_TRUE(cap.isOpened());\r
+\r
+    cv::Mat frame;\r
+    cap >> frame;\r
+    ASSERT_FALSE(frame.empty());\r
+\r
+    if (cn != 3)\r
+    {\r
+        cv::Mat temp;\r
+        if (cn == 1)\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+        else\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+        cv::swap(temp, frame);\r
+    }\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame(frame);\r
+        cv::gpu::VIBE_GPU d_vibe;\r
+        cv::gpu::GpuMat d_foreground;\r
+\r
+        d_vibe(d_frame, d_foreground);\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            cap >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            d_frame.upload(frame);\r
+\r
+            startTimer(); next();\r
+            d_vibe(d_frame, d_foreground);\r
+            stopTimer();\r
+        }\r
+    }\r
+    else\r
+    {\r
+        FAIL();\r
     }\r
 }\r
 \r
-INSTANTIATE_TEST_CASE_P(Video, FarnebackOpticalFlowTest, ALL_DEVICES);\r
+//////////////////////////////////////////////////////\r
+// GMG\r
+\r
+DEF_PARAM_TEST(Video_Cn_MaxFeatures, string, int, int);\r
+\r
+PERF_TEST_P(Video_Cn_MaxFeatures, Video_GMG, Combine(Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"), GPU_CHANNELS_1_3_4, Values(20, 40, 60)))\r
+{\r
+    std::string inputFile = perf::TestBase::getDataPath(GET_PARAM(0));\r
+    int cn = GET_PARAM(1);\r
+    int maxFeatures = GET_PARAM(2);\r
+\r
+    cv::VideoCapture cap(inputFile);\r
+    ASSERT_TRUE(cap.isOpened());\r
+\r
+    cv::Mat frame;\r
+    cap >> frame;\r
+    ASSERT_FALSE(frame.empty());\r
+\r
+    if (cn != 3)\r
+    {\r
+        cv::Mat temp;\r
+        if (cn == 1)\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+        else\r
+            cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+        cv::swap(temp, frame);\r
+    }\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::GpuMat d_frame(frame);\r
+        cv::gpu::GpuMat d_fgmask;\r
+\r
+        cv::gpu::GMG_GPU d_gmg;\r
+        d_gmg.maxFeatures = maxFeatures;\r
+\r
+        d_gmg(d_frame, d_fgmask);\r
+\r
+        for (int i = 0; i < 150; ++i)\r
+        {\r
+            cap >> frame;\r
+            if (frame.empty())\r
+            {\r
+                cap.open(inputFile);\r
+                cap >> frame;\r
+            }\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            d_frame.upload(frame);\r
+\r
+            startTimer(); next();\r
+            d_gmg(d_frame, d_fgmask);\r
+            stopTimer();\r
+        }\r
+    }\r
+    else\r
+    {\r
+        cv::Mat fgmask;\r
+        cv::Mat zeros(frame.size(), CV_8UC1, cv::Scalar::all(0));\r
+\r
+        cv::BackgroundSubtractorGMG gmg;\r
+        gmg.set("maxFeatures", maxFeatures);\r
+        gmg.initialize(frame.size(), 0.0, 255.0);\r
+\r
+        gmg(frame, fgmask);\r
+\r
+        for (int i = 0; i < 150; ++i)\r
+        {\r
+            cap >> frame;\r
+            if (frame.empty())\r
+            {\r
+                cap.open(inputFile);\r
+                cap >> frame;\r
+            }\r
+\r
+            if (cn != 3)\r
+            {\r
+                cv::Mat temp;\r
+                if (cn == 1)\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2GRAY);\r
+                else\r
+                    cv::cvtColor(frame, temp, cv::COLOR_BGR2BGRA);\r
+                cv::swap(temp, frame);\r
+            }\r
+\r
+            startTimer(); next();\r
+            gmg(frame, fgmask);\r
+            stopTimer();\r
+        }\r
+    }\r
+}\r
+\r
+//////////////////////////////////////////////////////\r
+// VideoWriter\r
+\r
+PERF_TEST_P(Video, Video_VideoWriter, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))\r
+{\r
+    declare.time(30);\r
+\r
+    string inputFile = perf::TestBase::getDataPath(GetParam());\r
+    string outputFile = cv::tempfile(".avi");\r
+\r
+    const double FPS = 25.0;\r
+\r
+    cv::VideoCapture reader(inputFile);\r
+    ASSERT_TRUE( reader.isOpened() );\r
+\r
+    cv::Mat frame;\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::VideoWriter_GPU d_writer;\r
+\r
+        cv::gpu::GpuMat d_frame;\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            reader >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            d_frame.upload(frame);\r
+\r
+            if (!d_writer.isOpened())\r
+                d_writer.open(outputFile, frame.size(), FPS);\r
+\r
+            startTimer(); next();\r
+            d_writer.write(d_frame);\r
+            stopTimer();\r
+        }\r
+    }\r
+    else\r
+    {\r
+        cv::VideoWriter writer;\r
+\r
+        for (int i = 0; i < 10; ++i)\r
+        {\r
+            reader >> frame;\r
+            ASSERT_FALSE(frame.empty());\r
+\r
+            if (!writer.isOpened())\r
+                writer.open(outputFile, CV_FOURCC('X', 'V', 'I', 'D'), FPS, frame.size());\r
+\r
+            startTimer(); next();\r
+            writer.write(frame);\r
+            stopTimer();\r
+        }\r
+    }\r
+}\r
+\r
+//////////////////////////////////////////////////////\r
+// VideoReader\r
+\r
+PERF_TEST_P(Video, Video_VideoReader, Values("gpu/video/768x576.avi", "gpu/video/1920x1080.avi"))\r
+{\r
+    declare.time(20);\r
+\r
+    string inputFile = perf::TestBase::getDataPath(GetParam());\r
+\r
+    if (runOnGpu)\r
+    {\r
+        cv::gpu::VideoReader_GPU d_reader(inputFile);\r
+        ASSERT_TRUE( d_reader.isOpened() );\r
+\r
+        cv::gpu::GpuMat d_frame;\r
+\r
+        d_reader.read(d_frame);\r
+\r
+        TEST_CYCLE_N(10)\r
+        {\r
+            d_reader.read(d_frame);\r
+        }\r
+    }\r
+    else\r
+    {\r
+        cv::VideoCapture reader(inputFile);\r
+        ASSERT_TRUE( reader.isOpened() );\r
+\r
+        cv::Mat frame;\r
+\r
+        reader >> frame;\r
+\r
+        TEST_CYCLE_N(10)\r
+        {\r
+            reader >> frame;\r
+        }\r
+    }\r
+}\r
 \r
-#endif\r
+} // namespace\r