Path: blob/master/modules/video/perf/perf_optflowpyrlk.cpp
16354 views
#include "perf_precomp.hpp"12namespace opencv_test { namespace {3using namespace perf;45typedef tuple<std::string, int, int, tuple<int,int>, int> Path_Idx_Cn_NPoints_WSize_t;6typedef TestBaseWithParam<Path_Idx_Cn_NPoints_WSize_t> Path_Idx_Cn_NPoints_WSize;78void FormTrackingPointsArray(vector<Point2f>& points, int width, int height, int nPointsX, int nPointsY)9{10int stepX = width / nPointsX;11int stepY = height / nPointsY;12if (stepX < 1 || stepY < 1) FAIL() << "Specified points number is too big";1314points.clear();15points.reserve(nPointsX * nPointsY);1617for( int x = stepX / 2; x < width; x += stepX )18{19for( int y = stepY / 2; y < height; y += stepY )20{21Point2f pt(static_cast<float>(x), static_cast<float>(y));22points.push_back(pt);23}24}25}2627PERF_TEST_P(Path_Idx_Cn_NPoints_WSize, OpticalFlowPyrLK_full, testing::Combine(28testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.png"),29testing::Range(1, 3),30testing::Values(1, 3, 4),31testing::Values(make_tuple(9, 9), make_tuple(15, 15)),32testing::Values(7, 11)33)34)35{36string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));37string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));38Mat img1 = imread(filename1);39Mat img2 = imread(filename2);40if (img1.empty()) FAIL() << "Unable to load source image " << filename1;41if (img2.empty()) FAIL() << "Unable to load source image " << filename2;4243int cn = get<2>(GetParam());44int nPointsX = std::min(get<0>(get<3>(GetParam())), img1.cols);45int nPointsY = std::min(get<1>(get<3>(GetParam())), img1.rows);46int winSize = get<4>(GetParam());4748int maxLevel = 2;49TermCriteria criteria(TermCriteria::COUNT|TermCriteria::EPS, 7, 0.001);50int flags = 0;51double minEigThreshold = 1e-4;5253Mat frame1, frame2;54switch(cn)55{56case 1:57cvtColor(img1, frame1, COLOR_BGR2GRAY, cn);58cvtColor(img2, frame2, COLOR_BGR2GRAY, cn);59break;60case 3:61frame1 = img1;62frame2 = img2;63break;64case 4:65cvtColor(img1, frame1, COLOR_BGR2BGRA, cn);66cvtColor(img2, frame2, COLOR_BGR2BGRA, cn);67break;68default:69FAIL() << "Unexpected number of channels: " << cn;70}7172vector<Point2f> inPoints;73vector<Point2f> outPoints;74vector<uchar> status;75vector<float> err;7677FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);78outPoints.resize(inPoints.size());79status.resize(inPoints.size());80err.resize(inPoints.size());8182declare.in(frame1, frame2, inPoints).out(outPoints);8384TEST_CYCLE_N(30)85{86calcOpticalFlowPyrLK(frame1, frame2, inPoints, outPoints, status, err,87Size(winSize, winSize), maxLevel, criteria,88flags, minEigThreshold);89}9091SANITY_CHECK_NOTHING();92}9394typedef tuple<std::string, int, tuple<int, int>, int> Path_Idx_NPoints_WSize_t;95typedef TestBaseWithParam<Path_Idx_NPoints_WSize_t> Path_Idx_NPoints_WSize;9697PERF_TEST_P(Path_Idx_NPoints_WSize, OpticalFlowPyrLK_ovx, testing::Combine(98testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.png"),99testing::Range(1, 3),100testing::Values(make_tuple(9, 9), make_tuple(15, 15)),101testing::Values(7, 11)102)103)104{105string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));106string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));107Mat img1 = imread(filename1);108Mat img2 = imread(filename2);109if (img1.empty()) FAIL() << "Unable to load source image " << filename1;110if (img2.empty()) FAIL() << "Unable to load source image " << filename2;111112int nPointsX = std::min(get<0>(get<2>(GetParam())), img1.cols);113int nPointsY = std::min(get<1>(get<2>(GetParam())), img1.rows);114int winSize = get<3>(GetParam());115116int maxLevel = 2;117TermCriteria criteria(TermCriteria::COUNT|TermCriteria::EPS, 7, 0.001);118int flags = 0;119double minEigThreshold = 1e-4;120121Mat frame1, frame2;122cvtColor(img1, frame1, COLOR_BGR2GRAY, 1);123cvtColor(img2, frame2, COLOR_BGR2GRAY, 1);124125vector<Point2f> inPoints;126vector<Point2f> outPoints;127vector<uchar> status;128129FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);130outPoints.resize(inPoints.size());131status.resize(inPoints.size());132133declare.in(frame1, frame2, inPoints).out(outPoints);134135TEST_CYCLE_N(30)136{137calcOpticalFlowPyrLK(frame1, frame2, inPoints, outPoints, status, cv::noArray(),138Size(winSize, winSize), maxLevel, criteria,139flags, minEigThreshold);140}141142SANITY_CHECK_NOTHING();143}144145typedef tuple<std::string, int, int, tuple<int,int>, int, bool> Path_Idx_Cn_NPoints_WSize_Deriv_t;146typedef TestBaseWithParam<Path_Idx_Cn_NPoints_WSize_Deriv_t> Path_Idx_Cn_NPoints_WSize_Deriv;147148PERF_TEST_P(Path_Idx_Cn_NPoints_WSize_Deriv, OpticalFlowPyrLK_self, testing::Combine(149testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.png"),150testing::Range(1, 3),151testing::Values(1, 3, 4),152testing::Values(make_tuple(9, 9), make_tuple(15, 15)),153testing::Values(7, 11),154testing::Bool()155)156)157{158string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));159string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));160Mat img1 = imread(filename1);161Mat img2 = imread(filename2);162if (img1.empty()) FAIL() << "Unable to load source image " << filename1;163if (img2.empty()) FAIL() << "Unable to load source image " << filename2;164165int cn = get<2>(GetParam());166int nPointsX = std::min(get<0>(get<3>(GetParam())), img1.cols);167int nPointsY = std::min(get<1>(get<3>(GetParam())), img1.rows);168int winSize = get<4>(GetParam());169bool withDerivatives = get<5>(GetParam());170171int maxLevel = 2;172TermCriteria criteria(TermCriteria::COUNT|TermCriteria::EPS, 7, 0.001);173int flags = 0;174double minEigThreshold = 1e-4;175176Mat frame1, frame2;177switch(cn)178{179case 1:180cvtColor(img1, frame1, COLOR_BGR2GRAY, cn);181cvtColor(img2, frame2, COLOR_BGR2GRAY, cn);182break;183case 3:184frame1 = img1;185frame2 = img2;186break;187case 4:188cvtColor(img1, frame1, COLOR_BGR2BGRA, cn);189cvtColor(img2, frame2, COLOR_BGR2BGRA, cn);190break;191default:192FAIL() << "Unexpected number of channels: " << cn;193}194195vector<Point2f> inPoints;196vector<Point2f> outPoints;197vector<uchar> status;198vector<float> err;199200FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);201outPoints.resize(inPoints.size());202status.resize(inPoints.size());203err.resize(inPoints.size());204205std::vector<Mat> pyramid1, pyramid2;206207maxLevel = buildOpticalFlowPyramid(frame1, pyramid1, Size(winSize, winSize), maxLevel, withDerivatives);208maxLevel = buildOpticalFlowPyramid(frame2, pyramid2, Size(winSize, winSize), maxLevel, withDerivatives);209210declare.in(pyramid1, pyramid2, inPoints).out(outPoints);211declare.time(400);212213int runs = 3;214TEST_CYCLE_MULTIRUN(runs)215{216calcOpticalFlowPyrLK(pyramid1, pyramid2, inPoints, outPoints, status, err,217Size(winSize, winSize), maxLevel, criteria,218flags, minEigThreshold);219}220221SANITY_CHECK_NOTHING();222}223224CV_ENUM(PyrBorderMode, BORDER_DEFAULT, BORDER_TRANSPARENT)225typedef tuple<std::string, int, bool, PyrBorderMode, bool> Path_Win_Deriv_Border_Reuse_t;226typedef TestBaseWithParam<Path_Win_Deriv_Border_Reuse_t> Path_Win_Deriv_Border_Reuse;227228PERF_TEST_P(Path_Win_Deriv_Border_Reuse, OpticalFlowPyrLK_pyr, testing::Combine(229testing::Values<std::string>("cv/optflow/frames/720p_01.png"),230testing::Values(7, 11),231testing::Bool(),232PyrBorderMode::all(),233testing::Bool()234)235)236{237string filename = getDataPath(get<0>(GetParam()));238Mat img = imread(filename);239Size winSize(get<1>(GetParam()), get<1>(GetParam()));240bool withDerivatives = get<2>(GetParam());241int derivBorder = get<3>(GetParam());242int pyrBorder = derivBorder;243if(derivBorder != BORDER_TRANSPARENT)244{245derivBorder = BORDER_CONSTANT;246pyrBorder = BORDER_REFLECT_101;247}248bool tryReuseInputImage = get<4>(GetParam());249std::vector<Mat> pyramid;250251img.adjustROI(winSize.height, winSize.height, winSize.width, winSize.width);252253int maxLevel = buildOpticalFlowPyramid(img, pyramid, winSize, 1000, withDerivatives, BORDER_CONSTANT, BORDER_CONSTANT, tryReuseInputImage);254255declare.in(img).out(pyramid);256257258TEST_CYCLE()259{260buildOpticalFlowPyramid(img, pyramid, winSize, maxLevel, withDerivatives, pyrBorder, derivBorder, tryReuseInputImage);261}262263size_t expected_layers = ((size_t)maxLevel + 1) * (withDerivatives ? 2 : 1);264ASSERT_EQ(expected_layers, pyramid.size());265SANITY_CHECK_NOTHING();266}267268}} // namespace269270271