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
f1c549fa
Commit
f1c549fa
authored
Jun 19, 2013
by
yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
revise ocl samples, add tvl1 sample
parent
2c198f6c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
290 additions
and
35 deletions
+290
-35
facedetect.cpp
samples/ocl/facedetect.cpp
+0
-0
hog.cpp
samples/ocl/hog.cpp
+0
-0
pyrlk_optical_flow.cpp
samples/ocl/pyrlk_optical_flow.cpp
+24
-35
squares.cpp
samples/ocl/squares.cpp
+0
-0
stereo_match.cpp
samples/ocl/stereo_match.cpp
+0
-0
surf_matcher.cpp
samples/ocl/surf_matcher.cpp
+0
-0
tvl1_optical_flow.cpp
samples/ocl/tvl1_optical_flow.cpp
+266
-0
No files found.
samples/ocl/facedetect.cpp
View file @
f1c549fa
This diff is collapsed.
Click to expand it.
samples/ocl/hog.cpp
View file @
f1c549fa
This diff is collapsed.
Click to expand it.
samples/ocl/pyrlk_optical_flow.cpp
View file @
f1c549fa
...
...
@@ -11,19 +11,20 @@ using namespace cv;
using
namespace
cv
::
ocl
;
typedef
unsigned
char
uchar
;
#define LOOP_NUM 10
#define LOOP_NUM 10
int64
work_begin
=
0
;
int64
work_end
=
0
;
static
void
workBegin
()
{
static
void
workBegin
()
{
work_begin
=
getTickCount
();
}
static
void
workEnd
()
{
work_end
+=
(
getTickCount
()
-
work_begin
);
}
static
double
getTime
(){
static
double
getTime
()
{
return
work_end
*
1000.
/
getTickFrequency
();
}
...
...
@@ -93,14 +94,15 @@ int main(int argc, const char* argv[])
//set this to save kernel compile time from second time you run
ocl
::
setBinpath
(
"./"
);
const
char
*
keys
=
"{ h | help | false | print help message }"
"{ l | left | | specify left image }"
"{ r | right | | specify right image }"
"{ c | camera | 0 | enable camera capturing }"
"{ s | use_cpu | false | use cpu or gpu to process the image }"
"{ v | video | | use video as input }"
"{ points | points | 1000 | specify points count [GoodFeatureToTrack] }"
"{ min_dist | min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }"
;
"{ h | help | false | print help message }"
"{ l | left | | specify left image }"
"{ r | right | | specify right image }"
"{ c | camera | 0 | specify camera id }"
"{ s | use_cpu | false | use cpu or gpu to process the image }"
"{ v | video | | use video as input }"
"{ o | output | pyrlk_output.jpg| specify output save path when input is images }"
"{ p | points | 1000 | specify points count [GoodFeatureToTrack] }"
"{ m | min_dist | 0 | specify minimal distance between points [GoodFeatureToTrack] }"
;
CommandLineParser
cmd
(
argc
,
argv
,
keys
);
...
...
@@ -113,13 +115,13 @@ int main(int argc, const char* argv[])
}
bool
defaultPicturesFail
=
false
;
string
fname0
=
cmd
.
get
<
string
>
(
"left"
);
string
fname1
=
cmd
.
get
<
string
>
(
"right"
);
string
vdofile
=
cmd
.
get
<
string
>
(
"video"
);
int
points
=
cmd
.
get
<
int
>
(
"points"
);
double
minDist
=
cmd
.
get
<
double
>
(
"min_dist"
);
string
fname0
=
cmd
.
get
<
string
>
(
"l"
);
string
fname1
=
cmd
.
get
<
string
>
(
"r"
);
string
vdofile
=
cmd
.
get
<
string
>
(
"v"
);
string
outfile
=
cmd
.
get
<
string
>
(
"o"
);
int
points
=
cmd
.
get
<
int
>
(
"p"
);
double
minDist
=
cmd
.
get
<
double
>
(
"m"
);
bool
useCPU
=
cmd
.
get
<
bool
>
(
"s"
);
bool
useCamera
=
cmd
.
get
<
bool
>
(
"c"
);
int
inputName
=
cmd
.
get
<
int
>
(
"c"
);
oclMat
d_nextPts
,
d_status
;
...
...
@@ -132,22 +134,9 @@ int main(int argc, const char* argv[])
vector
<
unsigned
char
>
status
(
points
);
vector
<
float
>
err
;
if
(
frame0
.
empty
()
||
frame1
.
empty
())
{
useCamera
=
true
;
defaultPicturesFail
=
true
;
CvCapture
*
capture
=
0
;
capture
=
cvCaptureFromCAM
(
inputName
);
if
(
!
capture
)
{
cout
<<
"Can't load input images"
<<
endl
;
return
-
1
;
}
}
cout
<<
"Points count : "
<<
points
<<
endl
<<
endl
;
if
(
useCamera
)
if
(
frame0
.
empty
()
||
frame1
.
empty
()
)
{
CvCapture
*
capture
=
0
;
Mat
frame
,
frameCopy
;
...
...
@@ -241,10 +230,10 @@ _cleanup_:
else
{
nocamera:
for
(
int
i
=
0
;
i
<=
LOOP_NUM
;
i
++
)
for
(
int
i
=
0
;
i
<=
LOOP_NUM
;
i
++
)
{
cout
<<
"loop"
<<
i
<<
endl
;
if
(
i
>
0
)
workBegin
();
if
(
i
>
0
)
workBegin
();
if
(
useCPU
)
{
...
...
@@ -274,8 +263,8 @@ nocamera:
cout
<<
getTime
()
/
LOOP_NUM
<<
" ms"
<<
endl
;
drawArrows
(
frame0
,
pts
,
nextPts
,
status
,
Scalar
(
255
,
0
,
0
));
imshow
(
"PyrLK [Sparse]"
,
frame0
);
imwrite
(
outfile
,
frame0
);
}
}
}
...
...
samples/ocl/squares.cpp
View file @
f1c549fa
This diff is collapsed.
Click to expand it.
samples/ocl/stereo_match.cpp
View file @
f1c549fa
This diff is collapsed.
Click to expand it.
samples/ocl/surf_matcher.cpp
View file @
f1c549fa
This diff is collapsed.
Click to expand it.
samples/ocl/tvl1_optical_flow.cpp
0 → 100644
View file @
f1c549fa
#include <iostream>
#include <vector>
#include <iomanip>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/ocl/ocl.hpp"
#include "opencv2/video/video.hpp"
using
namespace
std
;
using
namespace
cv
;
using
namespace
cv
::
ocl
;
typedef
unsigned
char
uchar
;
#define LOOP_NUM 10
int64
work_begin
=
0
;
int64
work_end
=
0
;
static
void
workBegin
()
{
work_begin
=
getTickCount
();
}
static
void
workEnd
()
{
work_end
+=
(
getTickCount
()
-
work_begin
);
}
static
double
getTime
()
{
return
work_end
*
1000.
/
getTickFrequency
();
}
template
<
typename
T
>
inline
T
clamp
(
T
x
,
T
a
,
T
b
)
{
return
((
x
)
>
(
a
)
?
((
x
)
<
(
b
)
?
(
x
)
:
(
b
))
:
(
a
));
}
template
<
typename
T
>
inline
T
mapValue
(
T
x
,
T
a
,
T
b
,
T
c
,
T
d
)
{
x
=
clamp
(
x
,
a
,
b
);
return
c
+
(
d
-
c
)
*
(
x
-
a
)
/
(
b
-
a
);
}
static
void
getFlowField
(
const
Mat
&
u
,
const
Mat
&
v
,
Mat
&
flowField
)
{
float
maxDisplacement
=
1.0
f
;
for
(
int
i
=
0
;
i
<
u
.
rows
;
++
i
)
{
const
float
*
ptr_u
=
u
.
ptr
<
float
>
(
i
);
const
float
*
ptr_v
=
v
.
ptr
<
float
>
(
i
);
for
(
int
j
=
0
;
j
<
u
.
cols
;
++
j
)
{
float
d
=
max
(
fabsf
(
ptr_u
[
j
]),
fabsf
(
ptr_v
[
j
]));
if
(
d
>
maxDisplacement
)
maxDisplacement
=
d
;
}
}
flowField
.
create
(
u
.
size
(),
CV_8UC4
);
for
(
int
i
=
0
;
i
<
flowField
.
rows
;
++
i
)
{
const
float
*
ptr_u
=
u
.
ptr
<
float
>
(
i
);
const
float
*
ptr_v
=
v
.
ptr
<
float
>
(
i
);
Vec4b
*
row
=
flowField
.
ptr
<
Vec4b
>
(
i
);
for
(
int
j
=
0
;
j
<
flowField
.
cols
;
++
j
)
{
row
[
j
][
0
]
=
0
;
row
[
j
][
1
]
=
static_cast
<
unsigned
char
>
(
mapValue
(
-
ptr_v
[
j
],
-
maxDisplacement
,
maxDisplacement
,
0.0
f
,
255.0
f
));
row
[
j
][
2
]
=
static_cast
<
unsigned
char
>
(
mapValue
(
ptr_u
[
j
],
-
maxDisplacement
,
maxDisplacement
,
0.0
f
,
255.0
f
));
row
[
j
][
3
]
=
255
;
}
}
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
static
std
::
vector
<
Info
>
ocl_info
;
ocl
::
getDevice
(
ocl_info
);
//if you want to use undefault device, set it here
setDevice
(
ocl_info
[
0
]);
//set this to save kernel compile time from second time you run
ocl
::
setBinpath
(
"./"
);
const
char
*
keys
=
"{ h | help | false | print help message }"
"{ l | left | | specify left image }"
"{ r | right | | specify right image }"
"{ o | output | tvl1_output.jpg | specify output save path }"
"{ c | camera | 0 | enable camera capturing }"
"{ s | use_cpu | false | use cpu or gpu to process the image }"
"{ v | video | | use video as input }"
;
CommandLineParser
cmd
(
argc
,
argv
,
keys
);
if
(
cmd
.
get
<
bool
>
(
"help"
))
{
cout
<<
"Usage: pyrlk_optical_flow [options]"
<<
endl
;
cout
<<
"Avaible options:"
<<
endl
;
cmd
.
printParams
();
return
0
;
}
bool
defaultPicturesFail
=
false
;
string
fname0
=
cmd
.
get
<
string
>
(
"l"
);
string
fname1
=
cmd
.
get
<
string
>
(
"r"
);
string
vdofile
=
cmd
.
get
<
string
>
(
"v"
);
string
outpath
=
cmd
.
get
<
string
>
(
"o"
);
bool
useCPU
=
cmd
.
get
<
bool
>
(
"s"
);
bool
useCamera
=
cmd
.
get
<
bool
>
(
"c"
);
int
inputName
=
cmd
.
get
<
int
>
(
"c"
);
Mat
frame0
=
imread
(
fname0
,
cv
::
IMREAD_GRAYSCALE
);
Mat
frame1
=
imread
(
fname1
,
cv
::
IMREAD_GRAYSCALE
);
cv
::
Ptr
<
cv
::
DenseOpticalFlow
>
alg
=
cv
::
createOptFlow_DualTVL1
();
cv
::
ocl
::
OpticalFlowDual_TVL1_OCL
d_alg
;
Mat
flow
,
show_flow
;
Mat
flow_vec
[
2
];
if
(
frame0
.
empty
()
||
frame1
.
empty
())
{
useCamera
=
true
;
defaultPicturesFail
=
true
;
CvCapture
*
capture
=
0
;
capture
=
cvCaptureFromCAM
(
inputName
);
if
(
!
capture
)
{
cout
<<
"Can't load input images"
<<
endl
;
return
-
1
;
}
}
if
(
useCamera
)
{
CvCapture
*
capture
=
0
;
Mat
frame
,
frameCopy
;
Mat
frame0Gray
,
frame1Gray
;
Mat
ptr0
,
ptr1
;
if
(
vdofile
==
""
)
capture
=
cvCaptureFromCAM
(
inputName
);
else
capture
=
cvCreateFileCapture
(
vdofile
.
c_str
());
int
c
=
inputName
;
if
(
!
capture
)
{
if
(
vdofile
==
""
)
cout
<<
"Capture from CAM "
<<
c
<<
" didn't work"
<<
endl
;
else
cout
<<
"Capture from file "
<<
vdofile
<<
" failed"
<<
endl
;
if
(
defaultPicturesFail
)
{
return
-
1
;
}
goto
nocamera
;
}
cout
<<
"In capture ..."
<<
endl
;
for
(
int
i
=
0
;;
i
++
)
{
frame
=
cvQueryFrame
(
capture
);
if
(
frame
.
empty
()
)
break
;
if
(
i
==
0
)
{
frame
.
copyTo
(
frame0
);
cvtColor
(
frame0
,
frame0Gray
,
COLOR_BGR2GRAY
);
}
else
{
if
(
i
%
2
==
1
)
{
frame
.
copyTo
(
frame1
);
cvtColor
(
frame1
,
frame1Gray
,
COLOR_BGR2GRAY
);
ptr0
=
frame0Gray
;
ptr1
=
frame1Gray
;
}
else
{
frame
.
copyTo
(
frame0
);
cvtColor
(
frame0
,
frame0Gray
,
COLOR_BGR2GRAY
);
ptr0
=
frame1Gray
;
ptr1
=
frame0Gray
;
}
if
(
useCPU
)
{
alg
->
calc
(
ptr0
,
ptr1
,
flow
);
split
(
flow
,
flow_vec
);
}
else
{
oclMat
d_flowx
,
d_flowy
;
d_alg
(
oclMat
(
ptr0
),
oclMat
(
ptr1
),
d_flowx
,
d_flowy
);
d_flowx
.
download
(
flow_vec
[
0
]);
d_flowy
.
download
(
flow_vec
[
1
]);
}
if
(
i
%
2
==
1
)
frame1
.
copyTo
(
frameCopy
);
else
frame0
.
copyTo
(
frameCopy
);
getFlowField
(
flow_vec
[
0
],
flow_vec
[
1
],
show_flow
);
imshow
(
"PyrLK [Sparse]"
,
show_flow
);
}
if
(
waitKey
(
10
)
>=
0
)
goto
_cleanup_
;
}
waitKey
(
0
);
_cleanup_
:
cvReleaseCapture
(
&
capture
);
}
else
{
nocamera
:
oclMat
d_flowx
,
d_flowy
;
for
(
int
i
=
0
;
i
<=
LOOP_NUM
;
i
++
)
{
cout
<<
"loop"
<<
i
<<
endl
;
if
(
i
>
0
)
workBegin
();
if
(
useCPU
)
{
alg
->
calc
(
frame0
,
frame1
,
flow
);
split
(
flow
,
flow_vec
);
}
else
{
d_alg
(
oclMat
(
frame0
),
oclMat
(
frame1
),
d_flowx
,
d_flowy
);
d_flowx
.
download
(
flow_vec
[
0
]);
d_flowy
.
download
(
flow_vec
[
1
]);
}
if
(
i
>
0
&&
i
<=
LOOP_NUM
)
workEnd
();
if
(
i
==
LOOP_NUM
)
{
if
(
useCPU
)
cout
<<
"average CPU time (noCamera) : "
;
else
cout
<<
"average GPU time (noCamera) : "
;
cout
<<
getTime
()
/
LOOP_NUM
<<
" ms"
<<
endl
;
getFlowField
(
flow_vec
[
0
],
flow_vec
[
1
],
show_flow
);
imshow
(
"PyrLK [Sparse]"
,
show_flow
);
imwrite
(
outpath
,
show_flow
);
}
}
}
waitKey
();
return
0
;
}
\ No newline at end of file
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