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.
46 /* Testing parameters */
47 static char FuncName[] = "cvCalcOpticalFlowHS";
48 static char TestName[] = "Optical flow (Horn & Schunck)";
49 static char TestClass[] = "Algorithm";
51 static long lImageWidth;
52 static long lImageHeight;
55 #define EPSILON 0.0001f
57 static int fmaCalcOpticalFlowHS( void )
77 CvTermCriteria criteria;
85 static int read_param = 0;
87 /* Initialization global parameters */
91 /* Reading test-parameters */
92 trslRead( &lImageHeight, "300", "Image height" );
93 trslRead( &lImageWidth, "300", "Image width" );
94 trssRead( &lambda, "20", "lambda" );
97 /* initialization - for warning disable */
99 criteria.max_iter = 0;
102 /* Allocating memory for all frames */
103 IplImage* imgA = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_8U, 1 );
104 IplImage* imgB = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_8U, 1 );
106 IplImage* testVelocityX = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_32F, 1 );
107 IplImage* testVelocityY = cvCreateImage( cvSize(lImageWidth,lImageHeight), IPL_DEPTH_32F, 1 );
109 VelocityX = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
110 VelocityY = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
112 auxVelocityX = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
113 auxVelocityY = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
115 DerX = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
116 DerY = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
117 DerT = (float*)cvAlloc( lImageWidth*lImageHeight * sizeof(float) );
120 ats1bInitRandom( 0, 255, (uchar*)imgA->imageData, lImageWidth * lImageHeight );
121 ats1bInitRandom( 0, 255, (uchar*)imgB->imageData, lImageWidth * lImageHeight );
123 /* set ROI of images */
124 roiA = (uchar*)imgA->imageData;
125 roiB = (uchar*)imgB->imageData;
127 /* example of 3*3 ROI*/
131 roiA[lImageWidth] = 0;
132 roiA[lImageWidth+1] = 1;
133 roiA[lImageWidth+2] = 2;
134 roiA[2*lImageWidth] = 0;
135 roiA[2*lImageWidth+1] = 1;
136 roiA[2*lImageWidth+2] = 2;
141 roiB[lImageWidth] = 1;
142 roiB[lImageWidth+1] = 2;
143 roiB[lImageWidth+2] = 3;
144 roiB[2*lImageWidth] = 1;
145 roiB[2*lImageWidth+1] = 2;
146 roiB[2*lImageWidth+2] = 3;*/
147 /****************************************************************************************\
148 * Calculate derivatives *
149 \****************************************************************************************/
150 for (i=0; i<lImageHeight; i++)
152 for(j=0; j<lImageWidth; j++)
156 if ( j==lImageWidth-1 )
164 if ( i==(lImageHeight - 1) )
165 ib = lImageHeight - 1;
172 DerX[ i*lImageWidth + j ] = (float)
173 (roiA[ (it)*imgA->widthStep + jr ]
174 - roiA[ (it)*imgA->widthStep + jl ]
175 + 2*roiA[ (i)*imgA->widthStep + jr ]
176 - 2*roiA[ (i)*imgA->widthStep + jl ]
177 + roiA[ (ib)*imgA->widthStep + jr ]
178 - roiA[ (ib)*imgA->widthStep + jl ])/8 ;
180 DerY[ i*lImageWidth + j ] = (float)
181 ( roiA[ (ib)*imgA->widthStep + jl ]
182 + 2*roiA[ (ib)*imgA->widthStep + j ]
183 + roiA[ (ib)*imgA->widthStep + jr ]
184 - roiA[ (it)*imgA->widthStep + jl ]
185 - 2*roiA[ (it)*imgA->widthStep + j ]
186 - roiA[ (it)*imgA->widthStep + jr ])/8 ;
188 DerT[ i*lImageWidth + j ] = (float)
189 (roiB[i*imgB->widthStep + j] - roiA[i*imgA->widthStep + j]);
192 for( usePrevious = 0; usePrevious < 2; usePrevious++ )
194 /****************************************************************************************\
196 \****************************************************************************************/
197 for ( k = 0; k < 4; k++ )
203 criteria.type = CV_TERMCRIT_ITER;
204 criteria.max_iter = 3;
206 trsWrite( ATS_LST|ATS_CON,
207 "usePrevious = %d, criteria = ITER, max_iter = %d\n",
208 usePrevious, criteria.max_iter);
214 criteria.type = CV_TERMCRIT_EPS;
215 criteria.epsilon = 0.001f;
216 trsWrite( ATS_LST|ATS_CON,
217 "usePrevious = %d, criteria = EPS, epsilon = %f\n",
218 usePrevious, criteria.epsilon);
224 criteria.type = CV_TERMCRIT_EPS | CV_TERMCRIT_ITER;
225 criteria.epsilon = 0.0001f;
226 criteria.max_iter = 3;
227 trsWrite( ATS_LST|ATS_CON,
229 "criteria = EPS|ITER,"
230 "epsilon = %f, max_iter = %d\n",
231 usePrevious, criteria.epsilon, criteria.max_iter);
237 criteria.type = CV_TERMCRIT_EPS | CV_TERMCRIT_ITER;
238 criteria.epsilon = 0.00001f;
239 criteria.max_iter = 100;
240 trsWrite( ATS_LST|ATS_CON,
242 "criteria = EPS|ITER,"
243 "epsilon = %f, max_iter = %d\n",
244 usePrevious, criteria.epsilon, criteria.max_iter);
251 /* Run CVL function */
252 cvCalcOpticalFlowHS( imgA , imgB, usePrevious,
253 testVelocityX, testVelocityY,
256 /* Calc by other way */
259 /* Filling initial velocity with zero */
260 for (i = 0; i < lImageWidth * lImageHeight; i++ )
291 for( i = 0; i < lImageHeight; i++)
293 for(j = 0; j< lImageWidth; j++)
299 aveX +=(j==0) ? oldX[ i*lImageWidth + j ] : oldX[ i*lImageWidth + j-1 ];
300 aveX +=(j==lImageWidth-1) ? oldX[ i*lImageWidth + j ] :
301 oldX[ i*lImageWidth + j+1 ];
302 aveX +=(i==0) ? oldX[ i*lImageWidth + j ] : oldX[ (i-1)*lImageWidth + j ];
303 aveX +=(i==lImageHeight-1) ? oldX[ i*lImageWidth + j ] :
304 oldX[ (i+1)*lImageWidth + j ];
307 aveY +=(j==0) ? oldY[ i*lImageWidth + j ] : oldY[ i*lImageWidth + j-1 ];
308 aveY +=(j==lImageWidth-1) ? oldY[ i*lImageWidth + j ] :
309 oldY[ i*lImageWidth + j+1 ];
310 aveY +=(i==0) ? oldY[ i*lImageWidth + j ] : oldY[ (i-1)*lImageWidth + j ];
311 aveY +=(i==lImageHeight-1) ? oldY[ i*lImageWidth + j ] :
312 oldY[ (i+1)*lImageWidth + j ];
315 dx = DerX[ i*lImageWidth + j ];
316 dy = DerY[ i*lImageWidth + j ];
317 dt = DerT[ i*lImageWidth + j ];
319 /* Horn & Schunck pure formulas */
320 newX[ i*lImageWidth + j ] = aveX - ( dx * aveX +
321 dy * aveY + dt ) * lambda * dx /
322 (1 + lambda * ( dx*dx + dy*dy ));
324 newY[ i*lImageWidth + j ] = aveY - ( dx * aveX +
325 dy * aveY + dt ) * lambda * dy /
326 (1 + lambda * ( dx*dx + dy*dy ));
329 /* evaluate epsilon */
331 for ( i = 0; i < lImageHeight; i++)
333 for ( j = 0; j < lImageWidth; j++)
335 epsilon = MAX((float)fabs(newX[i*lImageWidth + j]
336 - oldX[i*lImageWidth + j]), epsilon );
337 epsilon = MAX((float)fabs(newY[i*lImageWidth + j]
338 - oldY[i*lImageWidth + j]), epsilon );
342 switch (criteria.type)
344 case CV_TERMCRIT_ITER:
345 Stop = (criteria.max_iter == iteration );break;
346 case CV_TERMCRIT_EPS:
347 Stop = (criteria.epsilon > epsilon );break;
348 case CV_TERMCRIT_ITER|CV_TERMCRIT_EPS:
349 Stop = ( ( criteria.epsilon > epsilon ) ||
350 ( criteria.max_iter == iteration ));
355 if ( (newX != VelocityX) && (newY != VelocityY) )
357 memcpy( VelocityX, newX, lImageWidth * lImageHeight * sizeof(float) );
358 memcpy( VelocityY, newY, lImageWidth * lImageHeight * sizeof(float) );
362 trsWrite( ATS_LST|ATS_CON,
363 "%d iterations are made\n", iteration );
365 for( i = 0; i < lImageHeight; i++)
367 for(j = 0; j< lImageWidth; j++)
369 float tvx = ((float*)(testVelocityX->imageData + i*testVelocityX->widthStep))[j];
370 float tvy = ((float*)(testVelocityY->imageData + i*testVelocityY->widthStep))[j];
372 if (( fabs( tvx - VelocityX[i*lImageWidth + j])>EPSILON )||
373 ( fabs( tvy - VelocityY[i*lImageWidth + j])>EPSILON ) )
375 //trsWrite( ATS_LST | ATS_CON, " ValueX %f \n",
376 // testVelocityX[i*lROIWidth + j] );
377 //trsWrite( ATS_LST | ATS_CON, " mustX %f \n",
378 // VelocityX[i*lROIWidth + j] );
380 //trsWrite( ATS_LST | ATS_CON, " ValueY %f \n",
381 // testVelocityY[i*lROIWidth + j] );
382 //trsWrite( ATS_LST | ATS_CON, " mustY %f \n",
383 // VelocityY[i*lROIWidth + j] );
385 //trsWrite( ATS_LST | ATS_CON, " Coordinates %d %d\n", i, j );
392 /* Filling initial velocity with zero */
393 cvZero( testVelocityX );
394 cvZero( testVelocityY );
395 for (i = 0; i < lImageWidth * lImageHeight; i++ )
403 cvFree( &VelocityX );
404 cvFree( &VelocityY );
405 cvFree( &auxVelocityX );
406 cvFree( &auxVelocityY );
413 cvReleaseImage( &imgA );
414 cvReleaseImage( &imgB );
415 cvReleaseImage( &testVelocityX );
416 cvReleaseImage( &testVelocityY );
419 if( lErrors == 0 ) return trsResult( TRS_OK, "No errors fixed for this text" );
420 else return trsResult( TRS_FAIL, "Total fixed %d errors", lErrors );
421 } /*fmaCalcOpticalFlowHS*/
423 void InitACalcOpticalFlowHS( void )
425 /* Registering test function */
426 trsReg( FuncName, TestName, TestClass, fmaCalcOpticalFlowHS );
427 } /* InitACalcOpticalFlowHS */