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.
45 static inline int cmpBlocks(const uchar* A, const uchar* B, int Bstep, CvSize blockSize )
48 for( ; blockSize.height--; A += blockSize.width, B += Bstep )
50 for( x = 0; x <= blockSize.width - 4; x += 4 )
51 s += std::abs(A[x] - B[x]) + std::abs(A[x+1] - B[x+1]) +
52 std::abs(A[x+2] - B[x+2]) + std::abs(A[x+3] - B[x+3]);
53 for( ; x < blockSize.width; x++ )
54 s += std::abs(A[x] - B[x]);
61 cvCalcOpticalFlowBM( const void* srcarrA, const void* srcarrB,
62 CvSize blockSize, CvSize shiftSize,
63 CvSize maxRange, int usePrevious,
64 void* velarrx, void* velarry )
66 CvMat stubA, *srcA = cvGetMat( srcarrA, &stubA );
67 CvMat stubB, *srcB = cvGetMat( srcarrB, &stubB );
69 CvMat stubx, *velx = cvGetMat( velarrx, &stubx );
70 CvMat stuby, *vely = cvGetMat( velarry, &stuby );
72 if( !CV_ARE_TYPES_EQ( srcA, srcB ))
73 CV_Error( CV_StsUnmatchedFormats, "Source images have different formats" );
75 if( !CV_ARE_TYPES_EQ( velx, vely ))
76 CV_Error( CV_StsUnmatchedFormats, "Destination images have different formats" );
80 (srcA->width - blockSize.width)/shiftSize.width,
81 (srcA->height - blockSize.height)/shiftSize.height
84 if( !CV_ARE_SIZES_EQ( srcA, srcB ) ||
85 !CV_ARE_SIZES_EQ( velx, vely ) ||
86 velx->width != velSize.width ||
87 vely->height != velSize.height )
88 CV_Error( CV_StsUnmatchedSizes, "" );
90 if( CV_MAT_TYPE( srcA->type ) != CV_8UC1 ||
91 CV_MAT_TYPE( velx->type ) != CV_32FC1 )
92 CV_Error( CV_StsUnsupportedFormat, "Source images must have 8uC1 type and "
93 "destination images must have 32fC1 type" );
95 if( srcA->step != srcB->step || velx->step != vely->step )
96 CV_Error( CV_BadStep, "two source or two destination images have different steps" );
98 const int SMALL_DIFF=2;
99 const int BIG_DIFF=128;
101 // scanning scheme coordinates
102 cv::vector<CvPoint> _ss((2 * maxRange.width + 1) * (2 * maxRange.height + 1));
103 CvPoint* ss = &_ss[0];
106 int blWidth = blockSize.width, blHeight = blockSize.height;
107 int blSize = blWidth*blHeight;
108 int acceptLevel = blSize * SMALL_DIFF;
109 int escapeLevel = blSize * BIG_DIFF;
113 cv::vector<uchar> _blockA(cvAlign(blSize + 16, 16));
114 uchar* blockA = (uchar*)cvAlignPtr(&_blockA[0], 16);
116 // Calculate scanning scheme
117 int min_count = MIN( maxRange.width, maxRange.height );
119 // use spiral search pattern
128 for( i = 0; i < min_count; i++ )
130 // four cycles along sides
134 for( j = -i; j <= i + 1; j++, ss_count++ )
136 ss[ss_count].x = ++x;
141 for( j = -i; j <= i + 1; j++, ss_count++ )
144 ss[ss_count].y = ++y;
148 for( j = -i; j <= i + 1; j++, ss_count++ )
150 ss[ss_count].x = --x;
155 for( j = -i; j <= i + 1; j++, ss_count++ )
158 ss[ss_count].y = --y;
163 if( maxRange.width < maxRange.height )
165 int xleft = -min_count;
167 // cycle by neighbor rings
168 for( i = min_count; i < maxRange.height; i++ )
175 for( j = -maxRange.width; j <= maxRange.width; j++, ss_count++, x++ )
184 for( j = -maxRange.width; j <= maxRange.width; j++, ss_count++, x++ )
191 else if( maxRange.width > maxRange.height )
193 int yupper = -min_count;
195 // cycle by neighbor rings
196 for( i = min_count; i < maxRange.width; i++ )
203 for( j = -maxRange.height; j <= maxRange.height; j++, ss_count++, y++ )
212 for( j = -maxRange.height; j <= maxRange.height; j++, ss_count++, y++ )
220 int maxX = srcB->cols - blockSize.width, maxY = srcB->rows - blockSize.height;
221 const uchar* Adata = srcA->data.ptr;
222 const uchar* Bdata = srcB->data.ptr;
223 int Astep = srcA->step, Bstep = srcB->step;
226 for( i = 0; i < velx->rows; i++ )
228 float* vx = (float*)(velx->data.ptr + velx->step*i);
229 float* vy = (float*)(vely->data.ptr + vely->step*i);
231 for( j = 0; j < velx->cols; j++ )
233 int X1 = j*shiftSize.width, Y1 = i*shiftSize.height, X2, Y2;
234 int offX = 0, offY = 0;
238 offX = cvRound(vx[j]);
239 offY = cvRound(vy[j]);
243 for( k = 0; k < blHeight; k++ )
244 memcpy( blockA + k*blWidth, Adata + Astep*(Y1 + k) + X1, blWidth );
249 if( 0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY )
250 dist = cmpBlocks( blockA, Bdata + Bstep*Y2 + X2, Bstep, blockSize );
253 int sumx = offX, sumy = offY;
255 if( dist > acceptLevel )
257 // do brute-force search
258 for( k = 0; k < ss_count; k++ )
260 int dx = offX + ss[k].x;
261 int dy = offY + ss[k].y;
265 if( !(0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY) )
268 int tmpDist = cmpBlocks( blockA, Bdata + Bstep*Y2 + X2, Bstep, blockSize );
269 if( tmpDist < acceptLevel )
271 sumx = dx; sumy = dy;
279 sumx = dx; sumy = dy;
282 else if( tmpDist == dist )
284 sumx += dx; sumy += dy;
289 if( dist > escapeLevel )
297 vx[j] = (float)sumx/countMin;
298 vy[j] = (float)sumy/countMin;