 |
OpenCV
3.2.0
Open Source Computer Vision
|
An example using drawContours to clean up a background segmentation result
#include <stdio.h>
#include <string>
using namespace std;
static void help()
{
printf("\n"
"This program demonstrated a simple method of connected components clean up of background subtraction\n"
"When the program starts, it begins learning the background.\n"
"You can toggle background learning on and off by hitting the space bar.\n"
"Call\n"
"./segment_objects [video file, else it reads camera 0]\n\n");
}
static void refineSegments(
const Mat& img,
Mat& mask,
Mat& dst)
{
int niters = 3;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
if( contours.size() == 0 )
return;
int idx = 0, largestComp = 0;
double maxArea = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
const vector<Point>& c = contours[idx];
if( area > maxArea )
{
maxArea = area;
largestComp = idx;
}
}
}
int main(int argc, char** argv)
{
bool update_bg_model = true;
if (parser.has("help"))
{
help();
return 0;
}
string input = parser.get<std::string>("@input");
if (input.empty())
else
{
printf("\nCan not open camera or video file\n");
return -1;
}
Mat tmp_frame, bgmask, out_frame;
cap >> tmp_frame;
if(tmp_frame.empty())
{
printf("can not read data from the video source\n");
return -1;
}
for(;;)
{
cap >> tmp_frame;
if( tmp_frame.empty() )
break;
bgsubtractor->
apply(tmp_frame, bgmask, update_bg_model ? -1 : 0);
refineSegments(tmp_frame, bgmask, out_frame);
imshow(
"segmented", out_frame);
if( keycode == 27 )
break;
if( keycode == ' ' )
{
update_bg_model = !update_bg_model;
printf("Learn background is in state = %d\n",update_bg_model);
}
}
return 0;
}
@ RETR_CCOMP
Definition: imgproc.hpp:434
@ FILLED
Definition: core.hpp:215
virtual void apply(InputArray image, OutputArray fgmask, double learningRate=-1)=0
Computes a foreground mask.
void erode(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
Erodes an image by using a specific structuring element.
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:597
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
@ LINE_8
8-connected line
Definition: core.hpp:217
void dilate(InputArray src, OutputArray dst, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
Dilates an image by using a specific structuring element.
#define CV_8UC3
Definition: interface.h:84
double contourArea(InputArray contour, bool oriented=false)
Calculates a contour area.
MatSize size
Definition: mat.hpp:1978
Ptr< BackgroundSubtractorMOG2 > createBackgroundSubtractorMOG2(int history=500, double varThreshold=16, bool detectShadows=true)
Creates MOG2 Background Subtractor.
Template class for smart pointers with shared ownership.
Definition: cvstd.hpp:281
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
virtual bool open(const String &filename)
Open video file or a capturing device or a IP video stream for video capturing.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
Draws contours outlines or filled contours.
Point2i Point
Definition: types.hpp:183
virtual void setVarThreshold(double varThreshold)=0
Sets the variance threshold for the pixel-model match.
n-dimensional dense array class
Definition: mat.hpp:741
Designed for command line parsing.
Definition: utility.hpp:735
Definition: affine.hpp:52
@ CHAIN_APPROX_SIMPLE
Definition: imgproc.hpp:448
void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.