Added new performance tests for calcOpticalFlowPyrLK and buildOpticalFlowPyramid; extracted private header from lkpyramid.cpp
This commit is contained in:
@@ -28,12 +28,12 @@ void FormTrackingPointsArray(vector<Point2f>& points, int width, int height, int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PERF_TEST_P(Path_Idx_Cn_NPoints_WSize, OpticalFlowPyrLK, testing::Combine(
|
PERF_TEST_P(Path_Idx_Cn_NPoints_WSize, OpticalFlowPyrLK_full, testing::Combine(
|
||||||
testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.jpg"),
|
testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.jpg"),
|
||||||
testing::Range(0, 3),
|
testing::Range(1, 3),
|
||||||
testing::Values(1, 3, 4),
|
testing::Values(1, 3, 4),
|
||||||
testing::Values(make_tuple(9, 9), make_tuple(15, 15)),
|
testing::Values(make_tuple(9, 9), make_tuple(15, 15)),
|
||||||
testing::Values(7, 11, 21, 25)
|
testing::Values(7, 11, 25)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -48,6 +48,7 @@ PERF_TEST_P(Path_Idx_Cn_NPoints_WSize, OpticalFlowPyrLK, testing::Combine(
|
|||||||
int nPointsX = min(get<0>(get<3>(GetParam())), img1.cols);
|
int nPointsX = min(get<0>(get<3>(GetParam())), img1.cols);
|
||||||
int nPointsY = min(get<1>(get<3>(GetParam())), img1.rows);
|
int nPointsY = min(get<1>(get<3>(GetParam())), img1.rows);
|
||||||
int winSize = get<4>(GetParam());
|
int winSize = get<4>(GetParam());
|
||||||
|
|
||||||
int maxLevel = 2;
|
int maxLevel = 2;
|
||||||
TermCriteria criteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 7, 0.001);
|
TermCriteria criteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 7, 0.001);
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
@@ -91,3 +92,120 @@ PERF_TEST_P(Path_Idx_Cn_NPoints_WSize, OpticalFlowPyrLK, testing::Combine(
|
|||||||
flags, minEigThreshold);
|
flags, minEigThreshold);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef tr1::tuple<std::string, int, int, tr1::tuple<int,int>, int, bool> Path_Idx_Cn_NPoints_WSize_Deriv_t;
|
||||||
|
typedef TestBaseWithParam<Path_Idx_Cn_NPoints_WSize_Deriv_t> Path_Idx_Cn_NPoints_WSize_Deriv;
|
||||||
|
|
||||||
|
PERF_TEST_P(Path_Idx_Cn_NPoints_WSize_Deriv, OpticalFlowPyrLK_self, testing::Combine(
|
||||||
|
testing::Values<std::string>("cv/optflow/frames/VGA_%02d.png", "cv/optflow/frames/720p_%02d.jpg"),
|
||||||
|
testing::Range(1, 3),
|
||||||
|
testing::Values(1, 3, 4),
|
||||||
|
testing::Values(make_tuple(9, 9), make_tuple(15, 15)),
|
||||||
|
testing::Values(7, 11, 25),
|
||||||
|
testing::Bool()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
string filename1 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam())));
|
||||||
|
string filename2 = getDataPath(cv::format(get<0>(GetParam()).c_str(), get<1>(GetParam()) + 1));
|
||||||
|
Mat img1 = imread(filename1);
|
||||||
|
Mat img2 = imread(filename2);
|
||||||
|
if (img1.empty()) FAIL() << "Unable to load source image " << filename1;
|
||||||
|
if (img2.empty()) FAIL() << "Unable to load source image " << filename2;
|
||||||
|
|
||||||
|
int cn = get<2>(GetParam());
|
||||||
|
int nPointsX = min(get<0>(get<3>(GetParam())), img1.cols);
|
||||||
|
int nPointsY = min(get<1>(get<3>(GetParam())), img1.rows);
|
||||||
|
int winSize = get<4>(GetParam());
|
||||||
|
bool withDerivatives = get<5>(GetParam());
|
||||||
|
|
||||||
|
int maxLevel = 2;
|
||||||
|
TermCriteria criteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 7, 0.001);
|
||||||
|
int flags = 0;
|
||||||
|
double minEigThreshold = 1e-4;
|
||||||
|
|
||||||
|
Mat frame1, frame2;
|
||||||
|
switch(cn)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
cvtColor(img1, frame1, COLOR_BGR2GRAY, cn);
|
||||||
|
cvtColor(img2, frame2, COLOR_BGR2GRAY, cn);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
frame1 = img1;
|
||||||
|
frame2 = img2;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
cvtColor(img1, frame1, COLOR_BGR2BGRA, cn);
|
||||||
|
cvtColor(img2, frame2, COLOR_BGR2BGRA, cn);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
FAIL() << "Unexpected number of channels: " << cn;
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<Point2f> inPoints;
|
||||||
|
vector<Point2f> outPoints;
|
||||||
|
vector<uchar> status;
|
||||||
|
vector<float> err;
|
||||||
|
|
||||||
|
FormTrackingPointsArray(inPoints, frame1.cols, frame1.rows, nPointsX, nPointsY);
|
||||||
|
outPoints.resize(inPoints.size());
|
||||||
|
status.resize(inPoints.size());
|
||||||
|
err.resize(inPoints.size());
|
||||||
|
|
||||||
|
std::vector<Mat> pyramid1, pyramid2;
|
||||||
|
|
||||||
|
maxLevel = buildOpticalFlowPyramid(frame1, pyramid1, Size(winSize, winSize), maxLevel, withDerivatives);
|
||||||
|
maxLevel = buildOpticalFlowPyramid(frame2, pyramid2, Size(winSize, winSize), maxLevel, withDerivatives);
|
||||||
|
|
||||||
|
declare.in(pyramid1, pyramid2, inPoints).out(outPoints);
|
||||||
|
|
||||||
|
TEST_CYCLE()
|
||||||
|
{
|
||||||
|
calcOpticalFlowPyrLK(pyramid1, pyramid2, inPoints, outPoints, status, err,
|
||||||
|
Size(winSize, winSize), maxLevel, criteria,
|
||||||
|
flags, minEigThreshold);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CV_ENUM(PyrBorderMode, BORDER_DEFAULT, BORDER_TRANSPARENT);
|
||||||
|
typedef tr1::tuple<std::string, int, bool, PyrBorderMode, bool> Path_Win_Deriv_Border_Reuse_t;
|
||||||
|
typedef TestBaseWithParam<Path_Win_Deriv_Border_Reuse_t> Path_Win_Deriv_Border_Reuse;
|
||||||
|
|
||||||
|
PERF_TEST_P(Path_Win_Deriv_Border_Reuse, OpticalFlowPyrLK_pyr, testing::Combine(
|
||||||
|
testing::Values<std::string>("cv/optflow/frames/720p_01.jpg"),
|
||||||
|
testing::Values(7, 11),
|
||||||
|
testing::Bool(),
|
||||||
|
testing::ValuesIn(PyrBorderMode::all()),
|
||||||
|
testing::Bool()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
string filename = getDataPath(get<0>(GetParam()));
|
||||||
|
Mat img = imread(filename);
|
||||||
|
Size winSize(get<1>(GetParam()), get<1>(GetParam()));
|
||||||
|
bool withDerivatives = get<2>(GetParam());
|
||||||
|
int derivBorder = get<3>(GetParam());
|
||||||
|
int pyrBorder = derivBorder;
|
||||||
|
if(derivBorder != BORDER_TRANSPARENT)
|
||||||
|
{
|
||||||
|
derivBorder = BORDER_CONSTANT;
|
||||||
|
pyrBorder = BORDER_REFLECT_101;
|
||||||
|
}
|
||||||
|
bool tryReuseInputImage = get<4>(GetParam());
|
||||||
|
std::vector<Mat> pyramid;
|
||||||
|
|
||||||
|
img.adjustROI(winSize.height, winSize.height, winSize.width, winSize.width);
|
||||||
|
|
||||||
|
int maxLevel = buildOpticalFlowPyramid(img, pyramid, winSize, 1000, withDerivatives, BORDER_CONSTANT, BORDER_CONSTANT, tryReuseInputImage);
|
||||||
|
|
||||||
|
declare.in(img).out(pyramid);
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CYCLE()
|
||||||
|
{
|
||||||
|
buildOpticalFlowPyramid(img, pyramid, winSize, maxLevel, withDerivatives, pyrBorder, derivBorder, tryReuseInputImage);
|
||||||
|
}
|
||||||
|
|
||||||
|
SANITY_CHECK(pyramid);
|
||||||
|
}
|
||||||
@@ -41,18 +41,23 @@
|
|||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "lkpyramid.hpp"
|
||||||
|
|
||||||
namespace cv
|
namespace
|
||||||
{
|
{
|
||||||
|
static void calcSharrDeriv(const cv::Mat& src, cv::Mat& dst)
|
||||||
typedef short deriv_type;
|
|
||||||
|
|
||||||
static void calcSharrDeriv(const Mat& src, Mat& dst)
|
|
||||||
{
|
{
|
||||||
|
using namespace cv;
|
||||||
|
using cv::detail::deriv_type;
|
||||||
int rows = src.rows, cols = src.cols, cn = src.channels(), colsn = cols*cn, depth = src.depth();
|
int rows = src.rows, cols = src.cols, cn = src.channels(), colsn = cols*cn, depth = src.depth();
|
||||||
CV_Assert(depth == CV_8U);
|
CV_Assert(depth == CV_8U);
|
||||||
dst.create(rows, cols, CV_MAKETYPE(DataType<deriv_type>::depth, cn*2));
|
dst.create(rows, cols, CV_MAKETYPE(DataType<deriv_type>::depth, cn*2));
|
||||||
|
|
||||||
|
#ifdef HAVE_TEGRA_OPTIMIZATION
|
||||||
|
if (tegra::calcSharrDeriv(src, dst))
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
|
|
||||||
int x, y, delta = (int)alignSize((cols + 2)*cn, 16);
|
int x, y, delta = (int)alignSize((cols + 2)*cn, 16);
|
||||||
AutoBuffer<deriv_type> _tempBuf(delta*2 + 64);
|
AutoBuffer<deriv_type> _tempBuf(delta*2 + 64);
|
||||||
deriv_type *trow0 = alignPtr(_tempBuf + cn, 16), *trow1 = alignPtr(trow0 + delta, 16);
|
deriv_type *trow0 = alignPtr(_tempBuf + cn, 16), *trow1 = alignPtr(trow0 + delta, 16);
|
||||||
@@ -127,15 +132,15 @@ static void calcSharrDeriv(const Mat& src, Mat& dst)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}//namespace
|
||||||
|
|
||||||
struct LKTrackerInvoker
|
cv::detail::LKTrackerInvoker::LKTrackerInvoker(
|
||||||
{
|
const Mat& _prevImg, const Mat& _prevDeriv, const Mat& _nextImg,
|
||||||
LKTrackerInvoker( const Mat& _prevImg, const Mat& _prevDeriv, const Mat& _nextImg,
|
|
||||||
const Point2f* _prevPts, Point2f* _nextPts,
|
const Point2f* _prevPts, Point2f* _nextPts,
|
||||||
uchar* _status, float* _err,
|
uchar* _status, float* _err,
|
||||||
Size _winSize, TermCriteria _criteria,
|
Size _winSize, TermCriteria _criteria,
|
||||||
int _level, int _maxLevel, int _flags, float _minEigThreshold )
|
int _level, int _maxLevel, int _flags, float _minEigThreshold )
|
||||||
{
|
{
|
||||||
prevImg = &_prevImg;
|
prevImg = &_prevImg;
|
||||||
prevDeriv = &_prevDeriv;
|
prevDeriv = &_prevDeriv;
|
||||||
nextImg = &_nextImg;
|
nextImg = &_nextImg;
|
||||||
@@ -149,10 +154,10 @@ struct LKTrackerInvoker
|
|||||||
maxLevel = _maxLevel;
|
maxLevel = _maxLevel;
|
||||||
flags = _flags;
|
flags = _flags;
|
||||||
minEigThreshold = _minEigThreshold;
|
minEigThreshold = _minEigThreshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(const BlockedRange& range) const
|
void cv::detail::LKTrackerInvoker::operator()(const BlockedRange& range) const
|
||||||
{
|
{
|
||||||
Point2f halfWin((winSize.width-1)*0.5f, (winSize.height-1)*0.5f);
|
Point2f halfWin((winSize.width-1)*0.5f, (winSize.height-1)*0.5f);
|
||||||
const Mat& I = *prevImg;
|
const Mat& I = *prevImg;
|
||||||
const Mat& J = *nextImg;
|
const Mat& J = *nextImg;
|
||||||
@@ -474,26 +479,8 @@ struct LKTrackerInvoker
|
|||||||
err[ptidx] = errval * 1.f/(32*winSize.width*cn*winSize.height);
|
err[ptidx] = errval * 1.f/(32*winSize.width*cn*winSize.height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const Mat* prevImg;
|
|
||||||
const Mat* nextImg;
|
|
||||||
const Mat* prevDeriv;
|
|
||||||
const Point2f* prevPts;
|
|
||||||
Point2f* nextPts;
|
|
||||||
uchar* status;
|
|
||||||
float* err;
|
|
||||||
Size winSize;
|
|
||||||
TermCriteria criteria;
|
|
||||||
int level;
|
|
||||||
int maxLevel;
|
|
||||||
int flags;
|
|
||||||
float minEigThreshold;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int cv::buildOpticalFlowPyramid(InputArray _img, OutputArrayOfArrays pyramid, Size winSize, int maxLevel, bool withDerivatives,
|
int cv::buildOpticalFlowPyramid(InputArray _img, OutputArrayOfArrays pyramid, Size winSize, int maxLevel, bool withDerivatives,
|
||||||
int pyrBorder, int derivBorder, bool tryReuseInputImage)
|
int pyrBorder, int derivBorder, bool tryReuseInputImage)
|
||||||
{
|
{
|
||||||
@@ -503,7 +490,7 @@ int cv::buildOpticalFlowPyramid(InputArray _img, OutputArrayOfArrays pyramid, Si
|
|||||||
|
|
||||||
pyramid.create(1, (maxLevel + 1) * pyrstep, 0 /*type*/, -1, true, 0);
|
pyramid.create(1, (maxLevel + 1) * pyrstep, 0 /*type*/, -1, true, 0);
|
||||||
|
|
||||||
int derivType = CV_MAKETYPE(DataType<deriv_type>::depth, img.channels() * 2);
|
int derivType = CV_MAKETYPE(DataType<cv::detail::deriv_type>::depth, img.channels() * 2);
|
||||||
|
|
||||||
//level 0
|
//level 0
|
||||||
bool lvl0IsSet = false;
|
bool lvl0IsSet = false;
|
||||||
@@ -602,7 +589,7 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
|
|||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
Mat prevPtsMat = _prevPts.getMat();
|
Mat prevPtsMat = _prevPts.getMat();
|
||||||
const int derivDepth = DataType<deriv_type>::depth;
|
const int derivDepth = DataType<cv::detail::deriv_type>::depth;
|
||||||
|
|
||||||
CV_Assert( maxLevel >= 0 && winSize.width > 2 && winSize.height > 2 );
|
CV_Assert( maxLevel >= 0 && winSize.width > 2 && winSize.height > 2 );
|
||||||
|
|
||||||
@@ -744,6 +731,8 @@ void cv::calcOpticalFlowPyrLK( InputArray _prevImg, InputArray _nextImg,
|
|||||||
CV_Assert(prevPyr[level * lvlStep1].size() == nextPyr[level * lvlStep2].size());
|
CV_Assert(prevPyr[level * lvlStep1].size() == nextPyr[level * lvlStep2].size());
|
||||||
CV_Assert(prevPyr[level * lvlStep1].type() == nextPyr[level * lvlStep2].type());
|
CV_Assert(prevPyr[level * lvlStep1].type() == nextPyr[level * lvlStep2].type());
|
||||||
|
|
||||||
|
typedef cv::detail::LKTrackerInvoker LKTrackerInvoker;
|
||||||
|
|
||||||
parallel_for(BlockedRange(0, npoints), LKTrackerInvoker(prevPyr[level * lvlStep1], derivI,
|
parallel_for(BlockedRange(0, npoints), LKTrackerInvoker(prevPyr[level * lvlStep1], derivI,
|
||||||
nextPyr[level * lvlStep2], prevPts, nextPts,
|
nextPyr[level * lvlStep2], prevPts, nextPts,
|
||||||
status, err,
|
status, err,
|
||||||
|
|||||||
36
modules/video/src/lkpyramid.hpp
Normal file
36
modules/video/src/lkpyramid.hpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace cv
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
typedef short deriv_type;
|
||||||
|
|
||||||
|
struct LKTrackerInvoker
|
||||||
|
{
|
||||||
|
LKTrackerInvoker( const Mat& _prevImg, const Mat& _prevDeriv, const Mat& _nextImg,
|
||||||
|
const Point2f* _prevPts, Point2f* _nextPts,
|
||||||
|
uchar* _status, float* _err,
|
||||||
|
Size _winSize, TermCriteria _criteria,
|
||||||
|
int _level, int _maxLevel, int _flags, float _minEigThreshold );
|
||||||
|
|
||||||
|
void operator()(const BlockedRange& range) const;
|
||||||
|
|
||||||
|
const Mat* prevImg;
|
||||||
|
const Mat* nextImg;
|
||||||
|
const Mat* prevDeriv;
|
||||||
|
const Point2f* prevPts;
|
||||||
|
Point2f* nextPts;
|
||||||
|
uchar* status;
|
||||||
|
float* err;
|
||||||
|
Size winSize;
|
||||||
|
TermCriteria criteria;
|
||||||
|
int level;
|
||||||
|
int maxLevel;
|
||||||
|
int flags;
|
||||||
|
float minEigThreshold;
|
||||||
|
};
|
||||||
|
|
||||||
|
}// namespace detail
|
||||||
|
}// namespace cv
|
||||||
Reference in New Issue
Block a user