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
4bea70a6
Commit
4bea70a6
authored
Oct 26, 2018
by
catree
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update background subtraction tutorial with Java and Python codes.
parent
defeda2f
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
184 additions
and
148 deletions
+184
-148
background_subtraction.markdown
...eo/background_subtraction/background_subtraction.markdown
+0
-0
Background_Subtraction_Tutorial_Result_1.png
...ction/images/Background_Subtraction_Tutorial_Result_1.png
+0
-0
Background_Subtraction_Tutorial_Result_2.png
...ction/images/Background_Subtraction_Tutorial_Result_2.png
+0
-0
Background_Subtraction_Tutorial_frame.jpg
...traction/images/Background_Subtraction_Tutorial_frame.jpg
+0
-0
Background_Subtraction_Tutorial_result_KNN.jpg
...ion/images/Background_Subtraction_Tutorial_result_KNN.jpg
+0
-0
Background_Subtraction_Tutorial_result_MOG2.jpg
...on/images/Background_Subtraction_Tutorial_result_MOG2.jpg
+0
-0
table_of_content_video.markdown
doc/tutorials/video/table_of_content_video.markdown
+2
-0
bg_sub.cpp
samples/cpp/tutorial_code/video/bg_sub.cpp
+52
-148
BackgroundSubtractionDemo.java
...deo/background_subtraction/BackgroundSubtractionDemo.java
+79
-0
bg_sub.py
...thon/tutorial_code/video/background_subtraction/bg_sub.py
+51
-0
No files found.
doc/tutorials/video/background_subtraction/background_subtraction.markdown
View file @
4bea70a6
This diff is collapsed.
Click to expand it.
doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_Result_1.png
deleted
100644 → 0
View file @
defeda2f
83.6 KB
doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_Result_2.png
deleted
100644 → 0
View file @
defeda2f
93.6 KB
doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_frame.jpg
0 → 100644
View file @
4bea70a6
90.8 KB
doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_result_KNN.jpg
0 → 100644
View file @
4bea70a6
13.4 KB
doc/tutorials/video/background_subtraction/images/Background_Subtraction_Tutorial_result_MOG2.jpg
0 → 100644
View file @
4bea70a6
21.5 KB
doc/tutorials/video/table_of_content_video.markdown
View file @
4bea70a6
...
...
@@ -6,6 +6,8 @@ tracking and foreground extractions.
-
@subpage tutorial_background_subtraction
*Languages:* C++, Java, Python
*Compatibility:* \> OpenCV 2.4.6
*Author:* Domenico Daniele Bloisi
...
...
samples/cpp/tutorial_code/video/bg_sub.cpp
View file @
4bea70a6
...
...
@@ -4,180 +4,84 @@
* @author Domenico D. Bloisi
*/
//opencv
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
using
namespace
cv
;
using
namespace
std
;
// Global variables
Mat
frame
;
//current frame
Mat
fgMaskMOG2
;
//fg mask fg mask generated by MOG2 method
Ptr
<
BackgroundSubtractor
>
pMOG2
;
//MOG2 Background subtractor
char
keyboard
;
//input from keyboard
/** Function Headers */
void
help
();
void
processVideo
(
char
*
videoFilename
);
void
processImages
(
char
*
firstFrameFilename
);
const
char
*
params
=
"{ help h | | Print usage }"
"{ input | ../data/vtest.avi | Path to a video or a sequence of image }"
"{ algo | MOG2 | Background subtraction method (KNN, MOG2) }"
;
void
help
()
{
cout
<<
"--------------------------------------------------------------------------"
<<
endl
<<
"This program shows how to use background subtraction methods provided by "
<<
endl
<<
" OpenCV. You can process both videos (-vid) and images (-img)."
<<
endl
<<
endl
<<
"Usage:"
<<
endl
<<
"./bg_sub {-vid <video filename>|-img <image filename>}"
<<
endl
<<
"for example: ./bg_sub -vid video.avi"
<<
endl
<<
"or: ./bg_sub -img /data/images/1.png"
<<
endl
<<
"--------------------------------------------------------------------------"
<<
endl
<<
endl
;
}
/**
* @function main
*/
int
main
(
int
argc
,
char
*
argv
[])
{
//print help information
help
();
//check for the input parameter correctness
if
(
argc
!=
3
)
{
cerr
<<
"Incorret input list"
<<
endl
;
cerr
<<
"exiting..."
<<
endl
;
return
EXIT_FAILURE
;
CommandLineParser
parser
(
argc
,
argv
,
params
);
parser
.
about
(
"This program shows how to use background subtraction methods provided by "
" OpenCV. You can process both videos and images.
\n
"
);
if
(
parser
.
has
(
"help"
))
{
//print help information
parser
.
printMessage
();
}
//create GUI windows
namedWindow
(
"Frame"
);
namedWindow
(
"FG Mask MOG 2"
);
//! [create]
//create Background Subtractor objects
pMOG2
=
createBackgroundSubtractorMOG2
();
//MOG2 approach
if
(
strcmp
(
argv
[
1
],
"-vid"
)
==
0
)
{
//input data coming from a video
processVideo
(
argv
[
2
]);
}
else
if
(
strcmp
(
argv
[
1
],
"-img"
)
==
0
)
{
//input data coming from a sequence of images
processImages
(
argv
[
2
]);
}
else
{
//error in reading input parameters
cerr
<<
"Please, check the input parameters."
<<
endl
;
cerr
<<
"Exiting..."
<<
endl
;
return
EXIT_FAILURE
;
}
//destroy GUI windows
destroyAllWindows
();
return
EXIT_SUCCESS
;
}
Ptr
<
BackgroundSubtractor
>
pBackSub
;
if
(
parser
.
get
<
String
>
(
"algo"
)
==
"MOG2"
)
pBackSub
=
createBackgroundSubtractorMOG2
();
else
pBackSub
=
createBackgroundSubtractorKNN
();
//! [create]
/**
* @function processVideo
*/
void
processVideo
(
char
*
videoFilename
)
{
//create the capture object
VideoCapture
capture
(
videoFilename
);
if
(
!
capture
.
isOpened
()){
//! [capture]
VideoCapture
capture
(
parser
.
get
<
String
>
(
"input"
));
if
(
!
capture
.
isOpened
()){
//error in opening the video input
cerr
<<
"Unable to open
video file: "
<<
videoFilename
<<
endl
;
exit
(
EXIT_FAILURE
)
;
cerr
<<
"Unable to open
: "
<<
parser
.
get
<
String
>
(
"input"
)
<<
endl
;
return
0
;
}
//
read input data. ESC or 'q' for quitting
keyboard
=
0
;
while
(
keyboard
!=
'q'
&&
keyboard
!=
27
){
//read the current frame
if
(
!
capture
.
read
(
frame
))
{
cerr
<<
"Unable to read next frame."
<<
endl
;
cerr
<<
"Exiting..."
<<
endl
;
exit
(
EXIT_FAILURE
);
}
//
! [capture]
Mat
frame
,
fgMask
;
while
(
true
)
{
capture
>>
frame
;
if
(
frame
.
empty
())
break
;
//! [apply]
//update the background model
pMOG2
->
apply
(
frame
,
fgMaskMOG2
);
pBackSub
->
apply
(
frame
,
fgMask
);
//! [apply]
//! [display_frame_number]
//get the frame number and write it on the current frame
stringstream
ss
;
rectangle
(
frame
,
cv
::
Point
(
10
,
2
),
cv
::
Point
(
100
,
20
),
cv
::
Scalar
(
255
,
255
,
255
),
-
1
);
stringstream
ss
;
ss
<<
capture
.
get
(
CAP_PROP_POS_FRAMES
);
string
frameNumberString
=
ss
.
str
();
putText
(
frame
,
frameNumberString
.
c_str
(),
cv
::
Point
(
15
,
15
),
FONT_HERSHEY_SIMPLEX
,
0.5
,
cv
::
Scalar
(
0
,
0
,
0
));
//show the current frame and the fg masks
imshow
(
"Frame"
,
frame
);
imshow
(
"FG Mask MOG 2"
,
fgMaskMOG2
);
//get the input from the keyboard
keyboard
=
(
char
)
waitKey
(
30
);
}
//delete capture object
capture
.
release
();
}
//! [display_frame_number]
/**
* @function processImages
*/
void
processImages
(
char
*
fistFrameFilename
)
{
//read the first file of the sequence
frame
=
imread
(
fistFrameFilename
);
if
(
frame
.
empty
()){
//error in opening the first image
cerr
<<
"Unable to open first image frame: "
<<
fistFrameFilename
<<
endl
;
exit
(
EXIT_FAILURE
);
}
//current image filename
string
fn
(
fistFrameFilename
);
//read input data. ESC or 'q' for quitting
keyboard
=
0
;
while
(
keyboard
!=
'q'
&&
keyboard
!=
27
){
//update the background model
pMOG2
->
apply
(
frame
,
fgMaskMOG2
);
//get the frame number and write it on the current frame
size_t
index
=
fn
.
find_last_of
(
"/"
);
if
(
index
==
string
::
npos
)
{
index
=
fn
.
find_last_of
(
"
\\
"
);
}
size_t
index2
=
fn
.
find_last_of
(
"."
);
string
prefix
=
fn
.
substr
(
0
,
index
+
1
);
string
suffix
=
fn
.
substr
(
index2
);
string
frameNumberString
=
fn
.
substr
(
index
+
1
,
index2
-
index
-
1
);
istringstream
iss
(
frameNumberString
);
int
frameNumber
=
0
;
iss
>>
frameNumber
;
rectangle
(
frame
,
cv
::
Point
(
10
,
2
),
cv
::
Point
(
100
,
20
),
cv
::
Scalar
(
255
,
255
,
255
),
-
1
);
putText
(
frame
,
frameNumberString
.
c_str
(),
cv
::
Point
(
15
,
15
),
FONT_HERSHEY_SIMPLEX
,
0.5
,
cv
::
Scalar
(
0
,
0
,
0
));
//! [show]
//show the current frame and the fg masks
imshow
(
"Frame"
,
frame
);
imshow
(
"FG Mask MOG 2"
,
fgMaskMOG2
);
imshow
(
"FG Mask"
,
fgMask
);
//! [show]
//get the input from the keyboard
keyboard
=
(
char
)
waitKey
(
30
);
//search for the next image in the sequence
ostringstream
oss
;
oss
<<
(
frameNumber
+
1
);
string
nextFrameNumberString
=
oss
.
str
();
string
nextFrameFilename
=
prefix
+
nextFrameNumberString
+
suffix
;
//read the next frame
frame
=
imread
(
nextFrameFilename
);
if
(
frame
.
empty
()){
//error in opening the next image in the sequence
cerr
<<
"Unable to open image frame: "
<<
nextFrameFilename
<<
endl
;
exit
(
EXIT_FAILURE
);
}
//update the path of the current frame
fn
.
assign
(
nextFrameFilename
);
int
keyboard
=
waitKey
(
30
);
if
(
keyboard
==
'q'
||
keyboard
==
27
)
break
;
}
return
0
;
}
samples/java/tutorial_code/video/background_subtraction/BackgroundSubtractionDemo.java
0 → 100644
View file @
4bea70a6
import
org.opencv.core.Core
;
import
org.opencv.core.Mat
;
import
org.opencv.core.Point
;
import
org.opencv.core.Scalar
;
import
org.opencv.highgui.HighGui
;
import
org.opencv.imgproc.Imgproc
;
import
org.opencv.video.BackgroundSubtractor
;
import
org.opencv.video.Video
;
import
org.opencv.videoio.VideoCapture
;
import
org.opencv.videoio.Videoio
;
class
BackgroundSubtraction
{
public
void
run
(
String
[]
args
)
{
String
input
=
args
.
length
>
0
?
args
[
0
]
:
"../data/vtest.avi"
;
boolean
useMOG2
=
args
.
length
>
1
?
args
[
1
]
==
"MOG2"
:
true
;
//! [create]
BackgroundSubtractor
backSub
;
if
(
useMOG2
)
{
backSub
=
Video
.
createBackgroundSubtractorMOG2
();
}
else
{
backSub
=
Video
.
createBackgroundSubtractorKNN
();
}
//! [create]
//! [capture]
VideoCapture
capture
=
new
VideoCapture
(
input
);
if
(!
capture
.
isOpened
())
{
System
.
err
.
println
(
"Unable to open: "
+
input
);
System
.
exit
(
0
);
}
//! [capture]
Mat
frame
=
new
Mat
(),
fgMask
=
new
Mat
();
while
(
true
)
{
capture
.
read
(
frame
);
if
(
frame
.
empty
())
{
break
;
}
//! [apply]
// update the background model
backSub
.
apply
(
frame
,
fgMask
);
//! [apply]
//! [display_frame_number]
// get the frame number and write it on the current frame
Imgproc
.
rectangle
(
frame
,
new
Point
(
10
,
2
),
new
Point
(
100
,
20
),
new
Scalar
(
255
,
255
,
255
),
-
1
);
String
frameNumberString
=
String
.
format
(
"%d"
,
(
int
)
capture
.
get
(
Videoio
.
CAP_PROP_POS_FRAMES
));
Imgproc
.
putText
(
frame
,
frameNumberString
,
new
Point
(
15
,
15
),
Core
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
new
Scalar
(
0
,
0
,
0
));
//! [display_frame_number]
//! [show]
// show the current frame and the fg masks
HighGui
.
imshow
(
"Frame"
,
frame
);
HighGui
.
imshow
(
"FG Mask"
,
fgMask
);
//! [show]
// get the input from the keyboard
int
keyboard
=
HighGui
.
waitKey
(
30
);
if
(
keyboard
==
'q'
||
keyboard
==
27
)
{
break
;
}
}
HighGui
.
waitKey
();
System
.
exit
(
0
);
}
}
public
class
BackgroundSubtractionDemo
{
public
static
void
main
(
String
[]
args
)
{
// Load the native OpenCV library
System
.
loadLibrary
(
Core
.
NATIVE_LIBRARY_NAME
);
new
BackgroundSubtraction
().
run
(
args
);
}
}
samples/python/tutorial_code/video/background_subtraction/bg_sub.py
0 → 100644
View file @
4bea70a6
from
__future__
import
print_function
import
cv2
as
cv
import
argparse
parser
=
argparse
.
ArgumentParser
(
description
=
'This program shows how to use background subtraction methods provided by
\
OpenCV. You can process both videos and images.'
)
parser
.
add_argument
(
'--input'
,
type
=
str
,
help
=
'Path to a video or a sequence of image.'
,
default
=
'../data/vtest.avi'
)
parser
.
add_argument
(
'--algo'
,
type
=
str
,
help
=
'Background subtraction method (KNN, MOG2).'
,
default
=
'MOG2'
)
args
=
parser
.
parse_args
()
## [create]
#create Background Subtractor objects
if
args
.
algo
==
'MOG2'
:
backSub
=
cv
.
createBackgroundSubtractorMOG2
()
else
:
backSub
=
cv
.
createBackgroundSubtractorKNN
()
## [create]
## [capture]
capture
=
cv
.
VideoCapture
(
args
.
input
)
if
not
capture
.
isOpened
:
print
(
'Unable to open: '
+
args
.
input
)
exit
(
0
)
## [capture]
while
True
:
ret
,
frame
=
capture
.
read
()
if
frame
is
None
:
break
## [apply]
#update the background model
fgMask
=
backSub
.
apply
(
frame
)
## [apply]
## [display_frame_number]
#get the frame number and write it on the current frame
cv
.
rectangle
(
frame
,
(
10
,
2
),
(
100
,
20
),
(
255
,
255
,
255
),
-
1
)
cv
.
putText
(
frame
,
str
(
capture
.
get
(
cv
.
CAP_PROP_POS_FRAMES
)),
(
15
,
15
),
cv
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
(
0
,
0
,
0
))
## [display_frame_number]
## [show]
#show the current frame and the fg masks
cv
.
imshow
(
'Frame'
,
frame
)
cv
.
imshow
(
'FG Mask'
,
fgMask
)
## [show]
keyboard
=
cv
.
waitKey
(
30
)
if
keyboard
==
'q'
or
keyboard
==
27
:
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