1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 /*______________________________________________________________________________________*/
44 /* Test functions for the Eigen Objects functions group */
45 /*______________________________________________________________________________________*/
49 static int _cvCalcCovarMatrix_8u32fR_q( int nObjects,
59 if ( nObjects < 2 ) return CV_BADFACTOR_ERR;
60 if ( size.width > objStep || 4*size.width > avgStep || size.height < 1)
61 return CV_BADSIZE_ERR;
62 if ( objects == NULL || avg == NULL || covarMatrix == NULL ) return CV_NULLPTR_ERR;
66 for(i=0; i<nObjects; i++)
68 uchar* bu = objects[i];
69 for(j=i; j<nObjects; j++)
71 int ij = i*nObjects + j, k, l;
75 uchar* bu2 = objects[j];
77 for(k=0; k<size.height; k++, bu1 += objStep, bu2 += objStep, a += avgStep)
79 for(l = 0; l < size.width-3; l+=4)
84 w += ( u1 - f ) * ( u2 - f );
88 w += ( u1 - f ) * ( u2 - f );
92 w += ( u1 - f ) * ( u2 - f );
96 w += ( u1 - f ) * ( u2 - f );
98 for(; l < size.width; l++)
103 w += ( u1 - f ) * ( u2 - f );
114 } /* end of _cvCalcCovarMatrix_8u32fR */
117 /* copy of _cvJacobiEigen_32f */
118 int _cvJacobiEigens_32f ( float* A,
125 float *AA = A, *VV = V;
126 double Amax, anorm=0, ax;
128 if ( A == NULL || V == NULL || E == NULL ) return CV_NULLPTR_ERR;
129 if ( n <= 0 ) return CV_BADSIZE_ERR;
130 if (eps < 1.0e-7f ) eps = 1.0e-7f;
132 /*-------- Prepare --------*/
133 for(i=0; i<n; i++, VV+=n, AA+=n)
140 for(j=0; j<n; j++) VV[j] = 0.f;
144 anorm = sqrt( anorm + anorm );
154 float *V1 = V, *A1 = A;
156 for(p=0; p<n-1; p++, A1+=n, V1+=n)
158 float *A2 = A + n*(p+1), *V2 = V + n*(p+1);
159 for(q=p+1; q<n; q++, A2+=n, V2+=n)
161 double x, y, c, s, c2, s2, a;
162 float *A3, Apq=A1[q], App, Aqq, Aip, Aiq, Vpi, Vqi;
163 if( fabs( Apq ) < Amax ) continue;
167 /*---- Calculation of rotation angle's sine & cosine ----*/
170 y = 5.0e-1*(App - Aqq);
171 x = -Apq / sqrt(Apq*Apq + y*y);
173 s = x / sqrt(2.0*(1.0 + sqrt(1.0 - x*x)));
179 /*---- Apq annulation ----*/
181 for(i=0; i<p; i++, A3+=n)
187 A3[p] = (float)(Aip*c - Aiq*s);
188 A3[q] = (float)(Aiq*c + Aip*s);
189 V1[i] = (float)(Vpi*c - Vqi*s);
190 V2[i] = (float)(Vqi*c + Vpi*s);
192 for(; i<q; i++, A3+=n)
198 A1[i] = (float)(Aip*c - Aiq*s);
199 A3[q] = (float)(Aiq*c + Aip*s);
200 V1[i] = (float)(Vpi*c - Vqi*s);
201 V2[i] = (float)(Vqi*c + Vpi*s);
209 A1[i] = (float)(Aip*c - Aiq*s);
210 A2[i] = (float)(Aiq*c + Aip*s);
211 V1[i] = (float)(Vpi*c - Vqi*s);
212 V2[i] = (float)(Vqi*c + Vpi*s);
214 A1[p] = (float)(App*c2 + Aqq*s2 - a);
215 A2[q] = (float)(App*s2 + Aqq*c2 + a);
216 A1[q] = A2[p] = 0.0f;
221 } /* while ( Amax > ax ) */
223 for(i=0, k=0; i<n; i++, k+=n+1) E[i] = A[k];
224 /*printf(" M = %d\n", M);*/
226 /* -------- ordering --------*/
230 float Em = (float)fabs(E[i]);
233 float Ej = (float)fabs(E[j]);
234 m = ( Em < Ej ) ? j : m;
235 Em = ( Em < Ej ) ? Ej : Em;
243 for(j=0, k=i*n, l=m*n; j<n; j++, k++, l++)
257 /*______________________________________________________________________________________*/
259 int _cvCalcEigenObjects_8u32fR_q( int nObjects,
273 float *c=0, *ev=0, *bf, *bf1, *bf2, m;
277 if ( nObjects < 2 ) return CV_BADFACTOR_ERR;
278 if ( size.width > objStep || 4*size.width > eigStep ||
279 4*size.width > avgStep || size.height < 1) return CV_BADSIZE_ERR;
280 if ( objects == NULL || eigObjs == NULL || eigVals == NULL ||
281 avg == NULL || nEigObjs == NULL || eps == NULL ) return CV_NULLPTR_ERR;
282 for( i=0; i<nObjects; i++ ) if( objects[i] == NULL ) return CV_NULLPTR_ERR;
283 for( i=0; i<*nEigObjs; i++ ) if( eigObjs[i] == NULL ) return CV_NULLPTR_ERR;
288 /* Calculation of averaged object */
290 for(i = 0; i < size.height; i++, bf += avgStep)
291 for(j = 0; j < size.width; j++)
293 for(k = 0; k < nObjects; k++)
297 for(i = 0; i < size.height; i++, bu +=objStep, bf += avgStep)
298 for(j = 0; j < size.width; j++)
301 m = 1.0f/(float)nObjects;
303 for(i = 0; i < size.height; i++, bf += avgStep)
304 for(j = 0; j < size.width; j++)
307 /* Calculation of covariance matrix */
308 c = (float*)cvAlloc ( sizeof(float)*nObjects*nObjects );
309 if(c==NULL) return CV_OUTOFMEM_ERR;
311 r = _cvCalcCovarMatrix_8u32fR_q ( nObjects, objects, objStep,
312 avg, 4*avgStep, size, c );
313 if(r) { cvFree( &c ); return r; }
315 /* Calculation of eigenvalues & eigenvectors */
316 ev = (float*)cvAlloc ( sizeof(float)*nObjects*nObjects );
317 if(ev==NULL) { cvFree( &c ); return CV_OUTOFMEM_ERR; }
319 _cvJacobiEigens_32f( c, ev, eigVals, nObjects, 0.0f );
322 for(i=0; i<*nEigObjs; i++) if( fabs(eigVals[i]/eigVals[0]) < *eps ) break;
324 *eps = fabs(eigVals[*nEigObjs-1]/eigVals[0]);
326 /* Calculation of eigenobjects */
328 for(i=0; i<*nEigObjs; i++, bf2+=nObjects)
330 float e = (float)(1.0/sqrt(eigVals[i]));
331 float* u = eigObjs[i];
334 for(l=0; l<size.height; l++, bf+=eigStep)
335 for(j=0; j<size.width; j++) bf[j] = 0.0f;
337 for(k=0; k<nObjects; k++)
343 for(l=0; l<size.height; l++, bf+=eigStep, bf1+=avgStep, bu+=objStep)
344 for(j=0; j<size.width; j++) bf[j] += v * ((float)bu[j] - bf1[j]);
350 } /* --- End of _cvCalcEigenObjects_8u32fR --- */
351 /*______________________________________________________________________________________*/
353 float _cvCalcDecompCoeff_8u32fR_q( uchar* obj,
364 if ( size.width > objStep || 4*size.width > eigStep
365 || 4*size.width > avgStep || size.height < 1) return -1.0e30f;
366 if ( obj == NULL || eigObj == NULL || avg == NULL ) return -1.0e30f;
371 for(i = 0; i < size.height; i++, obj += objStep, eigObj += eigStep, avg += avgStep)
372 for(k = 0; k < size.width; k++)
373 w += eigObj[k]*( (float)obj[k] - avg[k] );
377 /*______________________________________________________________________________________*/
379 int _cvEigenDecomposite_8u32fR_q( uchar* obj,
391 if ( nEigObjs < 2 ) return CV_BADFACTOR_ERR;
392 if ( size.width > objStep || 4*size.width > eigStep ||
393 4*size.width > avgStep || size.height < 1) return CV_BADSIZE_ERR;
394 if ( obj == NULL || eigObjs == NULL || coeffs == NULL || avg == NULL)
395 return CV_NULLPTR_ERR;
397 for(i=0; i<nEigObjs; i++)
399 float w = _cvCalcDecompCoeff_8u32fR_q( obj, objStep, eigObjs[i], eigStep,
400 avg, avgStep, size );
401 if( w < -1.0e29f ) return CV_NOTDEFINED_ERR;
406 /*______________________________________________________________________________________*/
408 int _cvEigenProjection_8u32fR_q( int nEigObjs,
420 if ( size.width > avgStep || 4*size.width > eigStep || size.height < 1)
421 return CV_BADSIZE_ERR;
422 if ( rest == NULL || eigens == NULL || avg == NULL || coeffs == NULL )
423 return CV_NULLPTR_ERR;
427 for(i = 0; i < size.height; i++, rest+=restStep, avg+=avgStep)
430 for(j = 0; j < size.width; j++, ij++)
433 for(k=0; k<nEigObjs-3; k+=4)
435 float* b = eigens[k];
436 w += coeffs[k ] * b[ij];
438 w += coeffs[k+1] * b[ij];
440 w += coeffs[k+2] * b[ij];
442 w += coeffs[k+3] * b[ij];
444 for(; k<nEigObjs; k++)
446 float* b = eigens[k];
447 w += coeffs[k] * b[ij];
449 w = w<-0.499999f ? -0.499999f : w>255.499f ? 255.499f : w;
450 rest[j] = (uchar)cvRound( w );
455 /*______________________________________________________________________________________*/
457 /* << End of file >> */