Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videostab/src/optical_flow.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "opencv2/video.hpp"
45
#include "opencv2/videostab/optical_flow.hpp"
46
#include "opencv2/videostab/ring_buffer.hpp"
47
48
#ifdef HAVE_OPENCV_CUDAARITHM
49
#include "opencv2/cudaarithm.hpp"
50
#endif
51
52
namespace cv
53
{
54
namespace videostab
55
{
56
57
void SparsePyrLkOptFlowEstimator::run(
58
InputArray frame0, InputArray frame1, InputArray points0, InputOutputArray points1,
59
OutputArray status, OutputArray errors)
60
{
61
calcOpticalFlowPyrLK(frame0, frame1, points0, points1, status, errors, winSize_, maxLevel_);
62
}
63
64
65
#ifdef HAVE_OPENCV_CUDAOPTFLOW
66
67
SparsePyrLkOptFlowEstimatorGpu::SparsePyrLkOptFlowEstimatorGpu()
68
{
69
CV_Assert(cuda::getCudaEnabledDeviceCount() > 0);
70
optFlowEstimator_ = cuda::SparsePyrLKOpticalFlow::create();
71
}
72
73
74
void SparsePyrLkOptFlowEstimatorGpu::run(
75
InputArray frame0, InputArray frame1, InputArray points0, InputOutputArray points1,
76
OutputArray status, OutputArray errors)
77
{
78
frame0_.upload(frame0.getMat());
79
frame1_.upload(frame1.getMat());
80
points0_.upload(points0.getMat());
81
82
if (errors.needed())
83
{
84
run(frame0_, frame1_, points0_, points1_, status_, errors_);
85
errors_.download(errors.getMatRef());
86
}
87
else
88
run(frame0_, frame1_, points0_, points1_, status_);
89
90
points1_.download(points1.getMatRef());
91
status_.download(status.getMatRef());
92
}
93
94
95
void SparsePyrLkOptFlowEstimatorGpu::run(
96
const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, const cuda::GpuMat &points0,
97
cuda::GpuMat &points1, cuda::GpuMat &status, cuda::GpuMat &errors)
98
{
99
optFlowEstimator_->setWinSize(winSize_);
100
optFlowEstimator_->setMaxLevel(maxLevel_);
101
optFlowEstimator_->calc(frame0, frame1, points0, points1, status, errors);
102
}
103
104
105
void SparsePyrLkOptFlowEstimatorGpu::run(
106
const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, const cuda::GpuMat &points0,
107
cuda::GpuMat &points1, cuda::GpuMat &status)
108
{
109
optFlowEstimator_->setWinSize(winSize_);
110
optFlowEstimator_->setMaxLevel(maxLevel_);
111
optFlowEstimator_->calc(frame0, frame1, points0, points1, status);
112
}
113
114
115
DensePyrLkOptFlowEstimatorGpu::DensePyrLkOptFlowEstimatorGpu()
116
{
117
CV_Assert(cuda::getCudaEnabledDeviceCount() > 0);
118
optFlowEstimator_ = cuda::DensePyrLKOpticalFlow::create();
119
}
120
121
122
void DensePyrLkOptFlowEstimatorGpu::run(
123
InputArray frame0, InputArray frame1, InputOutputArray flowX, InputOutputArray flowY,
124
OutputArray errors)
125
{
126
frame0_.upload(frame0.getMat());
127
frame1_.upload(frame1.getMat());
128
129
optFlowEstimator_->setWinSize(winSize_);
130
optFlowEstimator_->setMaxLevel(maxLevel_);
131
132
if (errors.needed())
133
{
134
CV_Error(Error::StsNotImplemented, "DensePyrLkOptFlowEstimatorGpu doesn't support errors calculation");
135
}
136
else
137
{
138
cuda::GpuMat flow;
139
optFlowEstimator_->calc(frame0_, frame1_, flow);
140
141
cuda::GpuMat flows[2];
142
cuda::split(flow, flows);
143
144
flowX_ = flows[0];
145
flowY_ = flows[1];
146
}
147
148
flowX_.download(flowX.getMatRef());
149
flowY_.download(flowY.getMatRef());
150
}
151
152
#endif // HAVE_OPENCV_CUDAOPTFLOW
153
154
} // namespace videostab
155
} // namespace cv
156
157