Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
35d63083
Commit
35d63083
authored
Feb 20, 2012
by
Alexey Spizhevoy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Farneback's optical flow sample
parent
504262d3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
0 deletions
+133
-0
farneback_optical_flow.cpp
samples/gpu/farneback_optical_flow.cpp
+133
-0
No files found.
samples/gpu/farneback_optical_flow.cpp
0 → 100644
View file @
35d63083
#include <iostream>
#include <vector>
#include <sstream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/gpu/gpu.hpp"
using
namespace
std
;
using
namespace
cv
;
using
namespace
cv
::
gpu
;
template
<
typename
T
>
inline
T
mapVal
(
T
x
,
T
a
,
T
b
,
T
c
,
T
d
)
{
x
=
::
max
(
::
min
(
x
,
b
),
a
);
return
c
+
(
d
-
c
)
*
(
x
-
a
)
/
(
b
-
a
);
}
void
colorizeFlow
(
const
Mat
&
u
,
const
Mat
&
v
,
Mat
&
dst
)
{
double
uMin
,
uMax
;
minMaxLoc
(
u
,
&
uMin
,
&
uMax
,
0
,
0
);
double
vMin
,
vMax
;
minMaxLoc
(
v
,
&
vMin
,
&
vMax
,
0
,
0
);
uMin
=
::
abs
(
uMin
);
uMax
=
::
abs
(
uMax
);
vMin
=
::
abs
(
vMin
);
vMax
=
::
abs
(
vMax
);
float
dMax
=
::
max
(
::
max
(
uMin
,
uMax
),
::
max
(
vMin
,
vMax
));
dst
.
create
(
u
.
size
(),
CV_8UC3
);
for
(
int
y
=
0
;
y
<
u
.
rows
;
++
y
)
{
for
(
int
x
=
0
;
x
<
u
.
cols
;
++
x
)
{
dst
.
at
<
uchar
>
(
y
,
3
*
x
)
=
0
;
dst
.
at
<
uchar
>
(
y
,
3
*
x
+
1
)
=
(
uchar
)
mapVal
(
-
v
.
at
<
float
>
(
y
,
x
),
-
dMax
,
dMax
,
0.
f
,
255.
f
);
dst
.
at
<
uchar
>
(
y
,
3
*
x
+
2
)
=
(
uchar
)
mapVal
(
u
.
at
<
float
>
(
y
,
x
),
-
dMax
,
dMax
,
0.
f
,
255.
f
);
}
}
}
int
main
(
int
argc
,
char
**
argv
)
{
CommandLineParser
cmd
(
argc
,
argv
,
"{ l | left | | specify left image }"
"{ r | right | | specify right image }"
"{ h | help | false | print help message }"
);
if
(
cmd
.
get
<
bool
>
(
"help"
))
{
cout
<<
"Farneback's optical flow sample.
\n\n
"
<<
"Usage: farneback_optical_flow_gpu [arguments]
\n\n
"
<<
"Arguments:
\n
"
;
cmd
.
printParams
();
return
0
;
}
string
pathL
=
cmd
.
get
<
string
>
(
"left"
);
string
pathR
=
cmd
.
get
<
string
>
(
"right"
);
if
(
pathL
.
empty
())
cout
<<
"Specify left image path
\n
"
;
if
(
pathR
.
empty
())
cout
<<
"Specify right image path
\n
"
;
if
(
pathL
.
empty
()
||
pathR
.
empty
())
return
-
1
;
Mat
frameL
=
imread
(
pathL
,
IMREAD_GRAYSCALE
);
Mat
frameR
=
imread
(
pathR
,
IMREAD_GRAYSCALE
);
if
(
frameL
.
empty
())
cout
<<
"Can't open '"
<<
pathL
<<
"'
\n
"
;
if
(
frameR
.
empty
())
cout
<<
"Can't open '"
<<
pathR
<<
"'
\n
"
;
if
(
frameL
.
empty
()
||
frameR
.
empty
())
return
-
1
;
GpuMat
d_frameL
(
frameL
),
d_frameR
(
frameR
);
GpuMat
d_flowx
,
d_flowy
;
FarnebackOpticalFlow
d_calc
;
Mat
flowxy
,
flowx
,
flowy
,
image
;
bool
running
=
true
,
gpuMode
=
true
;
int64
t
,
t0
=
0
,
t1
=
1
,
tc0
,
tc1
;
cout
<<
"Use 'm' for CPU/GPU toggling
\n
"
;
while
(
running
)
{
t
=
getTickCount
();
if
(
gpuMode
)
{
tc0
=
getTickCount
();
d_calc
(
d_frameL
,
d_frameR
,
d_flowx
,
d_flowy
);
tc1
=
getTickCount
();
d_flowx
.
download
(
flowx
);
d_flowy
.
download
(
flowy
);
}
else
{
tc0
=
getTickCount
();
calcOpticalFlowFarneback
(
frameL
,
frameR
,
flowxy
,
d_calc
.
pyrScale
,
d_calc
.
numLevels
,
d_calc
.
winSize
,
d_calc
.
numIters
,
d_calc
.
polyN
,
d_calc
.
polySigma
,
d_calc
.
flags
);
tc1
=
getTickCount
();
Mat
planes
[]
=
{
flowx
,
flowy
};
split
(
flowxy
,
planes
);
flowx
=
planes
[
0
];
flowy
=
planes
[
1
];
}
colorizeFlow
(
flowx
,
flowy
,
image
);
stringstream
s
;
s
<<
"mode: "
<<
(
gpuMode
?
"GPU"
:
"CPU"
);
putText
(
image
,
s
.
str
(),
Point
(
5
,
25
),
FONT_HERSHEY_SIMPLEX
,
1.
,
Scalar
(
255
,
0
,
255
),
2
);
s
.
str
(
""
);
s
<<
"opt. flow FPS: "
<<
cvRound
((
getTickFrequency
()
/
(
tc1
-
tc0
)));
putText
(
image
,
s
.
str
(),
Point
(
5
,
65
),
FONT_HERSHEY_SIMPLEX
,
1.
,
Scalar
(
255
,
0
,
255
),
2.
);
s
.
str
(
""
);
s
<<
"total FPS: "
<<
cvRound
((
getTickFrequency
()
/
(
t1
-
t0
)));
putText
(
image
,
s
.
str
(),
Point
(
5
,
105
),
FONT_HERSHEY_SIMPLEX
,
1.
,
Scalar
(
255
,
0
,
255
),
2.
);
imshow
(
"flow"
,
image
);
char
ch
=
(
char
)
waitKey
(
3
);
if
(
ch
==
27
)
running
=
false
;
else
if
(
ch
==
'm'
||
ch
==
'M'
)
gpuMode
=
!
gpuMode
;
t0
=
t
;
t1
=
getTickCount
();
}
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment