Path: blob/master/modules/video/src/opencl/optical_flow_tvl1.cl
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.13// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// @Authors17// Jin Ma jin@multicorewareinc.com18//19// Redistribution and use in source and binary forms, with or without modification,20// are permitted provided that the following conditions are met:21//22// * Redistribution's of source code must retain the above copyright notice,23// this list of conditions and the following disclaimer.24//25// * Redistribution's in binary form must reproduce the above copyright notice,26// this list of conditions and the following disclaimer in the documentation27// and/or other materials provided with the distribution.28//29// * The name of the copyright holders may not be used to endorse or promote products30// derived from this software without specific prior written permission.31//32// This software is provided by the copyright holders and contributors as is and33// any express or implied warranties, including, but not limited to, the implied34// warranties of merchantability and fitness for a particular purpose are disclaimed.35// In no event shall the Intel Corporation or contributors be liable for any direct,36// indirect, incidental, special, exemplary, or consequential damages37// (including, but not limited to, procurement of substitute goods or services;38// loss of use, data, or profits; or business interruption) however caused39// and on any theory of liability, whether in contract, strict liability,40// or tort (including negligence or otherwise) arising in any way out of41// the use of this software, even if advised of the possibility of such damage.42//43//M*/4445__kernel void centeredGradientKernel(__global const float* src_ptr, int src_col, int src_row, int src_step,46__global float* dx, __global float* dy, int d_step)47{48int x = get_global_id(0);49int y = get_global_id(1);5051if((x < src_col)&&(y < src_row))52{53int src_x1 = (x + 1) < (src_col -1)? (x + 1) : (src_col - 1);54int src_x2 = (x - 1) > 0 ? (x -1) : 0;55dx[y * d_step+ x] = 0.5f * (src_ptr[y * src_step + src_x1] - src_ptr[y * src_step+ src_x2]);5657int src_y1 = (y+1) < (src_row - 1) ? (y + 1) : (src_row - 1);58int src_y2 = (y - 1) > 0 ? (y - 1) : 0;59dy[y * d_step+ x] = 0.5f * (src_ptr[src_y1 * src_step + x] - src_ptr[src_y2 * src_step+ x]);60}6162}6364inline float bicubicCoeff(float x_)65{6667float x = fabs(x_);68if (x <= 1.0f)69return x * x * (1.5f * x - 2.5f) + 1.0f;70else if (x < 2.0f)71return x * (x * (-0.5f * x + 2.5f) - 4.0f) + 2.0f;72else73return 0.0f;74}7576__kernel void warpBackwardKernel(__global const float* I0, int I0_step, int I0_col, int I0_row,77image2d_t tex_I1, image2d_t tex_I1x, image2d_t tex_I1y,78__global const float* u1, int u1_step,79__global const float* u2,80__global float* I1w,81__global float* I1wx, /*int I1wx_step,*/82__global float* I1wy, /*int I1wy_step,*/83__global float* grad, /*int grad_step,*/84__global float* rho,85int I1w_step,86int u2_step,87int u1_offset_x,88int u1_offset_y,89int u2_offset_x,90int u2_offset_y)91{92int x = get_global_id(0);93int y = get_global_id(1);9495if(x < I0_col&&y < I0_row)96{97//float u1Val = u1(y, x);98float u1Val = u1[(y + u1_offset_y) * u1_step + x + u1_offset_x];99//float u2Val = u2(y, x);100float u2Val = u2[(y + u2_offset_y) * u2_step + x + u2_offset_x];101102float wx = x + u1Val;103float wy = y + u2Val;104105int xmin = ceil(wx - 2.0f);106int xmax = floor(wx + 2.0f);107108int ymin = ceil(wy - 2.0f);109int ymax = floor(wy + 2.0f);110111float sum = 0.0f;112float sumx = 0.0f;113float sumy = 0.0f;114float wsum = 0.0f;115sampler_t sampleri = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;116for (int cy = ymin; cy <= ymax; ++cy)117{118for (int cx = xmin; cx <= xmax; ++cx)119{120float w = bicubicCoeff(wx - cx) * bicubicCoeff(wy - cy);121//sum += w * tex2D(tex_I1 , cx, cy);122int2 cood = (int2)(cx, cy);123sum += w * read_imagef(tex_I1, sampleri, cood).x;124//sumx += w * tex2D(tex_I1x, cx, cy);125sumx += w * read_imagef(tex_I1x, sampleri, cood).x;126//sumy += w * tex2D(tex_I1y, cx, cy);127sumy += w * read_imagef(tex_I1y, sampleri, cood).x;128wsum += w;129}130}131float coeff = 1.0f / wsum;132float I1wVal = sum * coeff;133float I1wxVal = sumx * coeff;134float I1wyVal = sumy * coeff;135I1w[y * I1w_step + x] = I1wVal;136I1wx[y * I1w_step + x] = I1wxVal;137I1wy[y * I1w_step + x] = I1wyVal;138float Ix2 = I1wxVal * I1wxVal;139float Iy2 = I1wyVal * I1wyVal;140141// store the |Grad(I1)|^2142grad[y * I1w_step + x] = Ix2 + Iy2;143144// compute the constant part of the rho function145float I0Val = I0[y * I0_step + x];146rho[y * I1w_step + x] = I1wVal - I1wxVal * u1Val - I1wyVal * u2Val - I0Val;147}148}149150inline float readImage(__global const float *image, int x, int y, int rows, int cols, int elemCntPerRow)151{152int i0 = clamp(x, 0, cols - 1);153int j0 = clamp(y, 0, rows - 1);154155return image[j0 * elemCntPerRow + i0];156}157158__kernel void warpBackwardKernelNoImage2d(__global const float* I0, int I0_step, int I0_col, int I0_row,159__global const float* tex_I1, __global const float* tex_I1x, __global const float* tex_I1y,160__global const float* u1, int u1_step,161__global const float* u2,162__global float* I1w,163__global float* I1wx, /*int I1wx_step,*/164__global float* I1wy, /*int I1wy_step,*/165__global float* grad, /*int grad_step,*/166__global float* rho,167int I1w_step,168int u2_step,169int I1_step,170int I1x_step)171{172int x = get_global_id(0);173int y = get_global_id(1);174175if(x < I0_col&&y < I0_row)176{177//float u1Val = u1(y, x);178float u1Val = u1[y * u1_step + x];179//float u2Val = u2(y, x);180float u2Val = u2[y * u2_step + x];181182float wx = x + u1Val;183float wy = y + u2Val;184185int xmin = ceil(wx - 2.0f);186int xmax = floor(wx + 2.0f);187188int ymin = ceil(wy - 2.0f);189int ymax = floor(wy + 2.0f);190191float sum = 0.0f;192float sumx = 0.0f;193float sumy = 0.0f;194float wsum = 0.0f;195196for (int cy = ymin; cy <= ymax; ++cy)197{198for (int cx = xmin; cx <= xmax; ++cx)199{200float w = bicubicCoeff(wx - cx) * bicubicCoeff(wy - cy);201202int2 cood = (int2)(cx, cy);203sum += w * readImage(tex_I1, cood.x, cood.y, I0_col, I0_row, I1_step);204sumx += w * readImage(tex_I1x, cood.x, cood.y, I0_col, I0_row, I1x_step);205sumy += w * readImage(tex_I1y, cood.x, cood.y, I0_col, I0_row, I1x_step);206wsum += w;207}208}209210float coeff = 1.0f / wsum;211212float I1wVal = sum * coeff;213float I1wxVal = sumx * coeff;214float I1wyVal = sumy * coeff;215216I1w[y * I1w_step + x] = I1wVal;217I1wx[y * I1w_step + x] = I1wxVal;218I1wy[y * I1w_step + x] = I1wyVal;219220float Ix2 = I1wxVal * I1wxVal;221float Iy2 = I1wyVal * I1wyVal;222223// store the |Grad(I1)|^2224grad[y * I1w_step + x] = Ix2 + Iy2;225226// compute the constant part of the rho function227float I0Val = I0[y * I0_step + x];228rho[y * I1w_step + x] = I1wVal - I1wxVal * u1Val - I1wyVal * u2Val - I0Val;229}230231}232233234__kernel void estimateDualVariablesKernel(__global const float* u1, int u1_col, int u1_row, int u1_step,235__global const float* u2,236__global float* p11, int p11_step,237__global float* p12,238__global float* p21,239__global float* p22,240float taut,241int u2_step,242int u1_offset_x,243int u1_offset_y,244int u2_offset_x,245int u2_offset_y)246{247int x = get_global_id(0);248int y = get_global_id(1);249250if(x < u1_col && y < u1_row)251{252int src_x1 = (x + 1) < (u1_col - 1) ? (x + 1) : (u1_col - 1);253float u1x = u1[(y + u1_offset_y) * u1_step + src_x1 + u1_offset_x] - u1[(y + u1_offset_y) * u1_step + x + u1_offset_x];254255int src_y1 = (y + 1) < (u1_row - 1) ? (y + 1) : (u1_row - 1);256float u1y = u1[(src_y1 + u1_offset_y) * u1_step + x + u1_offset_x] - u1[(y + u1_offset_y) * u1_step + x + u1_offset_x];257258int src_x2 = (x + 1) < (u1_col - 1) ? (x + 1) : (u1_col - 1);259float u2x = u2[(y + u2_offset_y) * u2_step + src_x2 + u2_offset_x] - u2[(y + u2_offset_y) * u2_step + x + u2_offset_x];260261int src_y2 = (y + 1) < (u1_row - 1) ? (y + 1) : (u1_row - 1);262float u2y = u2[(src_y2 + u2_offset_y) * u2_step + x + u2_offset_x] - u2[(y + u2_offset_y) * u2_step + x + u2_offset_x];263264float g1 = hypot(u1x, u1y);265float g2 = hypot(u2x, u2y);266267float ng1 = 1.0f + taut * g1;268float ng2 = 1.0f + taut * g2;269270p11[y * p11_step + x] = (p11[y * p11_step + x] + taut * u1x) / ng1;271p12[y * p11_step + x] = (p12[y * p11_step + x] + taut * u1y) / ng1;272p21[y * p11_step + x] = (p21[y * p11_step + x] + taut * u2x) / ng2;273p22[y * p11_step + x] = (p22[y * p11_step + x] + taut * u2y) / ng2;274}275276}277278inline float divergence(__global const float* v1, __global const float* v2, int y, int x, int v1_step, int v2_step)279{280281if (x > 0 && y > 0)282{283float v1x = v1[y * v1_step + x] - v1[y * v1_step + x - 1];284float v2y = v2[y * v2_step + x] - v2[(y - 1) * v2_step + x];285return v1x + v2y;286}287else288{289if (y > 0)290return v1[y * v1_step + 0] + v2[y * v2_step + 0] - v2[(y - 1) * v2_step + 0];291else292{293if (x > 0)294return v1[0 * v1_step + x] - v1[0 * v1_step + x - 1] + v2[0 * v2_step + x];295else296return v1[0 * v1_step + 0] + v2[0 * v2_step + 0];297}298}299300}301302__kernel void estimateUKernel(__global const float* I1wx, int I1wx_col, int I1wx_row, int I1wx_step,303__global const float* I1wy, /*int I1wy_step,*/304__global const float* grad, /*int grad_step,*/305__global const float* rho_c, /*int rho_c_step,*/306__global const float* p11, /*int p11_step,*/307__global const float* p12, /*int p12_step,*/308__global const float* p21, /*int p21_step,*/309__global const float* p22, /*int p22_step,*/310__global float* u1, int u1_step,311__global float* u2,312__global float* error, float l_t, float theta, int u2_step,313int u1_offset_x,314int u1_offset_y,315int u2_offset_x,316int u2_offset_y,317char calc_error)318{319int x = get_global_id(0);320int y = get_global_id(1);321322if(x < I1wx_col && y < I1wx_row)323{324float I1wxVal = I1wx[y * I1wx_step + x];325float I1wyVal = I1wy[y * I1wx_step + x];326float gradVal = grad[y * I1wx_step + x];327float u1OldVal = u1[(y + u1_offset_y) * u1_step + x + u1_offset_x];328float u2OldVal = u2[(y + u2_offset_y) * u2_step + x + u2_offset_x];329330float rho = rho_c[y * I1wx_step + x] + (I1wxVal * u1OldVal + I1wyVal * u2OldVal);331332// estimate the values of the variable (v1, v2) (thresholding operator TH)333334float d1 = 0.0f;335float d2 = 0.0f;336337if (rho < -l_t * gradVal)338{339d1 = l_t * I1wxVal;340d2 = l_t * I1wyVal;341}342else if (rho > l_t * gradVal)343{344d1 = -l_t * I1wxVal;345d2 = -l_t * I1wyVal;346}347else if (gradVal > 1.192092896e-07f)348{349float fi = -rho / gradVal;350d1 = fi * I1wxVal;351d2 = fi * I1wyVal;352}353354float v1 = u1OldVal + d1;355float v2 = u2OldVal + d2;356357// compute the divergence of the dual variable (p1, p2)358359float div_p1 = divergence(p11, p12, y, x, I1wx_step, I1wx_step);360float div_p2 = divergence(p21, p22, y, x, I1wx_step, I1wx_step);361362// estimate the values of the optical flow (u1, u2)363364float u1NewVal = v1 + theta * div_p1;365float u2NewVal = v2 + theta * div_p2;366367u1[(y + u1_offset_y) * u1_step + x + u1_offset_x] = u1NewVal;368u2[(y + u2_offset_y) * u2_step + x + u2_offset_x] = u2NewVal;369370if(calc_error)371{372float n1 = (u1OldVal - u1NewVal) * (u1OldVal - u1NewVal);373float n2 = (u2OldVal - u2NewVal) * (u2OldVal - u2NewVal);374error[y * I1wx_step + x] = n1 + n2;375}376}377}378379380