~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/samples/cpp/segment_objects.cpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

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