2bcea5573b5e6508d1f4e961d704bef08cad9e15
[platform/upstream/opencv.git] / modules / legacy / src / bgfg_common.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
14 //
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
17 //
18 //   * Redistribution's of source code must retain the above copyright notice,
19 //     this list of conditions and the following disclaimer.
20 //
21 //   * Redistribution's in binary form must reproduce the above copyright notice,
22 //     this list of conditions and the following disclaimer in the documentation
23 //     and/or other materials provided with the distribution.
24 //
25 //   * The name of Intel Corporation may not be used to endorse or promote products
26 //     derived from this software without specific prior written permission.
27 //
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
38 //
39 //M*/
40
41 #include "precomp.hpp"
42
43 void cvReleaseBGStatModel( CvBGStatModel** bg_model )
44 {
45     if( bg_model && *bg_model && (*bg_model)->release )
46         (*bg_model)->release( bg_model );
47 }
48
49 int cvUpdateBGStatModel( IplImage* current_frame,
50                         CvBGStatModel*  bg_model,
51                         double learningRate )
52 {
53     return bg_model && bg_model->update ? bg_model->update( current_frame, bg_model, learningRate ) : 0;
54 }
55
56
57 //  Function cvRefineForegroundMaskBySegm preforms FG post-processing based on segmentation
58 //    (all pixels of the segment will be classified as FG if majority of pixels of the region are FG).
59 // parameters:
60 //      segments - pointer to result of segmentation (for example MeanShiftSegmentation)
61 //      bg_model - pointer to CvBGStatModel structure
62 CV_IMPL void cvRefineForegroundMaskBySegm( CvSeq* segments, CvBGStatModel*  bg_model )
63 {
64     IplImage* tmp_image = cvCreateImage(cvSize(bg_model->foreground->width,bg_model->foreground->height),
65                             IPL_DEPTH_8U, 1);
66     for( ; segments; segments = ((CvSeq*)segments)->h_next )
67     {
68         CvSeq seq = *segments;
69         seq.v_next = seq.h_next = NULL;
70         cvZero(tmp_image);
71         cvDrawContours( tmp_image, &seq, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 10, -1);
72         int num1 = cvCountNonZero(tmp_image);
73         cvAnd(tmp_image, bg_model->foreground, tmp_image);
74         int num2 = cvCountNonZero(tmp_image);
75         if( num2 > num1*0.5 )
76             cvDrawContours( bg_model->foreground, &seq, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 10, -1);
77         else
78             cvDrawContours( bg_model->foreground, &seq, CV_RGB(0, 0, 0), CV_RGB(0, 0, 0), 10, -1);
79     }
80     cvReleaseImage(&tmp_image);
81 }
82
83
84
85 CV_IMPL CvSeq*
86 cvSegmentFGMask( CvArr* _mask, int poly1Hull0, float perimScale,
87                  CvMemStorage* storage, CvPoint offset )
88 {
89     CvMat mstub, *mask = cvGetMat( _mask, &mstub );
90     CvMemStorage* tempStorage = storage ? storage : cvCreateMemStorage();
91     CvSeq *contours, *c;
92     int nContours = 0;
93     CvContourScanner scanner;
94
95     // clean up raw mask
96     cvMorphologyEx( mask, mask, 0, 0, CV_MOP_OPEN, 1 );
97     cvMorphologyEx( mask, mask, 0, 0, CV_MOP_CLOSE, 1 );
98
99     // find contours around only bigger regions
100     scanner = cvStartFindContours( mask, tempStorage,
101         sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset );
102
103     while( (c = cvFindNextContour( scanner )) != 0 )
104     {
105         double len = cvContourPerimeter( c );
106         double q = (mask->rows + mask->cols)/perimScale; // calculate perimeter len threshold
107         if( len < q ) //Get rid of blob if it's perimeter is too small
108             cvSubstituteContour( scanner, 0 );
109         else //Smooth it's edges if it's large enough
110         {
111             CvSeq* newC;
112             if( poly1Hull0 ) //Polygonal approximation of the segmentation
113                 newC = cvApproxPoly( c, sizeof(CvContour), tempStorage, CV_POLY_APPROX_DP, 2, 0 );
114             else //Convex Hull of the segmentation
115                 newC = cvConvexHull2( c, tempStorage, CV_CLOCKWISE, 1 );
116             cvSubstituteContour( scanner, newC );
117             nContours++;
118         }
119     }
120     contours = cvEndFindContours( &scanner );
121
122     // paint the found regions back into the image
123     cvZero( mask );
124     for( c=contours; c != 0; c = c->h_next )
125         cvDrawContours( mask, c, cvScalarAll(255), cvScalarAll(0), -1, CV_FILLED, 8,
126             cvPoint(-offset.x,-offset.y));
127
128     if( tempStorage != storage )
129     {
130         cvReleaseMemStorage( &tempStorage );
131         contours = 0;
132     }
133
134     return contours;
135 }
136
137 /* End of file. */