Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Greenplum
Opencv
提交
aa73bf60
O
Opencv
项目概览
Greenplum
/
Opencv
大约 1 年 前同步成功
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
Opencv
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
aa73bf60
编写于
2月 13, 2013
作者:
V
Vladislav Vinogradov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added gpu optical flow sample
上级
50f28c9e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
253 addition
and
0 deletion
+253
-0
samples/gpu/optical_flow.cpp
samples/gpu/optical_flow.cpp
+253
-0
未找到文件。
samples/gpu/optical_flow.cpp
0 → 100644
浏览文件 @
aa73bf60
#include <iostream>
#include <fstream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
using
namespace
std
;
using
namespace
cv
;
using
namespace
cv
::
gpu
;
inline
bool
isFlowCorrect
(
Point2f
u
)
{
return
!
cvIsNaN
(
u
.
x
)
&&
!
cvIsNaN
(
u
.
y
)
&&
fabs
(
u
.
x
)
<
1e9
&&
fabs
(
u
.
y
)
<
1e9
;
}
static
Vec3b
computeColor
(
float
fx
,
float
fy
)
{
static
bool
first
=
true
;
// relative lengths of color transitions:
// these are chosen based on perceptual similarity
// (e.g. one can distinguish more shades between red and yellow
// than between yellow and green)
const
int
RY
=
15
;
const
int
YG
=
6
;
const
int
GC
=
4
;
const
int
CB
=
11
;
const
int
BM
=
13
;
const
int
MR
=
6
;
const
int
NCOLS
=
RY
+
YG
+
GC
+
CB
+
BM
+
MR
;
static
Vec3i
colorWheel
[
NCOLS
];
if
(
first
)
{
int
k
=
0
;
for
(
int
i
=
0
;
i
<
RY
;
++
i
,
++
k
)
colorWheel
[
k
]
=
Vec3i
(
255
,
255
*
i
/
RY
,
0
);
for
(
int
i
=
0
;
i
<
YG
;
++
i
,
++
k
)
colorWheel
[
k
]
=
Vec3i
(
255
-
255
*
i
/
YG
,
255
,
0
);
for
(
int
i
=
0
;
i
<
GC
;
++
i
,
++
k
)
colorWheel
[
k
]
=
Vec3i
(
0
,
255
,
255
*
i
/
GC
);
for
(
int
i
=
0
;
i
<
CB
;
++
i
,
++
k
)
colorWheel
[
k
]
=
Vec3i
(
0
,
255
-
255
*
i
/
CB
,
255
);
for
(
int
i
=
0
;
i
<
BM
;
++
i
,
++
k
)
colorWheel
[
k
]
=
Vec3i
(
255
*
i
/
BM
,
0
,
255
);
for
(
int
i
=
0
;
i
<
MR
;
++
i
,
++
k
)
colorWheel
[
k
]
=
Vec3i
(
255
,
0
,
255
-
255
*
i
/
MR
);
first
=
false
;
}
const
float
rad
=
sqrt
(
fx
*
fx
+
fy
*
fy
);
const
float
a
=
atan2
(
-
fy
,
-
fx
)
/
(
float
)
CV_PI
;
const
float
fk
=
(
a
+
1.0
f
)
/
2.0
f
*
(
NCOLS
-
1
);
const
int
k0
=
static_cast
<
int
>
(
fk
);
const
int
k1
=
(
k0
+
1
)
%
NCOLS
;
const
float
f
=
fk
-
k0
;
Vec3b
pix
;
for
(
int
b
=
0
;
b
<
3
;
b
++
)
{
const
float
col0
=
colorWheel
[
k0
][
b
]
/
255.0
f
;
const
float
col1
=
colorWheel
[
k1
][
b
]
/
255.0
f
;
float
col
=
(
1
-
f
)
*
col0
+
f
*
col1
;
if
(
rad
<=
1
)
col
=
1
-
rad
*
(
1
-
col
);
// increase saturation with radius
else
col
*=
.75
;
// out of range
pix
[
2
-
b
]
=
static_cast
<
uchar
>
(
255.0
*
col
);
}
return
pix
;
}
static
void
drawOpticalFlow
(
const
Mat_
<
float
>&
flowx
,
const
Mat_
<
float
>&
flowy
,
Mat
&
dst
,
float
maxmotion
=
-
1
)
{
dst
.
create
(
flowx
.
size
(),
CV_8UC3
);
dst
.
setTo
(
Scalar
::
all
(
0
));
// determine motion range:
float
maxrad
=
maxmotion
;
if
(
maxmotion
<=
0
)
{
maxrad
=
1
;
for
(
int
y
=
0
;
y
<
flowx
.
rows
;
++
y
)
{
for
(
int
x
=
0
;
x
<
flowx
.
cols
;
++
x
)
{
Point2f
u
(
flowx
(
y
,
x
),
flowy
(
y
,
x
));
if
(
!
isFlowCorrect
(
u
))
continue
;
maxrad
=
max
(
maxrad
,
sqrt
(
u
.
x
*
u
.
x
+
u
.
y
*
u
.
y
));
}
}
}
for
(
int
y
=
0
;
y
<
flowx
.
rows
;
++
y
)
{
for
(
int
x
=
0
;
x
<
flowx
.
cols
;
++
x
)
{
Point2f
u
(
flowx
(
y
,
x
),
flowy
(
y
,
x
));
if
(
isFlowCorrect
(
u
))
dst
.
at
<
Vec3b
>
(
y
,
x
)
=
computeColor
(
u
.
x
/
maxrad
,
u
.
y
/
maxrad
);
}
}
}
static
void
showFlow
(
const
char
*
name
,
const
GpuMat
&
d_flowx
,
const
GpuMat
&
d_flowy
)
{
Mat
flowx
(
d_flowx
);
Mat
flowy
(
d_flowy
);
Mat
out
;
drawOpticalFlow
(
flowx
,
flowy
,
out
,
10
);
imshow
(
name
,
out
);
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
if
(
argc
<
3
)
{
cerr
<<
"Usage : "
<<
argv
[
0
]
<<
"<frame0> <frame1>"
<<
endl
;
return
-
1
;
}
Mat
frame0
=
imread
(
argv
[
1
],
IMREAD_GRAYSCALE
);
Mat
frame1
=
imread
(
argv
[
2
],
IMREAD_GRAYSCALE
);
if
(
frame0
.
empty
())
{
cerr
<<
"Can't open image ["
<<
argv
[
1
]
<<
"]"
<<
endl
;
return
-
1
;
}
if
(
frame1
.
empty
())
{
cerr
<<
"Can't open image ["
<<
argv
[
2
]
<<
"]"
<<
endl
;
return
-
1
;
}
if
(
frame1
.
size
()
!=
frame0
.
size
())
{
cerr
<<
"Images should be of equal sizes"
<<
endl
;
return
-
1
;
}
GpuMat
d_frame0
(
frame0
);
GpuMat
d_frame1
(
frame1
);
GpuMat
d_flowx
(
frame0
.
size
(),
CV_32FC1
);
GpuMat
d_flowy
(
frame0
.
size
(),
CV_32FC1
);
BroxOpticalFlow
brox
(
0.197
f
,
50.0
f
,
0.8
f
,
10
,
77
,
10
);
PyrLKOpticalFlow
lk
;
lk
.
winSize
=
Size
(
7
,
7
);
FarnebackOpticalFlow
farn
;
OpticalFlowDual_TVL1_GPU
tvl1
;
FastOpticalFlowBM
fastBM
;
{
GpuMat
d_frame0f
;
GpuMat
d_frame1f
;
d_frame0
.
convertTo
(
d_frame0f
,
CV_32F
,
1.0
/
255.0
);
d_frame1
.
convertTo
(
d_frame1f
,
CV_32F
,
1.0
/
255.0
);
const
int64
start
=
getTickCount
();
brox
(
d_frame0f
,
d_frame1f
,
d_flowx
,
d_flowy
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"Brox : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"Brox"
,
d_flowx
,
d_flowy
);
}
{
const
int64
start
=
getTickCount
();
lk
.
dense
(
d_frame0
,
d_frame1
,
d_flowx
,
d_flowy
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"LK : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"LK"
,
d_flowx
,
d_flowy
);
}
{
const
int64
start
=
getTickCount
();
farn
(
d_frame0
,
d_frame1
,
d_flowx
,
d_flowy
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"Farn : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"Farn"
,
d_flowx
,
d_flowy
);
}
{
const
int64
start
=
getTickCount
();
tvl1
(
d_frame0
,
d_frame1
,
d_flowx
,
d_flowy
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"TVL1 : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"TVL1"
,
d_flowx
,
d_flowy
);
}
{
const
int64
start
=
getTickCount
();
GpuMat
buf
;
calcOpticalFlowBM
(
d_frame0
,
d_frame1
,
Size
(
7
,
7
),
Size
(
1
,
1
),
Size
(
21
,
21
),
false
,
d_flowx
,
d_flowy
,
buf
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"BM : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"BM"
,
d_flowx
,
d_flowy
);
}
{
const
int64
start
=
getTickCount
();
fastBM
(
d_frame0
,
d_frame1
,
d_flowx
,
d_flowy
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"Fast BM : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"Fast BM"
,
d_flowx
,
d_flowy
);
}
imshow
(
"Frame 0"
,
frame0
);
imshow
(
"Frame 1"
,
frame1
);
waitKey
();
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录