0

I have this interpolate function taken from this code which I want to optimize:

bool interpolate(const Mat &im, float ofsx, float ofsy, float a11, float a12, float a21, float a22, Mat &res)
{         
   bool ret = false;
   // input size (-1 for the safe bilinear interpolation)
   const int width = im.cols-1;
   const int height = im.rows-1;
   // output size
   const int halfWidth  = res.cols >> 1;
   const int halfHeight = res.rows >> 1;
   int dim = res.rows * res.cols;
   float *out = res.ptr<float>(0);
   for (int j=-halfHeight; j<=halfHeight; ++j)
   {
      const float rx = ofsx + j * a12;
      const float ry = ofsy + j * a22;
      #pragma omp simd
      for(int i=-halfWidth; i<=halfWidth; ++i)
      {
         float wx = rx + i * a11;
         float wy = ry + i * a21;
         const int x = (int) floor(wx);
         const int y = (int) floor(wy);
         if (x >= 0 && y >= 0 && x < width && y < height)
         {
            // compute weights
            wx -= x; wy -= y;
            // bilinear interpolation
            *out++ = 
               (1.0f - wy) * ((1.0f - wx) * im.at<float>(y,x)   + wx * im.at<float>(y,x+1)) +
               (       wy) * ((1.0f - wx) * im.at<float>(y+1,x) + wx * im.at<float>(y+1,x+1));
         } else {
            *out++ = 0;
            ret =  true; // touching boundary of the input            
         }
      }
   }
   return ret;
}

I'm not the image processing guy and I'm really struggling to perform the equivalent using the opencv implementation. This is crucial for my project since the function above is incredibly time consuming.

Please help.

To help you, these are examples in different parts of the code where interpolate is used:

  // warp input according to current shape matrix
  interpolate(wrapper.prevBlur, lx, ly, u11*ratio, u12*ratio, u21*ratio, u22*ratio, img);

  Mat smoothed(patchImageSize, patchImageSize, CV_32FC1, (void *)&workspace.front());
  // interpolate with det == 1
  if (!interpolate(img, x, y, a11, a12, a21, a22, smoothed))
  {

  // subsample with corresponding scale
  interpolate(smoothed, (float)(patchImageSize>>1), (float)(patchImageSize>>1), imageToPatchScale, 0, 0, imageToPatchScale, patch);


  // ok, do the interpolation
  interpolate(img, x, y, a11, a12, a21, a22, patch);
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • I can imagine that this is painful. Just look at the `if (x >= 0 && y >= 0 && x < width && y < height)`. This is inside a double loop. For every output pixel, it's checking whether covered by input pixels. I'd first profile what the typical overlap is. Are most inputs fully inside the output? Are most outputs fully inside the input? If either is the case, you'd benefit from a dedicated function. – MSalters May 03 '17 at 13:41
  • @MSalters Thanks so much for your comment and help, I really appreciate that (you have no idea how much I'm loosing my mind on this :D ). Can you please rephrase your comment? Especially the "dedicated function" part? – justHelloWorld May 03 '17 at 13:43
  • In case you don't understand the function, this function performs a linear transformation (rotation, move, scale) from one rectangular image to another. Imagine all the different ways in which two rectangles can overlap - the code above deals with all possibilities, but it does so on a pixel by pixel basis. But there are often special cases, including one rectangle lying fully within another or vice versa. – MSalters May 03 '17 at 15:10
  • @MSalters I kinda understand this, but the problem here is that this function is poorly implemented on perfomance aspect and I was wondering what is the equivalent function in OpenCV, which is better implemented for sure. Can you help me with this please? – justHelloWorld May 03 '17 at 15:51
  • sorry, no time to read/analyze the code. Do you know what it does? If it is homograpgy transformation of an image there is cv:: warpaffine and warpperspective and there probably exist IPP functions for image warping, too. But don't expect a 100% equivalence, probably there are at least small differences in interpolation and border handling. – Micka May 03 '17 at 19:31
  • @Micka thanks for your comment. Actually I'm only trying to optimize this, I don't quite understand the code. I totally forgot I opened a similar question [here](http://stackoverflow.com/questions/43364596/how-can-i-rewrite-this-warp-affine-using-opencv) if you want to take a more detailed look. – justHelloWorld May 03 '17 at 19:37

0 Answers0