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 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 15 // Third party copyrights are property of their respective owners. 16 // 17 // Redistribution and use in source and binary forms, with or without modification, 18 // are permitted provided that the following conditions are met: 19 // 20 // * Redistribution's of source code must retain the above copyright notice, 21 // this list of conditions and the following disclaimer. 22 // 23 // * Redistribution's in binary form must reproduce the above copyright notice, 24 // this list of conditions and the following disclaimer in the documentation 25 // and/or other materials provided with the distribution. 26 // 27 // * The name of the copyright holders may not be used to endorse or promote products 28 // derived from this software without specific prior written permission. 29 // 30 // This software is provided by the copyright holders and contributors "as is" and 31 // any express or implied warranties, including, but not limited to, the implied 32 // warranties of merchantability and fitness for a particular purpose are disclaimed. 33 // In no event shall the Intel Corporation or contributors be liable for any direct, 34 // indirect, incidental, special, exemplary, or consequential damages 35 // (including, but not limited to, procurement of substitute goods or services; 36 // loss of use, data, or profits; or business interruption) however caused 37 // and on any theory of liability, whether in contract, strict liability, 38 // or tort (including negligence or otherwise) arising in any way out of 39 // the use of this software, even if advised of the possibility of such damage. 40 // 41 //M*/ 42 43 #if !defined CUDA_DISABLER 44 45 #include "opencv2/core/cuda/common.hpp" 46 #include "opencv2/core/cuda/limits.hpp" 47 #include "opencv2/core/cuda/functional.hpp" 48 #include "opencv2/core/cuda/reduce.hpp" 49 50 using namespace cv::cuda; 51 using namespace cv::cuda::device; 52 53 namespace optflowbm 54 { 55 texture<uchar, cudaTextureType2D, cudaReadModeElementType> tex_prev(false, cudaFilterModePoint, cudaAddressModeClamp); 56 texture<uchar, cudaTextureType2D, cudaReadModeElementType> tex_curr(false, cudaFilterModePoint, cudaAddressModeClamp); 57 cmpBlocks(int X1,int Y1,int X2,int Y2,int2 blockSize)58 __device__ int cmpBlocks(int X1, int Y1, int X2, int Y2, int2 blockSize) 59 { 60 int s = 0; 61 62 for (int y = 0; y < blockSize.y; ++y) 63 { 64 for (int x = 0; x < blockSize.x; ++x) 65 s += ::abs(tex2D(tex_prev, X1 + x, Y1 + y) - tex2D(tex_curr, X2 + x, Y2 + y)); 66 } 67 68 return s; 69 } 70 calcOptFlowBM(PtrStepSzf velx,PtrStepf vely,const int2 blockSize,const int2 shiftSize,const bool usePrevious,const int maxX,const int maxY,const int acceptLevel,const int escapeLevel,const short2 * ss,const int ssCount)71 __global__ void calcOptFlowBM(PtrStepSzf velx, PtrStepf vely, const int2 blockSize, const int2 shiftSize, const bool usePrevious, 72 const int maxX, const int maxY, const int acceptLevel, const int escapeLevel, 73 const short2* ss, const int ssCount) 74 { 75 const int j = blockIdx.x * blockDim.x + threadIdx.x; 76 const int i = blockIdx.y * blockDim.y + threadIdx.y; 77 78 if (i >= velx.rows || j >= velx.cols) 79 return; 80 81 const int X1 = j * shiftSize.x; 82 const int Y1 = i * shiftSize.y; 83 84 const int offX = usePrevious ? __float2int_rn(velx(i, j)) : 0; 85 const int offY = usePrevious ? __float2int_rn(vely(i, j)) : 0; 86 87 int X2 = X1 + offX; 88 int Y2 = Y1 + offY; 89 90 int dist = numeric_limits<int>::max(); 91 92 if (0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY) 93 dist = cmpBlocks(X1, Y1, X2, Y2, blockSize); 94 95 int countMin = 1; 96 int sumx = offX; 97 int sumy = offY; 98 99 if (dist > acceptLevel) 100 { 101 // do brute-force search 102 for (int k = 0; k < ssCount; ++k) 103 { 104 const short2 ssVal = ss[k]; 105 106 const int dx = offX + ssVal.x; 107 const int dy = offY + ssVal.y; 108 109 X2 = X1 + dx; 110 Y2 = Y1 + dy; 111 112 if (0 <= X2 && X2 <= maxX && 0 <= Y2 && Y2 <= maxY) 113 { 114 const int tmpDist = cmpBlocks(X1, Y1, X2, Y2, blockSize); 115 if (tmpDist < acceptLevel) 116 { 117 sumx = dx; 118 sumy = dy; 119 countMin = 1; 120 break; 121 } 122 123 if (tmpDist < dist) 124 { 125 dist = tmpDist; 126 sumx = dx; 127 sumy = dy; 128 countMin = 1; 129 } 130 else if (tmpDist == dist) 131 { 132 sumx += dx; 133 sumy += dy; 134 countMin++; 135 } 136 } 137 } 138 139 if (dist > escapeLevel) 140 { 141 sumx = offX; 142 sumy = offY; 143 countMin = 1; 144 } 145 } 146 147 velx(i, j) = static_cast<float>(sumx) / countMin; 148 vely(i, j) = static_cast<float>(sumy) / countMin; 149 } 150 calc(PtrStepSzb prev,PtrStepSzb curr,PtrStepSzf velx,PtrStepSzf vely,int2 blockSize,int2 shiftSize,bool usePrevious,int maxX,int maxY,int acceptLevel,int escapeLevel,const short2 * ss,int ssCount,cudaStream_t stream)151 void calc(PtrStepSzb prev, PtrStepSzb curr, PtrStepSzf velx, PtrStepSzf vely, int2 blockSize, int2 shiftSize, bool usePrevious, 152 int maxX, int maxY, int acceptLevel, int escapeLevel, const short2* ss, int ssCount, cudaStream_t stream) 153 { 154 bindTexture(&tex_prev, prev); 155 bindTexture(&tex_curr, curr); 156 157 const dim3 block(32, 8); 158 const dim3 grid(divUp(velx.cols, block.x), divUp(vely.rows, block.y)); 159 160 calcOptFlowBM<<<grid, block, 0, stream>>>(velx, vely, blockSize, shiftSize, usePrevious, 161 maxX, maxY, acceptLevel, escapeLevel, ss, ssCount); 162 cudaSafeCall( cudaGetLastError() ); 163 164 if (stream == 0) 165 cudaSafeCall( cudaDeviceSynchronize() ); 166 } 167 } 168 169 #endif // !defined CUDA_DISABLER 170