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
84085873
Unverified
Commit
84085873
authored
5 years ago
by
Alexander Alekhin
Committed by
GitHub
5 years ago
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14888 from vchiluka5:NVIDIA_Optical_Flow
parents
43eba3d7
0fc58496
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
264 deletions
+49
-264
CMakeLists.txt
CMakeLists.txt
+0
-24
OpenCVDetectCUDA.cmake
cmake/OpenCVDetectCUDA.cmake
+49
-0
optical_flow.cpp
samples/gpu/optical_flow.cpp
+0
-240
No files found.
CMakeLists.txt
View file @
84085873
...
...
@@ -918,30 +918,6 @@ configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/custom_hal.hpp.in" "${CMAKE
unset
(
_hal_includes
)
# ----------------------------------------------------------------------------
# Add CUDA libraries (needed for apps/tools, samples)
# ----------------------------------------------------------------------------
if
(
HAVE_CUDA
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDA_LIBRARIES
}
${
CUDA_npp_LIBRARY
}
)
if
(
HAVE_CUBLAS
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDA_cublas_LIBRARY
}
)
endif
()
if
(
HAVE_CUDNN
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDNN_LIBRARIES
}
)
endif
()
if
(
HAVE_CUFFT
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDA_cufft_LIBRARY
}
)
endif
()
foreach
(
p
${
CUDA_LIBS_PATH
}
)
if
(
MSVC AND CMAKE_GENERATOR MATCHES
"Ninja|JOM"
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CMAKE_LIBRARY_PATH_FLAG
}
"
${
p
}
"
)
else
()
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CMAKE_LIBRARY_PATH_FLAG
}${
p
}
)
endif
()
endforeach
()
endif
()
# ----------------------------------------------------------------------------
# Code trace support
# ----------------------------------------------------------------------------
...
...
This diff is collapsed.
Click to expand it.
cmake/OpenCVDetectCUDA.cmake
View file @
84085873
...
...
@@ -337,3 +337,52 @@ if(HAVE_CUDA)
ocv_convert_to_lib_name
(
CUDA_cufft_LIBRARY
${
CUDA_cufft_LIBRARY
}
)
endif
()
endif
()
# ----------------------------------------------------------------------------
# Add CUDA libraries (needed for apps/tools, samples)
# ----------------------------------------------------------------------------
if
(
HAVE_CUDA
)
# details: https://github.com/NVIDIA/nvidia-docker/issues/775
if
(
"
${
CUDA_CUDA_LIBRARY
}
"
MATCHES
"/stubs/libcuda.so"
AND NOT OPENCV_SKIP_CUDA_STUB_WORKAROUND
)
set
(
CUDA_STUB_ENABLED_LINK_WORKAROUND 1
)
if
(
EXISTS
"
${
CUDA_CUDA_LIBRARY
}
"
AND NOT OPENCV_SKIP_CUDA_STUB_WORKAROUND_RPATH_LINK
)
set
(
CUDA_STUB_TARGET_PATH
"
${
CMAKE_BINARY_DIR
}${
CMAKE_FILES_DIRECTORY
}
/"
)
execute_process
(
COMMAND
${
CMAKE_COMMAND
}
-E create_symlink
"
${
CUDA_CUDA_LIBRARY
}
"
"
${
CUDA_STUB_TARGET_PATH
}
/libcuda.so.1"
RESULT_VARIABLE CUDA_STUB_SYMLINK_RESULT
)
if
(
NOT CUDA_STUB_SYMLINK_RESULT EQUAL 0
)
execute_process
(
COMMAND
${
CMAKE_COMMAND
}
-E copy_if_different
"
${
CUDA_CUDA_LIBRARY
}
"
"
${
CUDA_STUB_TARGET_PATH
}
/libcuda.so.1"
RESULT_VARIABLE CUDA_STUB_COPY_RESULT
)
if
(
NOT CUDA_STUB_COPY_RESULT EQUAL 0
)
set
(
CUDA_STUB_ENABLED_LINK_WORKAROUND 0
)
endif
()
endif
()
if
(
CUDA_STUB_ENABLED_LINK_WORKAROUND
)
set
(
CMAKE_EXE_LINKER_FLAGS
"
${
CMAKE_EXE_LINKER_FLAGS
}
-Wl,-rpath-link,
\"
${
CUDA_STUB_TARGET_PATH
}
\"
"
)
endif
()
else
()
set
(
CMAKE_EXE_LINKER_FLAGS
"
${
CMAKE_EXE_LINKER_FLAGS
}
-Wl,--allow-shlib-undefined"
)
endif
()
if
(
NOT CUDA_STUB_ENABLED_LINK_WORKAROUND
)
message
(
WARNING
"CUDA: workaround for stubs/libcuda.so.1 is not applied"
)
endif
()
endif
()
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDA_LIBRARIES
}
${
CUDA_CUDA_LIBRARY
}
${
CUDA_npp_LIBRARY
}
)
if
(
HAVE_CUBLAS
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDA_cublas_LIBRARY
}
)
endif
()
if
(
HAVE_CUDNN
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDNN_LIBRARIES
}
)
endif
()
if
(
HAVE_CUFFT
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CUDA_cufft_LIBRARY
}
)
endif
()
foreach
(
p
${
CUDA_LIBS_PATH
}
)
if
(
MSVC AND CMAKE_GENERATOR MATCHES
"Ninja|JOM"
)
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CMAKE_LIBRARY_PATH_FLAG
}
"
${
p
}
"
)
else
()
set
(
OPENCV_LINKER_LIBS
${
OPENCV_LINKER_LIBS
}
${
CMAKE_LIBRARY_PATH_FLAG
}${
p
}
)
endif
()
endforeach
()
endif
()
This diff is collapsed.
Click to expand it.
samples/gpu/optical_flow.cpp
deleted
100644 → 0
View file @
43eba3d7
#include <iostream>
#include <fstream>
#include "opencv2/core.hpp"
#include <opencv2/core/utility.hpp>
#include "opencv2/highgui.hpp"
#include "opencv2/cudaoptflow.hpp"
#include "opencv2/cudaarithm.hpp"
using
namespace
std
;
using
namespace
cv
;
using
namespace
cv
::
cuda
;
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_flow
)
{
GpuMat
planes
[
2
];
cuda
::
split
(
d_flow
,
planes
);
Mat
flowx
(
planes
[
0
]);
Mat
flowy
(
planes
[
1
]);
Mat
out
;
drawOpticalFlow
(
flowx
,
flowy
,
out
,
10
);
imshow
(
name
,
out
);
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
string
filename1
,
filename2
;
if
(
argc
<
3
)
{
cerr
<<
"Usage : "
<<
argv
[
0
]
<<
" <frame0> <frame1>"
<<
endl
;
filename1
=
"../data/basketball1.png"
;
filename2
=
"../data/basketball2.png"
;
}
else
{
filename1
=
argv
[
1
];
filename2
=
argv
[
2
];
}
Mat
frame0
=
imread
(
filename1
,
IMREAD_GRAYSCALE
);
Mat
frame1
=
imread
(
filename2
,
IMREAD_GRAYSCALE
);
if
(
frame0
.
empty
())
{
cerr
<<
"Can't open image ["
<<
filename1
<<
"]"
<<
endl
;
return
-
1
;
}
if
(
frame1
.
empty
())
{
cerr
<<
"Can't open image ["
<<
filename2
<<
"]"
<<
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_flow
(
frame0
.
size
(),
CV_32FC2
);
Ptr
<
cuda
::
BroxOpticalFlow
>
brox
=
cuda
::
BroxOpticalFlow
::
create
(
0.197
f
,
50.0
f
,
0.8
f
,
10
,
77
,
10
);
Ptr
<
cuda
::
DensePyrLKOpticalFlow
>
lk
=
cuda
::
DensePyrLKOpticalFlow
::
create
(
Size
(
7
,
7
));
Ptr
<
cuda
::
FarnebackOpticalFlow
>
farn
=
cuda
::
FarnebackOpticalFlow
::
create
();
Ptr
<
cuda
::
OpticalFlowDual_TVL1
>
tvl1
=
cuda
::
OpticalFlowDual_TVL1
::
create
();
{
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
->
calc
(
d_frame0f
,
d_frame1f
,
d_flow
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"Brox : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"Brox"
,
d_flow
);
}
{
const
int64
start
=
getTickCount
();
lk
->
calc
(
d_frame0
,
d_frame1
,
d_flow
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"LK : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"LK"
,
d_flow
);
}
{
const
int64
start
=
getTickCount
();
farn
->
calc
(
d_frame0
,
d_frame1
,
d_flow
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"Farn : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"Farn"
,
d_flow
);
}
{
const
int64
start
=
getTickCount
();
tvl1
->
calc
(
d_frame0
,
d_frame1
,
d_flow
);
const
double
timeSec
=
(
getTickCount
()
-
start
)
/
getTickFrequency
();
cout
<<
"TVL1 : "
<<
timeSec
<<
" sec"
<<
endl
;
showFlow
(
"TVL1"
,
d_flow
);
}
imshow
(
"Frame 0"
,
frame0
);
imshow
(
"Frame 1"
,
frame1
);
waitKey
();
return
0
;
}
This diff is collapsed.
Click to expand it.
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