Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv_contrib
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_contrib
Commits
b32180b2
Commit
b32180b2
authored
Sep 13, 2019
by
Alexander Alekhin
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2255 from sturkmen72:patch-4
parents
260d385e
bf688f0a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
37 deletions
+60
-37
bgfg.cpp
modules/bgsegm/samples/bgfg.cpp
+60
-37
No files found.
modules/bgsegm/samples/bgfg.cpp
View file @
b32180b2
...
@@ -8,15 +8,15 @@ using namespace cv;
...
@@ -8,15 +8,15 @@ using namespace cv;
using
namespace
cv
::
bgsegm
;
using
namespace
cv
::
bgsegm
;
const
String
about
=
const
String
about
=
"
\n
A program demonstrating the use and capabilities of different background subtraction alg
r
orithms
\n
"
"
\n
A program demonstrating the use and capabilities of different background subtraction algorithms
\n
"
"Using OpenCV version "
+
String
(
CV_VERSION
)
+
"Using OpenCV version "
+
String
(
CV_VERSION
)
+
"
\n
Press q or ESC to exit
\n
"
;
"
\n\n
Press 'c' to change the algorithm"
"
\n
Press 'm' to toggle showing only foreground mask or ghost effect"
"
\n
Press 'n' to change number of threads"
"
\n
Press SPACE to toggle wait delay of imshow"
"
\n
Press 'q' or ESC to exit
\n
"
;
const
String
keys
=
const
String
algos
[
7
]
=
{
"GMG"
,
"CNT"
,
"KNN"
,
"MOG"
,
"MOG2"
,
"GSOC"
,
"LSBP"
};
"{help h usage ? | | print this message }"
"{vid | | path to a video file }"
"{algo | GMG | name of the algorithm (GMG, CNT, KNN, MOG, MOG2) }"
;
static
Ptr
<
BackgroundSubtractor
>
createBGSubtractorByName
(
const
String
&
algoName
)
static
Ptr
<
BackgroundSubtractor
>
createBGSubtractorByName
(
const
String
&
algoName
)
{
{
...
@@ -31,42 +31,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
...
@@ -31,42 +31,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
algo
=
createBackgroundSubtractorMOG
();
algo
=
createBackgroundSubtractorMOG
();
else
if
(
algoName
==
String
(
"MOG2"
))
else
if
(
algoName
==
String
(
"MOG2"
))
algo
=
createBackgroundSubtractorMOG2
();
algo
=
createBackgroundSubtractorMOG2
();
else
if
(
algoName
==
String
(
"GSOC"
))
algo
=
createBackgroundSubtractorGSOC
();
else
if
(
algoName
==
String
(
"LSBP"
))
algo
=
createBackgroundSubtractorLSBP
();
return
algo
;
return
algo
;
}
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
CommandLineParser
parser
(
argc
,
argv
,
keys
);
CommandLineParser
parser
(
argc
,
argv
,
"{@video | vtest.avi | path to a video file}"
);
parser
.
about
(
about
);
parser
.
about
(
about
);
parser
.
printMessage
();
parser
.
printMessage
();
if
(
parser
.
has
(
"help"
))
{
parser
.
printMessage
();
return
0
;
}
String
videoPath
=
parser
.
get
<
String
>
(
"vid"
);
String
videoPath
=
samples
::
findFile
(
parser
.
get
<
String
>
(
0
),
false
);
String
algoName
=
parser
.
get
<
String
>
(
"algo"
);
if
(
!
parser
.
check
())
Ptr
<
BackgroundSubtractor
>
bgfs
=
createBGSubtractorByName
(
algos
[
0
]);
{
parser
.
printErrors
();
return
0
;
}
Ptr
<
BackgroundSubtractor
>
bgfs
=
createBGSubtractorByName
(
algoName
);
if
(
!
bgfs
)
{
std
::
cerr
<<
"Failed to create "
<<
algoName
<<
" background subtractor"
<<
std
::
endl
;
return
-
1
;
}
VideoCapture
cap
;
VideoCapture
cap
;
if
(
argc
>
1
)
cap
.
open
(
videoPath
);
cap
.
open
(
videoPath
);
else
cap
.
open
(
0
);
if
(
!
cap
.
isOpened
())
if
(
!
cap
.
isOpened
())
{
{
...
@@ -76,24 +60,63 @@ int main(int argc, char** argv)
...
@@ -76,24 +60,63 @@ int main(int argc, char** argv)
Mat
frame
,
fgmask
,
segm
;
Mat
frame
,
fgmask
,
segm
;
namedWindow
(
"FG Segmentation"
,
WINDOW_NORMAL
);
int
delay
=
30
;
int
algo_index
=
0
;
int
nthreads
=
getNumberOfCPUs
();
bool
show_fgmask
=
false
;
for
(;;)
for
(;;)
{
{
cap
>>
frame
;
cap
>>
frame
;
if
(
frame
.
empty
())
if
(
frame
.
empty
())
break
;
{
cap
.
set
(
CAP_PROP_POS_FRAMES
,
0
);
cap
>>
frame
;
}
bgfs
->
apply
(
frame
,
fgmask
);
bgfs
->
apply
(
frame
,
fgmask
);
frame
.
convertTo
(
segm
,
CV_8U
,
0.5
);
if
(
show_fgmask
)
add
(
frame
,
Scalar
(
100
,
100
,
0
),
segm
,
fgmask
);
segm
=
fgmask
;
else
{
frame
.
convertTo
(
segm
,
CV_8U
,
0.5
);
add
(
frame
,
Scalar
(
100
,
100
,
0
),
segm
,
fgmask
);
}
putText
(
segm
,
algos
[
algo_index
],
Point
(
10
,
30
),
FONT_HERSHEY_PLAIN
,
2.0
,
Scalar
(
255
,
0
,
255
),
2
,
LINE_AA
);
putText
(
segm
,
format
(
"%d threads"
,
nthreads
),
Point
(
10
,
60
),
FONT_HERSHEY_PLAIN
,
2.0
,
Scalar
(
255
,
0
,
255
),
2
,
LINE_AA
);
imshow
(
"FG Segmentation"
,
segm
);
imshow
(
"FG Segmentation"
,
segm
);
int
c
=
waitKey
(
30
);
int
c
=
waitKey
(
delay
);
if
(
c
==
'q'
||
c
==
'Q'
||
(
c
&
255
)
==
27
)
if
(
c
==
' '
)
delay
=
delay
==
30
?
1
:
30
;
if
(
c
==
'c'
||
c
==
'C'
)
{
algo_index
++
;
if
(
algo_index
>
6
)
algo_index
=
0
;
bgfs
=
createBGSubtractorByName
(
algos
[
algo_index
]);
}
if
(
c
==
'n'
||
c
==
'N'
)
{
nthreads
++
;
if
(
nthreads
>
8
)
nthreads
=
1
;
setNumThreads
(
nthreads
);
}
if
(
c
==
'm'
||
c
==
'M'
)
show_fgmask
=
!
show_fgmask
;
if
(
c
==
'q'
||
c
==
'Q'
||
c
==
27
)
break
;
break
;
}
}
...
...
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