Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / modules / legacy / src / condens.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 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 #include "precomp.hpp"
42
43 /*F///////////////////////////////////////////////////////////////////////////////////////
44 //    Name:    cvCreateConDensation
45 //    Purpose: Creating CvConDensation structure and allocating memory for it
46 //    Context:
47 //    Parameters:
48 //      Kalman     - double pointer to CvConDensation structure
49 //      DP         - dimension of the dynamical vector
50 //      MP         - dimension of the measurement vector
51 //      SamplesNum - number of samples in sample set used in algorithm
52 //    Returns:
53 //    Notes:
54 //
55 //F*/
56
57 CV_IMPL CvConDensation* cvCreateConDensation( int DP, int MP, int SamplesNum )
58 {
59     int i;
60     CvConDensation *CD = 0;
61
62     if( DP < 0 || MP < 0 || SamplesNum < 0 )
63         CV_Error( CV_StsOutOfRange, "" );
64
65     /* allocating memory for the structure */
66     CD = (CvConDensation *) cvAlloc( sizeof( CvConDensation ));
67     /* setting structure params */
68     CD->SamplesNum = SamplesNum;
69     CD->DP = DP;
70     CD->MP = MP;
71     /* allocating memory for structure fields */
72     CD->flSamples = (float **) cvAlloc( sizeof( float * ) * SamplesNum );
73     CD->flNewSamples = (float **) cvAlloc( sizeof( float * ) * SamplesNum );
74     CD->flSamples[0] = (float *) cvAlloc( sizeof( float ) * SamplesNum * DP );
75     CD->flNewSamples[0] = (float *) cvAlloc( sizeof( float ) * SamplesNum * DP );
76
77     /* setting pointers in pointer's arrays */
78     for( i = 1; i < SamplesNum; i++ )
79     {
80         CD->flSamples[i] = CD->flSamples[i - 1] + DP;
81         CD->flNewSamples[i] = CD->flNewSamples[i - 1] + DP;
82     }
83
84     CD->State = (float *) cvAlloc( sizeof( float ) * DP );
85     CD->DynamMatr = (float *) cvAlloc( sizeof( float ) * DP * DP );
86     CD->flConfidence = (float *) cvAlloc( sizeof( float ) * SamplesNum );
87     CD->flCumulative = (float *) cvAlloc( sizeof( float ) * SamplesNum );
88
89     CD->RandS = (CvRandState *) cvAlloc( sizeof( CvRandState ) * DP );
90     CD->Temp = (float *) cvAlloc( sizeof( float ) * DP );
91     CD->RandomSample = (float *) cvAlloc( sizeof( float ) * DP );
92
93     /* Returning created structure */
94
95     return CD;
96 }
97
98 /*F///////////////////////////////////////////////////////////////////////////////////////
99 //    Name:    cvReleaseConDensation
100 //    Purpose: Releases CvConDensation structure and frees memory allocated for it
101 //    Context:
102 //    Parameters:
103 //      Kalman     - double pointer to CvConDensation structure
104 //      DP         - dimension of the dynamical vector
105 //      MP         - dimension of the measurement vector
106 //      SamplesNum - number of samples in sample set used in algorithm
107 //    Returns:
108 //    Notes:
109 //
110 //F*/
111 CV_IMPL void
112 cvReleaseConDensation( CvConDensation ** ConDensation )
113 {
114     CvConDensation *CD = *ConDensation;
115
116     if( !ConDensation )
117         CV_Error( CV_StsNullPtr, "" );
118
119     if( !CD )
120         return;
121
122     /* freeing the memory */
123     cvFree( &CD->State );
124     cvFree( &CD->DynamMatr);
125     cvFree( &CD->flConfidence );
126     cvFree( &CD->flCumulative );
127     cvFree( &CD->flSamples[0] );
128     cvFree( &CD->flNewSamples[0] );
129     cvFree( &CD->flSamples );
130     cvFree( &CD->flNewSamples );
131     cvFree( &CD->Temp );
132     cvFree( &CD->RandS );
133     cvFree( &CD->RandomSample );
134     /* release structure */
135     cvFree( ConDensation );
136 }
137
138 /*F///////////////////////////////////////////////////////////////////////////////////////
139 //    Name:    cvConDensUpdateByTime
140 //    Purpose: Performing Time Update routine for ConDensation algorithm
141 //    Context:
142 //    Parameters:
143 //      Kalman     - pointer to CvConDensation structure
144 //    Returns:
145 //    Notes:
146 //
147 //F*/
148 CV_IMPL void
149 cvConDensUpdateByTime( CvConDensation * ConDens )
150 {
151     int i, j;
152     float Sum = 0;
153
154     if( !ConDens )
155         CV_Error( CV_StsNullPtr, "" );
156
157     /* Sets Temp to Zero */
158     icvSetZero_32f( ConDens->Temp, ConDens->DP, 1 );
159
160     /* Calculating the Mean */
161     for( i = 0; i < ConDens->SamplesNum; i++ )
162     {
163         icvScaleVector_32f( ConDens->flSamples[i], ConDens->State, ConDens->DP,
164                              ConDens->flConfidence[i] );
165         icvAddVector_32f( ConDens->Temp, ConDens->State, ConDens->Temp, ConDens->DP );
166         Sum += ConDens->flConfidence[i];
167         ConDens->flCumulative[i] = Sum;
168     }
169
170     /* Taking the new vector from transformation of mean by dynamics matrix */
171
172     icvScaleVector_32f( ConDens->Temp, ConDens->Temp, ConDens->DP, 1.f / Sum );
173     icvTransformVector_32f( ConDens->DynamMatr, ConDens->Temp, ConDens->State, ConDens->DP,
174                              ConDens->DP );
175     Sum = Sum / ConDens->SamplesNum;
176
177     /* Updating the set of random samples */
178     for( i = 0; i < ConDens->SamplesNum; i++ )
179     {
180         j = 0;
181         while( (ConDens->flCumulative[j] <= (float) i * Sum)&&(j<ConDens->SamplesNum-1))
182         {
183             j++;
184         }
185         icvCopyVector_32f( ConDens->flSamples[j], ConDens->DP, ConDens->flNewSamples[i] );
186     }
187
188     /* Adding the random-generated vector to every vector in sample set */
189     for( i = 0; i < ConDens->SamplesNum; i++ )
190     {
191         for( j = 0; j < ConDens->DP; j++ )
192         {
193             cvbRand( ConDens->RandS + j, ConDens->RandomSample + j, 1 );
194         }
195
196         icvTransformVector_32f( ConDens->DynamMatr, ConDens->flNewSamples[i],
197                                  ConDens->flSamples[i], ConDens->DP, ConDens->DP );
198         icvAddVector_32f( ConDens->flSamples[i], ConDens->RandomSample, ConDens->flSamples[i],
199                            ConDens->DP );
200     }
201 }
202
203 /*F///////////////////////////////////////////////////////////////////////////////////////
204 //    Name:    cvConDensInitSamplSet
205 //    Purpose: Performing Time Update routine for ConDensation algorithm
206 //    Context:
207 //    Parameters:
208 //    conDens     - pointer to CvConDensation structure
209 //    lowerBound  - vector of lower bounds used to random update of sample set
210 //    lowerBound  - vector of upper bounds used to random update of sample set
211 //    Returns:
212 //    Notes:
213 //
214 //F*/
215
216 CV_IMPL void
217 cvConDensInitSampleSet( CvConDensation * conDens, CvMat * lowerBound, CvMat * upperBound )
218 {
219     int i, j;
220     float *LBound;
221     float *UBound;
222     float Prob = 1.f / conDens->SamplesNum;
223
224     if( !conDens || !lowerBound || !upperBound )
225         CV_Error( CV_StsNullPtr, "" );
226
227     if( CV_MAT_TYPE(lowerBound->type) != CV_32FC1 ||
228         !CV_ARE_TYPES_EQ(lowerBound,upperBound) )
229         CV_Error( CV_StsBadArg, "source  has not appropriate format" );
230
231     if( (lowerBound->cols != 1) || (upperBound->cols != 1) )
232         CV_Error( CV_StsBadArg, "source  has not appropriate size" );
233
234     if( (lowerBound->rows != conDens->DP) || (upperBound->rows != conDens->DP) )
235         CV_Error( CV_StsBadArg, "source  has not appropriate size" );
236
237     LBound = lowerBound->data.fl;
238     UBound = upperBound->data.fl;
239     /* Initializing the structures to create initial Sample set */
240     for( i = 0; i < conDens->DP; i++ )
241     {
242         cvRandInit( &(conDens->RandS[i]),
243                     LBound[i],
244                     UBound[i],
245                     i );
246     }
247     /* Generating the samples */
248     for( j = 0; j < conDens->SamplesNum; j++ )
249     {
250         for( i = 0; i < conDens->DP; i++ )
251         {
252             cvbRand( conDens->RandS + i, conDens->flSamples[j] + i, 1 );
253         }
254         conDens->flConfidence[j] = Prob;
255     }
256     /* Reinitializes the structures to update samples randomly */
257     for( i = 0; i < conDens->DP; i++ )
258     {
259         cvRandInit( &(conDens->RandS[i]),
260                     (LBound[i] - UBound[i]) / 5,
261                     (UBound[i] - LBound[i]) / 5,
262                     i);
263     }
264 }