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 #include "precomp.hpp"
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" );
79 (srcA->width - blockSize.width + shiftSize.width)/shiftSize.width,
80 (srcA->height - blockSize.height + shiftSize.height)/shiftSize.height
83 if( !CV_ARE_SIZES_EQ( srcA, srcB ) ||
84 !CV_ARE_SIZES_EQ( velx, vely ) ||
85 velx->width != velSize.width ||
86 vely->height != velSize.height )
87 CV_Error( CV_StsUnmatchedSizes, "" );
89 if( CV_MAT_TYPE( srcA->type ) != CV_8UC1 ||
90 CV_MAT_TYPE( velx->type ) != CV_32FC1 )
91 CV_Error( CV_StsUnsupportedFormat, "Source images must have 8uC1 type and "
92 "destination images must have 32fC1 type" );
94 if( srcA->step != srcB->step || velx->step != vely->step )
95 CV_Error( CV_BadStep, "two source or two destination images have different steps" );
97 const int SMALL_DIFF=2;
98 const int BIG_DIFF=128;
100 // scanning scheme coordinates
101 std::vector<CvPoint> _ss((2 * maxRange.width + 1) * (2 * maxRange.height + 1));
102 CvPoint* ss = &_ss[0];
105 int blWidth = blockSize.width, blHeight = blockSize.height;
106 int blSize = blWidth*blHeight;
107 int acceptLevel = blSize * SMALL_DIFF;
108 int escapeLevel = blSize * BIG_DIFF;
112 std::vector<uchar> _blockA(cvAlign(blSize + 16, 16));
113 uchar* blockA = (uchar*)cvAlignPtr(&_blockA[0], 16);
115 // Calculate scanning scheme
116 int min_count = MIN( maxRange.width, maxRange.height );
118 // use spiral search pattern
127 for( i = 0; i < min_count; i++ )
129 // four cycles along sides
133 for( j = -i; j <= i + 1; j++, ss_count++ )
135 ss[ss_count].x = ++x;
140 for( j = -i; j <= i + 1; j++, ss_count++ )
143 ss[ss_count].y = ++y;
147 for( j = -i; j <= i + 1; j++, ss_count++ )
149 ss[ss_count].x = --x;
154 for( j = -i; j <= i + 1; j++, ss_count++ )
157 ss[ss_count].y = --y;
162 if( maxRange.width < maxRange.height )
164 int xleft = -min_count;
166 // cycle by neighbor rings
167 for( i = min_count; i < maxRange.height; i++ )
174 for( j = -maxRange.width; j <= maxRange.width; j++, ss_count++, x++ )
183 for( j = -maxRange.width; j <= maxRange.width; j++, ss_count++, x++ )
190 else if( maxRange.width > maxRange.height )
192 int yupper = -min_count;
194 // cycle by neighbor rings
195 for( i = min_count; i < maxRange.width; i++ )
202 for( j = -maxRange.height; j <= maxRange.height; j++, ss_count++, y++ )
211 for( j = -maxRange.height; j <= maxRange.height; j++, ss_count++, y++ )
219 int maxX = srcB->cols - blockSize.width, maxY = srcB->rows - blockSize.height;
220 const uchar* Adata = srcA->data.ptr;
221 const uchar* Bdata = srcB->data.ptr;
222 int Astep = srcA->step, Bstep = srcB->step;
225 for( i = 0; i < velx->rows; i++ )
227 float* vx = (float*)(velx->data.ptr + velx->step*i);
228 float* vy = (float*)(vely->data.ptr + vely->step*i);
230 for( j = 0; j < velx->cols; j++ )
232 int X1 = j*shiftSize.width, Y1 = i*shiftSize.height, X2, Y2;
233 int offX = 0, offY = 0;
237 offX = cvRound(vx[j]);
238 offY = cvRound(vy[j]);
242 for( k = 0; k < blHeight; k++ )
243 memcpy( blockA + k*blWidth, Adata + Astep*(Y1 + k) + X1, blWidth );
248 if( 0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY )
249 dist = cmpBlocks( blockA, Bdata + Bstep*Y2 + X2, Bstep, blockSize );
252 int sumx = offX, sumy = offY;
254 if( dist > acceptLevel )
256 // do brute-force search
257 for( k = 0; k < ss_count; k++ )
259 int dx = offX + ss[k].x;
260 int dy = offY + ss[k].y;
264 if( !(0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY) )
267 int tmpDist = cmpBlocks( blockA, Bdata + Bstep*Y2 + X2, Bstep, blockSize );
268 if( tmpDist < acceptLevel )
270 sumx = dx; sumy = dy;
278 sumx = dx; sumy = dy;
281 else if( tmpDist == dist )
283 sumx += dx; sumy += dy;
288 if( dist > escapeLevel )
296 vx[j] = (float)sumx/countMin;
297 vy[j] = (float)sumy/countMin;