Advertisement
Guest User

OpenCV SqDiff

a guest
Jan 16th, 2016
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  2. static void common_matchTemplate( Mat& img, Mat& templ, Mat& result, int method, int cn )
  3. {
  4.     int numType = 2;
  5.     bool isNormed = method == CV_TM_SQDIFF_NORMED;
  6.  
  7.     double invArea = 1./((double)templ.rows * templ.cols);
  8.  
  9.     Mat sum, sqsum;
  10.     Scalar templMean, templSdv;
  11.     double *q0 = 0, *q1 = 0, *q2 = 0, *q3 = 0;
  12.     double templNorm = 0, templSum2 = 0;
  13.  
  14.     integral(img, sum, sqsum, CV_64F);
  15.     meanStdDev( templ, templMean, templSdv );
  16.  
  17.     templNorm = templSdv[0]*templSdv[0] + templSdv[1]*templSdv[1] + templSdv[2]*templSdv[2] + templSdv[3]*templSdv[3];
  18.  
  19.     templSum2 = templNorm + templMean[0]*templMean[0] + templMean[1]*templMean[1] + templMean[2]*templMean[2] + templMean[3]*templMean[3];
  20.  
  21.     templMean = Scalar::all(0);
  22.     templNorm = templSum2;
  23.  
  24.     templSum2 /= invArea;
  25.     templNorm = std::sqrt(templNorm);
  26.     templNorm /= std::sqrt(invArea); // care of accuracy here
  27.  
  28.     q0 = (double*)sqsum.data;
  29.     q1 = q0 + templ.cols*cn;
  30.     q2 = (double*)(sqsum.data + templ.rows*sqsum.step);
  31.     q3 = q2 + templ.cols*cn;
  32.  
  33.     double* p0 = (double*)sum.data;
  34.     double* p1 = p0 + templ.cols*cn;
  35.     double* p2 = (double*)(sum.data + templ.rows*sum.step);
  36.     double* p3 = p2 + templ.cols*cn;
  37.  
  38.     int sumstep = sum.data ? (int)(sum.step / sizeof(double)) : 0;
  39.     int sqstep = sqsum.data ? (int)(sqsum.step / sizeof(double)) : 0;
  40.  
  41.     int i, j, k;
  42.  
  43.     for( i = 0; i < result.rows; i++ )
  44.     {
  45.         float* rrow = result.ptr<float>(i);
  46.         int idx = i * sumstep;
  47.         int idx2 = i * sqstep;
  48.  
  49.         for( j = 0; j < result.cols; j++, idx += cn, idx2 += cn )
  50.         {
  51.             double num = rrow[j], t;
  52.             double wndMean2 = 0, wndSum2 = 0;
  53.  
  54.             for( k = 0; k < cn; k++ )
  55.             {
  56.                 t = q0[idx2+k] - q1[idx2+k] - q2[idx2+k] + q3[idx2+k];
  57.                 wndSum2 += t;
  58.             }
  59.  
  60.             num = wndSum2 - 2*num + templSum2;
  61.             num = MAX(num, 0.);
  62.  
  63.             if( isNormed )
  64.             {
  65.                 t = std::sqrt(MAX(wndSum2 - wndMean2,0))*templNorm;
  66.                 if( fabs(num) < t )
  67.                     num /= t;
  68.                 else if( fabs(num) < t*1.125 )
  69.                     num = num > 0 ? 1 : -1;
  70.                 else
  71.                     num = 1;
  72.             }
  73.  
  74.             rrow[j] = (float)num;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement