Merge pull request #1722 from StevenPuttemans:feature_1631_second
[profile/ivi/opencv.git] / samples / cpp / segment_objects.cpp
1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include "opencv2/video/background_segm.hpp"
4 #include <stdio.h>
5 #include <string>
6
7 using namespace cv;
8
9 static void help()
10 {
11     printf("\n"
12             "This program demonstrated a simple method of connected components clean up of background subtraction\n"
13             "When the program starts, it begins learning the background.\n"
14             "You can toggle background learning on and off by hitting the space bar.\n"
15             "Call\n"
16             "./segment_objects [video file, else it reads camera 0]\n\n");
17 }
18
19 static void refineSegments(const Mat& img, Mat& mask, Mat& dst)
20 {
21     int niters = 3;
22
23     vector<vector<Point> > contours;
24     vector<Vec4i> hierarchy;
25
26     Mat temp;
27
28     dilate(mask, temp, Mat(), Point(-1,-1), niters);
29     erode(temp, temp, Mat(), Point(-1,-1), niters*2);
30     dilate(temp, temp, Mat(), Point(-1,-1), niters);
31
32     findContours( temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
33
34     dst = Mat::zeros(img.size(), CV_8UC3);
35
36     if( contours.size() == 0 )
37         return;
38
39     // iterate through all the top-level contours,
40     // draw each connected component with its own random color
41     int idx = 0, largestComp = 0;
42     double maxArea = 0;
43
44     for( ; idx >= 0; idx = hierarchy[idx][0] )
45     {
46         const vector<Point>& c = contours[idx];
47         double area = fabs(contourArea(Mat(c)));
48         if( area > maxArea )
49         {
50             maxArea = area;
51             largestComp = idx;
52         }
53     }
54     Scalar color( 0, 0, 255 );
55     drawContours( dst, contours, largestComp, color, CV_FILLED, 8, hierarchy );
56 }
57
58
59 int main(int argc, char** argv)
60 {
61     VideoCapture cap;
62     bool update_bg_model = true;
63
64     help();
65
66     if( argc < 2 )
67         cap.open(0);
68     else
69         cap.open(std::string(argv[1]));
70
71     if( !cap.isOpened() )
72     {
73         printf("\nCan not open camera or video file\n");
74         return -1;
75     }
76
77     Mat tmp_frame, bgmask, out_frame;
78
79     cap >> tmp_frame;
80     if(!tmp_frame.data)
81     {
82         printf("can not read data from the video source\n");
83         return -1;
84     }
85
86     namedWindow("video", 1);
87     namedWindow("segmented", 1);
88
89     BackgroundSubtractorMOG bgsubtractor;
90     bgsubtractor.set("noiseSigma", 10);
91
92     for(;;)
93     {
94         cap >> tmp_frame;
95         if( !tmp_frame.data )
96             break;
97         bgsubtractor(tmp_frame, bgmask, update_bg_model ? -1 : 0);
98         refineSegments(tmp_frame, bgmask, out_frame);
99         imshow("video", tmp_frame);
100         imshow("segmented", out_frame);
101         int keycode = waitKey(30);
102         if( keycode == 27 )
103             break;
104         if( keycode == ' ' )
105         {
106             update_bg_model = !update_bg_model;
107             printf("Learn background is in state = %d\n",update_bg_model);
108         }
109     }
110
111     return 0;
112 }